|
This thread is locked; no one can reply to it. |
1
2
|
The Danger of c-style Strings |
someone972
Member #7,719
August 2006
|
This only applies to c++ programmers. char* name = "bob"; name[0] = 'B';//BUG!
The first line assigns a name to name, but it's a const char*! When the second line tries to edit the const char it has an error and might crash. The compiler doesn't detect this because it sees it as you modifing a char*. char* name = "Bob"; name = "Joe";
With character arrays this isn't a problem because the compiler automatically copies the characters into the array. char name[] = "Bob"; strcpy(name,"This_name_is_too_long!"); //Writes into unknown space, possibly over another object! this can also happen if the user forgets to put a terminating NULL on the end of the string. char* make_all_ms(char in) { for(int i = 0; in<i> != '\0';i++) in<i> = 'm'; } char name[4]; name[0] = 'H'; name[1] = 'i'; name = make_all_ms(name);//BUG!
The solution to all your problems: using namespace std; string str1 = "Hello World!";//This works. string str2; str1 += "I own a lizard." //concatenates the strings. Now is "Hello World!I own a lizard." str2 = str1; Also if you need to use a c-style string in a function or something you can do this: The string cleans itself up when it goes out of scope. ______________________________________ |
Thomas Fjellstrom
Member #476
June 2000
|
Of course C strings are dangerous if you do something wrong PEBKAC. -- |
Goalie Ca
Member #2,579
July 2002
|
I recommend ropes myself for large chunks of text. In such a case regular strings can wreak havock ------------- |
Edgar Reynaldo
Major Reynaldo
May 2007
|
What is a rope? How do you use it? My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
Kibiz0r
Member #6,203
September 2005
|
A rope (IPA: rəʊp) is a length of fibers, twisted or braided together to improve strength for pulling and connecting. It has tensile strength but is too flexible to provide compressive strength (i.e., it can be used for pulling, not pushing). Rope is thicker and stronger than similarly constructed cord, line, string, or twine. Common materials for rope include natural fibers such as Manila hemp, hemp, linen, cotton, coir, jute, and sisal. Synthetic fibers in use for rope-making include polypropylene, nylon, polyester (e.g. PET), polyethylene (e.g. Spectra) and Aramids (e.g. Twaron, Technora and Kevlar). Some ropes are constructed of mixtures of several fibres or use co-polymer fibres. Ropes can also be made out of metal fibers. Ropes have been constructed of other fibrous materials such as silk, wool, and hair, but such ropes are not generally available. The use of ropes for hunting, pulling, fastening, attaching, carrying, lifting, and climbing dates back to prehistoric times and has always been essential to mankind's technological progress. --- |
Kibiz0r
Member #6,203
September 2005
|
I could, but I didn't. --- |
Edgar Reynaldo
Major Reynaldo
May 2007
|
I should just answer my own questions any way. Here's a link to another definition of rope which as I found out is an My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
CursedTyrant
Member #7,080
April 2006
|
Quote:
The solution to all your problems: No, really? Oh come on, the string class was probably the first thing I learned from STL. It's really not that hard to find and there are a lot of examples out there on how to use it. Your post is just nothing new, sorry. Besides, since it's not a programming question, I believe it doesn't belong to this forum --------- |
Kris Asick
Member #1,424
July 2001
|
I use c-style strings everywhere, I just keep in mind that they're like arrays of characters, not strings, and treat them as such. If I want to mess with them as a string, I use a string command. (You'll never catch me copying one string to another with an = sign for example.) --- Kris Asick (Gemini) --- Kris Asick (Gemini) |
GullRaDriel
Member #3,861
September 2003
|
You programmer must know what you do. If not, go with some scripting language who will get the job done for you. C is not for quiche eater. I said. _ "Code is like shit - it only smells if it is not yours" |
CGamesPlay
Member #2,559
July 2002
|
Quote: This only applies to c++ programmers. Do C programmers just know what they're doing or something? -- Ryan Patterson - <http://cgamesplay.com/> |
GullRaDriel
Member #3,861
September 2003
|
No, the alien have not invaded C, that common knowledge they are residing inside the stl ;-p "Code is like shit - it only smells if it is not yours" |
Thomas Fjellstrom
Member #476
June 2000
|
Quote: Do C programmers just know what they're doing or something? They have to learn quickly or risk blowing their foot off. Whereas C++ lets you quietly blow your foot off -- |
Kris Asick
Member #1,424
July 2001
|
"How much C could a C++ if a C+ could +C?" A lot of experts use C over C++, and the two can be combined in as much or as little quantity as desired. I simply prefer some of the coding conventions of C++ over C and have only recently started taking advantage of some of the more advanced C++ features. ...C++ string objects still scare me though. --- Kris Asick (Gemini) --- Kris Asick (Gemini) |
Samuel Henderson
Member #3,757
August 2003
|
I'm not sure I understand the overall point of this thread. Was it just to recommend people to switch to the string class? If it was then good job. Had I not already been using them for a long time I would have been convinced to switch (maybe) ================================================= |
BAF
Member #2,981
December 2002
|
Quote: I just keep in mind that they're like arrays of characters What are they then if they are only like arrays of characters? Quote: ...C++ string objects still scare me though. Why? |
ImLeftFooted
Member #3,935
October 2003
|
BAF said: Why? Maybe because they use so many template arguments? |
BAF
Member #2,981
December 2002
|
C++ string objects do? |
Goalie Ca
Member #2,579
July 2002
|
Yes they do. basic_string doesn't though IIRC. ------------- |
BAF
Member #2,981
December 2002
|
basic_string is a templated typedef, but what uses a template argument in basic_string? |
Richard Phipps
Member #1,632
November 2001
|
One of the things I missed when I first switched from Basic to C was proper integrated string support. Now I've got used to it though. |
m c
Member #5,337
December 2004
|
just store all strings on the heap, and remember to free them. (\ /) |
James Stanley
Member #7,275
May 2006
|
Or do what I do! EDIT: EDIT2: |
Kibiz0r
Member #6,203
September 2005
|
Quote:
Or do what I do! EDIT: EDIT2: Oh goody, I'm going to go for the high score! --- |
|
1
2
|