Re: Who MOTD

From: Daniel A. Koepke (dkoepke@circlemud.org)
Date: 11/09/01


On Fri, 9 Nov 2001, Pj Bean wrote:

> char whomessage;

You mean "char *whomessage = NULL;"  I don't believe he wants a single
character whomessage.

> whomessage = str_dup("");

This allocates a one-byte string and fills it with '\0'.  This is not what
you want.  You want to duplicate buf, assuming buf has the text in it that
you want:

  whomessage = str_dup(buf);

Of course, if you do this multiple times, you're orphaning the memory
you've allocated each time.  So instead of the above, you really need to
do

  if (whomessage)
    free(whomessage);
  whomessage = str_dup(buf);

so that you free memory you've allocated before you lose the pointer to it
by allocating new memory and assinging its address to the whomessage
pointer.

> //if not comment that out we dont neccessarily have to
> //give mem to characters ...only big ones to keep it stable

This isn't true.  You can have very large C arrays without affecting
stability.  It's a waste of memory, though, if you're not going to be
filling it all.

> whomessage = buf;

This points whomessage to buf.  Let's say that we do:

  do_whomessage(ch, "Blah.");
  sprintf(buf, "Foo.");
  printf("whomessage = %s\n", whomessage);

The output will not be what a newbie expects:

  whomessage = Foo.

This is because whomessage merely points to the address-of the buf array's
beginning.  By changing buf, you're changing the memory that whomessage
points to.

> strcpy(whomessage,buf);

If you're going to go that route, you really want

  strncpy(whomessage, buf, <size of the buffer>);

where <size of the buffer> is the size of the statically allocated
whomessage array.

> send_to_char("Who message set.\n\r",ch);

CRLF is the proper line termination sequence, not LFCR.  Use "\r\n".

> return;

As in the other message, this is unnecessary and potentially confusing.

> I've never had to ...but use buf instead, its usually already sizable
> enough for it.

As mentioned above, it's not a size concern, but due to the nature of
pointers.

> its called being a newbie, maybe he hasnt read a book

That's still his failure.  But don't misinterpret that as an attack or
read more into the statement than there is.  The reason it's brought up is
that this a CircleMUD mailing list and such C-related questions are barely
on the periphery of this list's topics.  There are C-specific forums in
which these questions are better asked, such as comp.lang.c.  The fact of
the matter is that using this list for asking basic questions about the C
language is no more appropriate than using this list for asking basic
questions about the English language.  While it's not entirely wrong,
there are much better resources for these things that do not increase the
noise on this list.  I cannot imagine someone going to comp.lang.c and
asking how to add classes to their CircleMUD, even though CircleMUD is
written in C.  That the converse situation happens so frequently is a bit
baffling.

It falls to common sense and courtesy.  As there are forums specifically
for these types of questions, one should try there first.

-dak

--
   +---------------------------------------------------------------+
   | FAQ: http://qsilver.queensu.ca/~fletchra/Circle/list-faq.html |
   | Archives: http://post.queensu.ca/listserv/wwwarch/circle.html |
   +---------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/06/01 PST