[Source]
char *str = new char[100];
sprintf(str,"zzzzzzz %s %d %f %g",s,i,f,f);
delete str;
[/Source]
Array of chars size
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:
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
Well if you know the size of the string, then this can be achieved like this:
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:
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
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
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.
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.
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
Popular Topics
Advertisement