Advertisement

char vs string vs strstream

Started by September 24, 2002 10:22 PM
15 comments, last by taciturn 22 years, 1 month ago
I have been wondering for a while why it seems that so many people prefer to use string and/or strstream over the standard char. I know some people consider string and strstream easier than char, but sacrificing performance for ease feels so dirty. My questions are as follows: 1) How much memory does an object of type "string" use? 2) What is the big advantage of "strstream" over "string" ? 3) Why do people prefer "string" over using "char" with sprintf(), strcmp(), and so on. 4) Do people see the abundance of "new computers" as an excuse to slack off whilst programming (assuming that the above methods indeed sacrifice efficiency for ease -- if not, ignore this)? I haven't seen (m)any 'char vs string' articles in my Google query, so I decided to ask these questions once and for all. /*-=-=-=-=-=-=-=-=-=-=-=-= trae@illicitstudios.com =-=-=-=-=-=-=-=-=-=-=-=-*/ [edited by - taciturn on September 24, 2002 11:24:42 PM]
/*-=-=-=-=-=-=-=-=-=-=-=-= trae@illicitstudios.com=-=-=-=-=-=-=-=-=-=-=-=-*/
quote: Original post by taciturn
I have been wondering for a while why it seems that so many people prefer to use string and/or strstream over the standard char.

Because std::string better represents the concept of what a string actually is. Because arrays in C++ suffer from all manner of bogosity. Because dynamic allocation of memory puts a lot of responsibility on the user of a library to get things right or risk leaks or undefined behaviour. Because using char arrays just plain makes it harder to write correct s/w.
quote:
I know some people consider string and strstream easier than char, but sacrificing performance for ease feels so dirty.

Who said anything about performance? You''ve jumped to a conclusion here, and its an incorrect conclusion.
quote:
My questions are as follows:
1) How much memory does an object of type "string" use?

Err... how long is a piece of string (snigger)?
quote:
2) What is the big advantage of "strstream" over "string" ?

In what respect? In the respect that string isn''t deprecated maybe? Other than that, they represent different concepts.
quote:
3) Why do people prefer "string" over using "char" with sprintf(), strcmp(), and so on.

In simple terms: its a "better" abstraction.
quote:
4) Do people see the abundance of "new computers" as an excuse to slack off whilst programming (assuming that the above methods indeed sacrifice efficiency for ease -- if not, ignore this)?

Some people do, some people don''t. OTOH, some people wastefully engage in premature optimisation, taking much longer to write s/w than is necessary.
quote:
I haven''t seen (m)any ''char vs string'' articles in my Google query, so I decided to ask these questions once and for all.

Why don''t you try using std::string for a couple of months and then make up your own mind about it? I''m sure that you will not want to go back to char arrays.
Advertisement
quote: Who said anything about performance? You've jumped to a conclusion here, and its an incorrect conclusion.


I have always assumed that easy and efficient are two words that never can be mixed.... yet yesterday they were. My world seems to be flipped upside down at the moment because I am so disillusioned.

quote: Err... how long is a piece of string (snigger)?

If I understand this pseudo-response correctly, a std::string takes the same amount of memory as a char array.

However, it was late when I posted this, and the question I meant to ask was which is more efficient. So back to asking questions:

And with that I append these questions:

5) Does std::string use more/less/same processor cycles than strcmp(), strcat(), sprintf(), and others with char?

quote: In what respect? In the respect that string isn't deprecated maybe? Other than that, they represent different concepts.


I appologize for not being clear, as I have already mentioned, it was late.

6) What different concepts do string and strstream represent?

quote:
Some people do, some people don't. OTOH, some people wastefully engage in premature optimisation, taking much longer to write s/w than is necessary.


7) agree with that, but are you saying the difference between char arrays and std::string is so little it is not a necessary optimization?

I probably should invest in a book about the standard libraries, but right now I don't have the money.

quote:
Why don't you try using std::string for a couple of months and then make up your own mind about it? I'm sure that you will not want to go back to char arrays.

will do.

/*-=-=-=-=-=-=-=-=-=-=-=-=
trae@illicitstudios.com
=-=-=-=-=-=-=-=-=-=-=-=-*/

[edited by - taciturn on September 25, 2002 1:55:24 PM]
/*-=-=-=-=-=-=-=-=-=-=-=-= trae@illicitstudios.com=-=-=-=-=-=-=-=-=-=-=-=-*/
quote:
5) Does std::string use more/less/same processor cycles than strcmp(), strcat(), sprintf(), and others with char?

At least not less than a corresponding char *. A string can reserves a little more memory than what is used, so the stirng can be expanded without reallocation. Should not be a problem cause you don''t have strings of several megabytes, or do you?

