Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUDs ➜ General ➜ Used uninitialized in this function

Used uninitialized in this function

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Zeno   USA  (2,871 posts)  Bio
Date Sat 06 Dec 2003 03:09 AM (UTC)
Message
I seem to have encountered a problem, and I cannot think of why its doing this. The error is:


commands.c:223: warning: `dsock' might be used uninitialized in this function


Now, the dsock is defined. (As this)

D_SOCKET *dsock;


Without it, it would get a undefinded error. With it, the function crashes the MUD. Here is the full function:

void cmd_score(D_MOBILE *dMob, char *arg)
{
  BUFFER *buf = buffer_new(MAX_BUFFER);
  D_SOCKET *dsock;

   bprintf(buf, "#c- - - = = = Score = = = - - -#n\n\r" );
   bprintf(buf, "#CName: %s\n\rKills: %d\n\rDeaths: (null)\n\r", dMob->name, dMob->kills);
   bprintf(buf, "#CHost: %s\n\r", dsock->hostname );
   if ( dMob->level == 4 )
    bprintf(buf, "#CRank: Owner\n\r" );
   else if ( dMob->level == 3 )
    bprintf(buf, "#CRank: Admin\n\r" );
   else if ( dMob->level == 2 )
    bprintf(buf, "#CRank: Player\n\r" );
   else if ( dMob->level == 1 )
    bprintf(buf, "#CRank: Guest\n\r" );

   bprintf(buf, "#c- - - = = = = = = = = = - - -\n\r#n" );
  text_to_mobile(dMob, buf->data);
  buffer_free(buf);
}


What is this uninitialized problem doing, and how do I correct it?

Zeno McDohl,
Owner of Bleached InuYasha Galaxy
http://www.biyg.org
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #1 on Sat 06 Dec 2003 07:12 AM (UTC)
Message
That's what's called "dereferencing" an undefined pointer.

Your pointer, dsock, is not initialized, and therefore defaults to whatever was in the memory block it was assigned to. 99.99999% of the time, that is total giberish.

You're using dsock->hostname without assigning it a value; that's your error. You need to either have dsock point to something valid, or not have it there at all - including places where you use it.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Zeno   USA  (2,871 posts)  Bio
Date Reply #2 on Sat 06 Dec 2003 06:30 PM (UTC)
Message
I've gotten it to work other places, such as this:

void cmd_who(D_MOBILE *dMob, char *arg)
{
  D_MOBILE *xMob;
  D_SOCKET *dsock;
  BUFFER *buf = buffer_new(MAX_BUFFER);

  bprintf(buf, " - - - - ----==== Who's Online ====---- - - - -\n\r");
  for (dsock = dsock_list; dsock; dsock = dsock->next)
  {
    if (dsock->state != STATE_PLAYING) continue;
    if ((xMob = dsock->player) == NULL) continue;

    bprintf(buf, " %-12s   %s\n\r", xMob->name, dsock->hostname);
  }
  bprintf(buf, " - - - - ----======================---- - - - -\n\r");
  text_to_mobile(dMob, buf->data);
  buffer_free(buf);
}


What makes this work, and not the first one?

Zeno McDohl,
Owner of Bleached InuYasha Galaxy
http://www.biyg.org
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #3 on Sat 06 Dec 2003 07:13 PM (UTC)
Message
Well, before this line:
if (dsock->state != STATE_PLAYING) continue;

You have this line:
for (dsock = dsock_list; dsock; dsock = dsock->next)

Which "initializes" the variable dsock to dsock_list.

Imagine the following code:
int i;
printf("Hello! My number is %d.", i);


What would you expect this to do? What value would i have? It's the same problem with pointers, and arguably even more so. Before you access a what the pointer points to, it has to point to something real.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Zeno   USA  (2,871 posts)  Bio
Date Reply #4 on Sat 06 Dec 2003 07:37 PM (UTC)
Message
Oh, no wonder. I am thinking that dsock is defined as the person who typed the command... I have to look up how to make it define that then...

Zeno McDohl,
Owner of Bleached InuYasha Galaxy
http://www.biyg.org
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


11,215 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.