Sign in to follow this  

Array of chars size

This topic is 4690 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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 use
void 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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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() .


Share this post


Link to post
Share on other sites

This topic is 4690 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this