Re: Thnx for memcpy/gdb help :)

From: Mark Devlin (markd@eskimo.com)
Date: 04/11/96


On Thu, 11 Apr 1996, Rasmus Rxnlev wrote:

> [SNIP]
>   file = fopen(fn,"wt");
>   if (!file)
>   {
>     sprintf(buf, "ERROR: Unable to get filedescriptor in write_alias()");
>     mudlog(buf, NRM, MAX(LVL_JUDGE, GET_INVIS_LEV(ch)), TRUE);
>     return;
>   }
> [END SNIP]
>
> buf is c pointer to a char, and fn is an array of chars (the filename:)

What is buf pointing to?  Can't just say it's a pointer and expect it to
work.  Kinda like handing someone the water before you give them the
cup.  :-)

> Shouldnt it work flawlessly ? I mean it's not that I don't assign anything
> to buf... Well, I'm actually wondering if it's because buf is a pointer
> to a char... should it be an array of chars ? (the mud doesnt crash
> whenever an alias is written though..).

It most definately has to be, or point to, an array of chars.  See the
water analogy above.  :-)

You've stumbled across what can be one of the hardest bugs to find in a
C program: uninitiliazed pointers.  If you can, always compile with
all the warnings your compiler can generate turned on.  It may be annoying
when you KNOW the code's ok, but I'll be damned if it doesn't solve a
great many problems.

Also, why were you even using sprintf at all?  You only need to use it
if you want formatted output.  The string you were copying to buf didn't
have any format codes in it.

Someone (:pokes Jeremy) should make the log() and mudlog() functions
take a variable number of arguments and get rid of the "external" sprintf
stuff.  It's really easy to do.

If anyone wants, I'll make up a diff against a clean bpl10 (or 11 if it's
out) that'll do it.  Was going to do it anyway, eventually..:-)

- Mark
markd@eskimo.com	(finger markd@eskimo.com for my PGP signature)
http://www.eskimo.com/~markd	U.S.A. Home of the Alternate Reality page



This archive was generated by hypermail 2b30 : 12/18/00 PST