Valhalla Legends Forums Archive | C/C++ Programming | Seeking Quick Answer

AuthorMessageTime
Mephisto
nevermind, fixed
January 11, 2006, 4:44 AM
Kp
Where to begin?  It's silly to dynamically allocate, then immediately free, a fixed size buffer.  Make it an automatic variable instead.  Also, you're deleting it wrong.  An allocation with scalar new requires a release with scalar delete (delete [] buf;).  Finally, don't use sprintf.  It's ok here (your buffer's big enough), but be very wary of getting into the habit of using unsafe string manipulation functions.
January 11, 2006, 4:58 AM
Mephisto
[quote author=Kp link=topic=13876.msg141401#msg141401 date=1136955529]
Where to begin?  It's silly to dynamically allocate, then immediately free, a fixed size buffer.  Make it an automatic variable instead.  Also, you're deleting it wrong.  An allocation with scalar new requires a release with scalar delete (delete [] buf;).  Finally, don't use sprintf.  It's ok here (your buffer's big enough), but be very wary of getting into the habit of using unsafe string manipulation functions.
[/quote]

Thank you for the coding tips (it's been quite a long time since I've programmed and my coding habits then weren't even very good!).  I can't believe I forgot the requirement of delete [] buf; for deleting arrays, silly..

What do you recommend for an alternative to sprintf() when you want to have variables easily inserted into the buffer in specific places?
January 11, 2006, 5:00 AM
Myndfyr
[quote author=Mephisto link=topic=13876.msg141395#msg141395 date=1136954690]
nevermind, fixed
[/quote]

Please don't do things like this.  It destroys any chance of other people learning from your question.
January 11, 2006, 6:12 AM
Mephisto
[quote author=MyndFyre link=topic=13876.msg141409#msg141409 date=1136959958]
[quote author=Mephisto link=topic=13876.msg141395#msg141395 date=1136954690]
nevermind, fixed
[/quote]

Please don't do things like this.  It destroys any chance of other people learning from your question.
[/quote]

There was nothing to learn from.  Was stupid, really.  If there was any though, Kp mentioned it.
January 11, 2006, 3:30 PM
Kp
[quote author=Mephisto link=topic=13876.msg141402#msg141402 date=1136955642]What do you recommend for an alternative to sprintf() when you want to have variables easily inserted into the buffer in specific places?[/quote]

snprintf (Unix only).  Microsoft provides a similar function (_snprintf), but their implementation is not compatible with traditional semantics.  Specifically, it fails to perform one of the major tasks of a length-limited string function: ensuring the buffer is null terminated!
January 12, 2006, 2:51 AM

Search