Skip to main content
gamedev.net
🔒 Locked

variable arguments list, vsprintf, etc.

Started by zedd Aug 15, 2001 at 12:06 PM 0 replies 550+ views
Original Post
zedd
zedd
I''m currently using a variable arguments function and vsprintf() to parse text to be added to a buffer. My function looks like this: AddBuffer(const char *entry, ...) { ... // parse the arguments list char parsed_text[2048]; va_list ap; va_start(ap, entry); vsprintf(parsed_text, entry, ap); va_end(ap); ... } This works, but it''s not safe. If the parsed string (format + all variables inserted) happens to be longer than the buffer i declared (in this case 2048), this function would crash. I can ''try'' to prevent this by declaring a huge buffer but it''s not very efficient. Anyone knows how i could allocate the buffer depending on the length of the parsed text? The only solution i found is to vfprintf() the text to a FILE and then read the text back from the file into memory (the file will never overflow). But this is not efficient either. Thanks! zedd
Anon Mike
Anon Mike
Use vsnprintf instead.

-Mike
-Mike

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.