Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

billybob

knowing how big to make buffer for sprintf?

This topic is 5334 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

I am wondering how big to make a buffer for sprintf to print in. I''ve thought of a few things, like multiply the length of the format string by 2 or so. all of the ways that I''ve found are either too inefficient or prone to mistakes. for example, multiply by 2 will fail for: sprintf(Buffer, "%s", "asdf"), or even sprintf(Buffer, "%i", any number above 999). obviously these examples I should use the C++ standard library for, and I do. but there are a few things where I find sprintf easier to work with, and I''d like a reliable way to do this. I noticed that sprintf returns strlen of the string, but I''m not sure how to use this information, because I''m actually using vsprintf, and I doubt the va_list va_start va_end work twice for the same arguments twice (as I understand them, that is, very little) currently I just allocate a buffer 1024 chars, and hope. I noticed there is _vsnprintf which lets you give the maximum, but I got the impression thats a MS specific standard library implementation thing

Share this post


Link to post
Share on other sites
Advertisement
quote:
Original post by billybob
I noticed there is _vsnprintf which lets you give the maximum, but I got the impression thats a MS specific standard library implementation thing


The 'v' and 'n' variants of printf are standard in C99, and relatively common otherwise. Don't be afraid to use them.

If you want printf-style formatting with C++ streams, check out boost::format.


“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 (C programming language co-inventor)


[edited by - Fruny on January 12, 2004 10:01:25 PM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!