why not to use char * for strings?

Started by
14 comments, last by johnnyBravo 20 years, 6 months ago
Hello johnnyBravo,

use of const char* is alright to do for any "string" in C/C++
or char* if your allocating a buffer to do something with like copy data into it and sending over socket.
We have a multi-threaded app that takes care of sending data over socket so we just pass it a buffer and size of buffer and when it done sending it deletes the buffer.

Else if your going to do things with the string then like the others point out std:string is alright to use and should be use as much as possible. But I would advise a few rules.
1) try not to past a std::string by value always do it by reference.
2) try to avoid return of a string.

This will avoid a lot of temp strings and copy memory.

in example:
very efficient
void myMethod(string & t)
{
t = "Hi";
}
next is retun a sting of the temp string.
creates a stack sting on stack allocating memory for "Hi"
then returns this which involves creating another temp string
then copies memory form stack to the temp string
then copies form this temp string to the sting being assigned.
string myMethod();
{
string stack("Hi"); //create on stack
return stack; // has to create a temp sting and copy stack data into it
}

Note most compiler tempory return object stay around until end of block ie. closing }
But I would always use the myMethod with pass in reference my self.
it cleaner code and no mem copies and no temp string do to return of a string.

Lord Bart




[edited by - lord bart on October 14, 2003 9:50:51 AM]
Advertisement
As I've already said many times before, the conversion to a char* is deprecated, as stated by the standard. That is, use const char*.

[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]

[edited by - Lektrix on October 14, 2003 11:03:31 AM]
[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || [email=lektrix@barrysworld.com]E-Mail Me[/email] ]
Thanks alot on all the insight, it has cleared things up for me.
quote:Original post by Lord Bart
2) try to avoid return of a string.



[edited by - lord bart on October 14, 2003 9:50:51 AM]



About the avoiding return of a string,
i didnt realise this until you described it in that sense,

like i had a read method for reading text files.
And i made it return a string instead of using references.

I think i''ll change it back to using references

Im just use to using vb where most things return stuff

quote:Original post by Last Attacker
I myself like to use char* instead of std::string.

Firstly its faster;

Except when it isn''t, of course.

quote:Secondly I''m comfortble with it; and
Thirdly even though I know I have to manage the memory for it, it keeps me from slacking off into the lazy chair and I know exactly whats going on.

Bollocks. Most people are too busy with actually writing applications to bother with allocating memory for strings.

quote:Sure the std::string is comfortable to work with and does everything for you but if you have an array that is supposed to hold about 10,000 strings, then you are wasting a lot of RAM!

You''re "wasting" perhaps 16 bytes per string. That hardly constitutes a "lot" of RAM.
char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;}  /*** drpizza@battleaxe.net ***/

As with all things, "It depends" is the best answer.

I assume you are aware that the ''char'' datatype isn''t guaranteed to be eight bits in size from platform to platform. If you ever want to work with multi-byte character sets, such as Unicode, you could find that your prized, hand-rolled string manipulator code falls to pieces.

Also, some operating systems don''t allow zero terminated strings on principle. (There are those who believe that such constructs are unsuitable for anything beyond low-level programming.) Symbian versions 5.1 and up, for instance, are Unicode through and through; char datatypes aren''t used. It doesn''t allow zero-terminated strings as these are prone to buffer overflow issues as well as the memory management requirements mentioned by other posters.

Sure, you may THINK you can cope with all that malloc/free stuff for each and every string you deal with, but why bother? It''s a detail best left to the computer, since tedious make-work like that is what computers are *for*. Why make life harder for yourself?

For those who think std::string is ''too slow'', check your algorithm, not your tools. The standard string class is a lightweight wrapper, nothing more.

char * is fine if you''re doing simple low-level coding in C, because that''s what it was designed for. C was designed as a ''portable assembler'' at a time when few computers even supported graphics or VDUs. It certainly wasn''t intended to be used in complex applications 30 years later.


--
Sean Timarco Baggaley
Sean Timarco Baggaley (Est. 1971.)Warning: May contain bollocks.
why babble about this when it''s so clear.

If you''re going to make modifications to the string later in the code, use a string class.

If you''re not going to make modifications to the string then use char*.

This topic is closed to new replies.

Advertisement