• Welcome to Valhalla Legends Archive.
 

Seeking Quick Answer

Started by Mephisto, January 10, 2006, 10:44 PM

Previous topic - Next topic

Mephisto

nevermind, fixed

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.
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!

Mephisto

#2
Quote from: Kp on January 10, 2006, 10:58 PM
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.

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?

MyndFyre

Quote from: Mephisto on January 10, 2006, 10:44 PM
nevermind, fixed

Please don't do things like this.  It destroys any chance of other people learning from your question.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Mephisto

Quote from: MyndFyre on January 11, 2006, 12:12 AM
Quote from: Mephisto on January 10, 2006, 10:44 PM
nevermind, fixed

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

There was nothing to learn from.  Was stupid, really.  If there was any though, Kp mentioned it.

Kp

Quote from: Mephisto on January 10, 2006, 11:00 PMWhat do you recommend for an alternative to sprintf() when you want to have variables easily inserted into the buffer in specific places?

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!
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!