His function won't allocate anything at boot time.

If you look close, you'll notice he is using

        char command_string[MSL];

which will allocate memory on the stack when the function is called, not

        static char command_string[MSL];

The only difference in the two functions is that one allocates memory
on the heap and one on the stack.
The heap alloacation will take a little bit longer (more work to do),
while the stack allocation will use up space on the stack (Get too many of those MSL buffers on the stack at the same time and you'll have some problems. I'm guessing that's why the original
function didn't use the stack).


Dennis


On Wed, 6 Apr 2005, Richard Lindsey wrote:

Well, the memory for that allocation won't be freed before it's done
being used, as do_function will wait for the function to return before
freeing that chunk, at which point it's done being used... if, for some
reason, the function being called were to free the memory internally
before returning to do_function, then you would just get a warning in
your log that free_string tried to free an invalid size of <whatever>...
your revised function looks like it would be ok as well, except that it
will allocate the memory for command_string at boot time, even if
do_function was never called during gameplay... the current method
ensures that memory is only allocated for the command_string if
do_function is called... since it's only 1 variable that's MSL in size,
it won't really impact performance any, as tons of do_func's allocate
multiple variables of that size at boot time...

Richard Lindsey.

-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Valnir
Sent: Wednesday, April 06, 2005 10:57 AM
To: [email protected]
Subject: Re: Memory issues.

Well.. I think I found ONE of my problems.. It's in the function
"do_function".

/* function to keep argument safe in all commands -- no static strings
*/
void do_function (CHAR_DATA *ch, DO_FUN *do_fun, char *argument)
{
   char *command_string;

   /* copy the string */
   command_string = str_dup(argument);

   /* dispatch the command */
   (*do_fun) (ch, command_string);

   /* free the string */
   free_string(command_string);
}

Umm.. is it just me, or does this seem a little wasteful to allocate
memory
that could get released before it's done being used? I also posted my
REVISED version, please tell me if it makes more sense.

/* function to keep argument safe in all commands -- no static strings
*/
void do_function (CHAR_DATA *ch, DO_FUN *do_fun, char *argument)
{
   char command_string[MSL];

   /* copy the string */
   strcpy( command_string, argument );

   /* dispatch the command */
   (*do_fun) (ch, command_string);
}

- Valnir

Reply via email to