Array of chars size

Started by
5 comments, last by Zahlman 19 years, 2 months ago
When you want to put on screen a string, of which you don't know its size, how much chars you must alloc ? Imagine this:
[Source]
char *str = new char[100]; 
sprintf(str,"zzzzzzz %s %d %f %g",s,i,f,f);
delete str;
[/Source]
this code is called much times with different values for string if you use char[ ] with a low value maybe your string wont fit in array, but if you use an high value, you are alloc and dealloc all time much memory. thnaks
Advertisement
Well if you know the size of the string, then this can be achieved like this:

void SizeStr(char* str){    char *buffer = new char[ strlen(str) ];    sprintf( buffer, str );    delete buffer;}


Of course you would have to send the final string in before hand, since it calculates the size from that.

However, since you do not know the length of the string you can just do something like this:

char buffer[2048]; // static buffer that is > the max string size you will ever usevoid UseStr(int s, long i, float f){    memset( buffer, 0, 2048);    sprintf(buffer,"zzzzzzz %s %d %f %g",s,i,f,f);}


That way you will not have any memory fragmentation and the stack is used to hold the memory. I would personally use that method rather then always allocating and deallocating memory, but you must at least give it a good max size to be safe. You can alwasy add in checks and use snprintf instead as well.

- Drew
Thanks! I'll use the static array
Quote:Original post by Silly_con
Thanks! I'll use the static array


And that's a buffer overflow waiting to happen [rolleyes]. Since you're using C++ already, just use a std::stringstream, possibly with boost::format to get printf-style formatting.

If you're unwilling to do so, at least use snprintf instead of sprintf, as it'll let you specify the maximum number of characters to write, and thus make sure you don't overflow the buffer.
"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
A little off topic - Whenever you use new[], make sure you delete it with delete[] and not delete. Otherwise you get a nice memory leak because only the first element is actually freed.
Quote:Original post by Evil Steve
Otherwise you get a nice memory leak because only the first element is actually freed.


Worse, you get undefined behaviour. Often, you just get a crash.
"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
If you are using 'new', you are using C++. If you are using C++, you are PLEASE PLEASE PLEASE not using char* for textual data in any but the simplest cases.

stringstream ss("zzzzzzz ");ss << s << " " << i << " " << f << " " << f;// If the difference in formatting between %f and %g is really important to you,// then I'm afraid I can't help you, but I'm sure someone else around here can.// Access the string with ss.str() .// Access the underlying string representation (if you really have to) with// ss.str().c_str() .

This topic is closed to new replies.

Advertisement