quote:
6) What different concepts do string and strstream represent?

A string can be though of as C''s char *, while strstream is C''s sprintf. Then, if you know the difference between char * and sprintf, you know the difference between string and strstream.

quote:
7) agree with that, but are you saying the difference between char arrays and std::string is so little it is not a necessary optimization?

Sort of. You have to look in your code and see WHERE you use your strings. Do you use them in your speed critical inner rendering loops? Do you use them only, say, during initialization of your program? Also, don''t forget that string handles memory management for you, and they expand automatically if needed. Less chance of memory leaks.

Also strstream is, as SabreMan said, deprecated, you should use stringstream instead.
quote: Original post by taciturn
If I understand this pseudo-response correctly, a std::string takes the same amount of memory as a char array.

It depends on the specific implementation. It can take the same amount, it can take a negligible amount more (say, enough space for an end-of-string marker), or it might take less if you have a reference-counted implementation and your usage is conducive to sharing. The average scenario is probably that std::string uses a little more memory, but performs reallocations quicker. Hence, if you are doing a lot of reallocations, it might be quicker overall. Or then again, maybe it won''t...
quote:
However, it was late when I posted this, and the question I meant to ask was which is more efficient.

It really depends. It could swing either way depending on how you use them. std::string reallocation strategy is OK for general-purpose usage, and you might even find it more efficient than some of your own solutions.
quote:
7) agree with that, but are you saying the difference between char arrays and std::string is so little it is not a necessary optimization?

Its an unnecessary optimisation until proven otherwise. All this talk of optimisation is rather meaningless when devoid of a concrete scenario.
Actually, string provides significantly better performance than char * in certain (classical) scenarios.

char* uses a null terminator, that''s an easy, but pretty inefficient way to represent a string. Most implementation of string use a character counter instead, as well as additional memory management. This has a big advantages: string manipulations can be much better optimized.

Consider strlen(). It has to loop through the whole char* array, until it reaches the terminator. And that, just to return the length. A string size() simply returns the length counter.

Also, string supports enhanced error checking, if the user wishes, eg. bound checking. How often does it happen that you write over the allocated space of a char* array. The result are bugs that can be very hard to trace (since almost unreproduceable).

All that, in combination with easy of use and the simple elegance of a templated type with operators, makes string definitely the choice number one for abstract high level code.

/ Yann
Advertisement
The reason why windows crashes so frequently and why internet has so many "buffer overflow" security issues is because the programmers uss char*, and forgets to do range checking/reallocation.

2. strstream is an advantage over char* but it is badly designed, therefore deprecated. Use stringstream instead.

4. If using string over char* makes such a big efficiency difference, then you have bigger problems to worry about. Or you must working in such a limited platform, in which case why not go directly to assembly?
quote: 4. If using string over char* makes such a big efficiency difference, then you have bigger problems to worry about. Or you must working in such a limited platform, in which case why not go directly to assembly?


I dont have a problem with using char, thats why I posted in the first place.

Tomorrow I have to run out to buy parts for a new development/testing platform, and while I am out I am going to buy a book on the standard libs. I guess I should have followed my instincts and just bought the book in the first place.

Thank you anyway.

/*-=-=-=-=-=-=-=-=-=-=-=-=
trae@illicitstudios.com
=-=-=-=-=-=-=-=-=-=-=-=-*/

[edited by - taciturn on September 26, 2002 2:31:33 AM]
/*-=-=-=-=-=-=-=-=-=-=-=-= trae@illicitstudios.com=-=-=-=-=-=-=-=-=-=-=-=-*/
quote: Original post by taciturn
Tomorrow I have to run out to buy parts for a new development/testing platform, and while I am out I am going to buy a book on the standard libs. I guess I should have followed my instincts and just bought the book in the first place.


Suggestions :
"The C++ Standard Library" by N. Josuttis
"The C++ Programming Language, Special Edition" by B. Stroustrup


Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
I was thinking of buying a book by Stroustrup, but many people I have talked to tell me that his explanations are not very good, unless you have a great explanation of what is going on already. Maybe I have that understanding, but hey - there's only one way to find out!

If I drive back here with enough money left I will buy them both as well as either "PC Intern System Programming," "C++ Power Paradigms," or "The Data Compression Book" as these three topics are of great interest to me.

Thanks again.

[edited by - taciturn on September 26, 2002 2:46:22 AM]
/*-=-=-=-=-=-=-=-=-=-=-=-= trae@illicitstudios.com=-=-=-=-=-=-=-=-=-=-=-=-*/

This topic is closed to new replies.

Advertisement