Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » The Danger of c-style Strings

This thread is locked; no one can reply to it. rss feed Print
 1   2 
The Danger of c-style Strings
someone972
Member #7,719
August 2006
avatar

This only applies to c++ programmers.
How c-style strings are dangerous
Let's say that you are making an editable name. You type this code in:

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*.
You can still assign a different value to the whole pointer though:

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";//Works fine.
One of the major problems with character arrays is that they can write on top of uncharted memory. Consider this example:

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:
Use the string class in the std namespace.It has all the functionality of a c-style string exept it is safer and easier to use. Some examples are shown below.

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:

string name = "Bob";
textprintf(screen,font,0,0,0,0,name.c_str());

The string cleans itself up when it goes out of scope.

______________________________________
As long as it remains classified how long it took me to make I'll be deemed a computer game genius. - William Labbett
Theory is when you know something, but it doesn't work. Practice is when something works, but you don't know why. Programmers combine theory and practice: Nothing works and they don't know why. -Unknown
I have recklessly set in motion a chain of events with the potential to so-drastically change the path of my life that I can only find it to be beautifully frightening.

Thomas Fjellstrom
Member #476
June 2000
avatar

Of course C strings are dangerous if you do something wrong ::)

PEBKAC.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Goalie Ca
Member #2,579
July 2002
avatar

I recommend ropes myself for large chunks of text. In such a case regular strings can wreak havock :D

-------------
Bah weep granah weep nini bong!

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Kibiz0r
Member #6,203
September 2005
avatar

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.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Kibiz0r
Member #6,203
September 2005
avatar

I could, but I didn't.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

CursedTyrant
Member #7,080
April 2006
avatar

Quote:

The solution to all your problems:
Use the string class in the std namespace.It has all the functionality of a c-style string exept it is safer and easier to use. Some examples are shown below.

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 :P

---------
Signature.
----
[My Website] | [My YouTube Channel]

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)
--- http://www.pixelships.com

--- Kris Asick (Gemini)
--- http://www.pixelships.com

GullRaDriel
Member #3,861
September 2003
avatar

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"
Allegro Wiki, full of examples and articles !!

CGamesPlay
Member #2,559
July 2002
avatar

Quote:

This only applies to c++ programmers.

Do C programmers just know what they're doing or something? :-X

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

GullRaDriel
Member #3,861
September 2003
avatar

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"
Allegro Wiki, full of examples and articles !!

Thomas Fjellstrom
Member #476
June 2000
avatar

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 :)

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

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)
--- http://www.pixelships.com

--- Kris Asick (Gemini)
--- http://www.pixelships.com

Samuel Henderson
Member #3,757
August 2003
avatar

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) :)

=================================================
Paul whoknows: Why is this thread still open?
Onewing: Because it is a pthread: a thread for me to pee on.

BAF
Member #2,981
December 2002
avatar

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
avatar

BAF said:

Why?

Maybe because they use so many template arguments?

BAF
Member #2,981
December 2002
avatar

C++ string objects do?

Goalie Ca
Member #2,579
July 2002
avatar

Yes they do. basic_string doesn't though IIRC.

-------------
Bah weep granah weep nini bong!

BAF
Member #2,981
December 2002
avatar

basic_string is a templated typedef, but what uses a template argument in basic_string?

Richard Phipps
Member #1,632
November 2001
avatar

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
avatar

just store all strings on the heap, and remember to free them.

(\ /)
(O.o)
(> <)

James Stanley
Member #7,275
May 2006
avatar

Or do what I do!
Store things on the heap and leave them there.

EDIT:
Bonus points if you malloc() the string every loop...

EDIT2:
Without free()ing it first.

Kibiz0r
Member #6,203
September 2005
avatar

Quote:

Or do what I do!
Store things on the heap and leave them there.

EDIT:
Bonus points if you malloc() the string every loop...

EDIT2:
Without free()ing it first.

Oh goody, I'm going to go for the high score!

 1   2 


Go to: