The Danger of c-style Strings
someone972

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.

Thomas Fjellstrom

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

PEBKAC.

Goalie Ca

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

Edgar Reynaldo

What is a rope? How do you use it?

Kibiz0r

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

Couldn't resist , could you?

Kibiz0r

I could, but I didn't.

Edgar Reynaldo

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
SGI extension and not part of the C++ standard (their words).

http://www.sgi.com/tech/stl/Rope.html

CursedTyrant
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

Kris Asick

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

GullRaDriel

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.

_

CGamesPlay
Quote:

This only applies to c++ programmers.

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

GullRaDriel

No, the alien have not invaded C, that common knowledge they are residing inside the stl ;-p

Thomas Fjellstrom
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

"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

Samuel Henderson

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
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
BAF said:

Why?

Maybe because they use so many template arguments?

BAF

C++ string objects do?

Goalie Ca

Yes they do. basic_string doesn't though IIRC.

BAF

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

Richard Phipps

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

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

James Stanley

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
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!

Trezker

Templated arguments in strings? I have been using strings a lot and I haven't noticed a single templated thing about them, ever.

BAF

I asked the same thing. I guess the other string class has templated arguments, but std::string is just a typedef for basic_string<char>, and basic_string doesn't have templates.

Neil Black

I'm sorry, but I just don't see why this thread was started. Did anyone NOT know that? Excepting maybe a few beginners who don' have much experience?

Thomas Fjellstrom
Quote:

basic_string doesn't have templates.

No, but it IS a template.

BAF

So? That doesn't make the "templates are confusing" argument valid. You can happily use std::string and be totally ignorant of templates for the most part (you may be confused by some compiler errors, then again, I've seen way worse errors).

Neil Black

I use std::string, and I have no idea what a template is. I haven't had a problem with it since the last one I asked about here.

bamccaig

I appreciate the free refreshments. :)

BAF
Quote:

I use std::string, and I have no idea what a template is. I haven't had a problem with it since the last one I asked about here.

Hear the evidence for yourself!

ImLeftFooted

Heh, why do threads like this always end in an argument?

BAF

Hmm, I said something about it being a.cc, and expecting a thread not to end up like that. I forgot who sigged it.

Thread #591346. Printed from Allegro.cc