RE: [SMAUG] Usage of str_dup, and other memory handling functions
RE: [SMAUG] Usage of str_dup, and other memory handling functions
Thanks Xerxes
Actually, (this should simplify life) I am not running a smaug
derivative (ducks due to this being the SML)
It's a merc derivative (and theres no mailling list for that, from what
I've seen)
Anyhow, thanks all.
Krule
-----Original Message-----
From: Xerves [mailto:xerves@rafermand.net]
Sent: Wednesday, December 25, 2002 10:21 PM
To: Eos
Cc: 'Ben Baril'; smaug@realms.game.org
Subject: Re: [SMAUG] Usage of str_dup, and other memory handling
functions
A general rule of thumb is to always use the macros/functions with
anything allocated in SMAUG, he might be right
about what will work and what will not, but you should use strcat,
strcpy only on defined variables or a variable you
are going to pass to variable that is being allocated by STRALLOC or
str_dup.
So it would look like this
char buf[MAX_STRING_LENGTH];
sprintf(buf, "A sword lies here.\n\r");
if (newfield)
STRFREE(newfield);
newfield = STRALLOC(buf);
I have also had problems I think with STRALLOC and strings passed like
this STRALLOC("Insrt string here");
maybe it was str_dup, but I also try to use buffers, just something odd
that came up once or twice for me.
LASTLY, always use nonhashed and hashed macros/functions with their
equivalant, mixing will cause errors that might pop up in 1 hour or 6
months, the latter being rather hard to find because you are
thinking current
--X
Eos wrote:
>Easy enough.
>
>Two categories:
>
>Hashed, and non-hashed.
>
>Hashed are saved into the hash table and consist of:
>
>Fread_string, stralloc, and to remove either, strfreee
>
>Nonhashed consist of:
>
>Fread_string_nohash, str_dup, and dispose to free.
>
>Strcat should be safe to use with non-hashed strings, provided its not
>used to empty them. Smaug itself does this in mud_prog.c where it
>parses variables.
>
>The requirement for strcat is that you cannot use it on a NULL, it must
>have been initialized to at the very least yourtext[0] = '\0' or actual
>text, or it will cause problems, and likely crash.
>
>strcpy is often used in its place, if you don't know if the variable
>will have been initialized already. The flaw in this, is strcpy
>overwrites, it does not append/concatenate. You can begin a string
>using strcpy, and append to it using strcat, to insure there has been
>something added.
>
>As long as the string is not one that is being hashed, you can strcpy
>and strcat it all you like. If the string is being hashed, anywhere at
>all, you'll be causing problems, and keep in mind, in your loading
>routines, if you haven't add that _nohash to fread_string, you're
>unintentionally hashing.
>
>All those annoying buffers, freeing, etc, is why I finally made a
>strdup and stralloc printf, to format them better.
>
>|-----Original Message-----
>|From: owner-smaug@game.org [mailto:owner-smaug@game.org] On Behalf Of
>Ben
>|Baril
>|Sent: Wednesday, December 25, 2002 12:57 PM
>|To: smaug@realms.game.org
>|Subject: [SMAUG] Usage of str_dup, and other memory handling functions
>|
>|First off...
>|
>|Merry X-Mas all! :-)
>|
>|Now onto business. Been a while since I posted to this group, but I
>have
>|a pretty generic question that hopefully someone here can help me
>|with. I recently started to teach a friend of mine how to code for a
>|mud. He got the hang of it pretty quickly and helped me implement some
>|code for my account system.
>|
>|However I recently noticed that some new code he put in completely
>|messed up pfiles, filling all the empty strings, with some value.
>|
>|I went looking for where this value originated from, and eventually
>|discovered it was this line of code:
>|
>| strcat( pAccount->referral, " " );
>| strcat( pAccount->referral, d->account->owner );
>|
>|That didn't look right to me, since the referral string is initialized
>|as referral = str_dup(""); and from what I know of str_dup'ed strings,
>|you can't just go strcat them willy nilly, you need to free them, then
>|str_dup them as one whole string.
>|
>|I changed the code to look like this:
>|
>| char buf[MSL];
>| strcpy(buf, pAccount->referral);
>| strcat(buf, " ");
>| strcat(buf, d->account->owner);
>| pAccount->referral = str_dup(buf);
>|
>|Now, that works..which is good..
>|
>|What I'm looking for is like a basic guide of things you can/cannot do
>|with str_dup'ed string, and when to use str_dup, STRALLOC, and all
>those
>|other macros. I've seen one around before, and I myself have a good
>|grasp, but a guide would be kinda neat to show him. Thanks all!
>|
>|Krule
>
>
>
>