Re: Help with code please 98 00:49:18 am"

From: Christian Loth (sg618lo@UNIDUI.UNI-DUISBURG.DE)
Date: 09/02/98


Hello,

Okay, lets see...

> I have added in variables to mobs and players for number of attacks (no
> problems there).
> In perform_violence I can not figure out why I get this error:
>
> fight.c: In function `perform_violence':
> fight.c:927: warning: `ch' might be used uninitialized in this function
>
> If I remove the two if statements, the code works fine. The rest of the
> code references (ch) in several places so I don't quite understand what
> is wrong.
> Can someone point out what I am missing? or what I am doing wrong..
>
>
> void perform_violence(void)
> {
>   struct char_data *ch;  /* this is line 927 */
>   int attacks = 1, i;
>
>   if (IS_NPC(ch) && (GET_MOB_ATTACKS(ch) > 0))
>     attacks = GET_MOB_ATTACKS(ch);

ch is pointing at memory garbage here and will most likely cause
a segfault (or whatever depending on OS). You need to let ch
point at something sensible; either give the char you want to check
here as parameter, or first go through the character-list, or
whatever. chs don't come out of nowhere, you know ;)

>
>   if (!IS_NPC(ch) && (GET_PC_ATTACKS(ch) > 0))
>     attacks = GET_PC_ATTACKS(ch);
>
>   for (ch = combat_list; ch; ch = next_combat_list) {
>     next_combat_list = ch->next_fighting;

AHAAA!! Here it gets initalised for the first time! By
first letting it point at the beginning of the combat_list
and then letting it 'advance' through the list.

>
>    for (i = 0; i < attacks; i++) {
>     if (FIGHTING(ch) == NULL || ch->in_room != FIGHTING(ch)->in_room) {
>

- Chris

--
/----------------------------------------------------------------------------\
| Christian Loth                      | Meet me as the >Shadowdancer< in the |
| chris@rom.mud.de                    |           REALM OF MAGIC             |
| http://rom.mud.de/~chris            | telnet rom.mud.de 4000               |
|                                     | http://rom.mud.de/                   |
|               "The road goes ever on and on!" - Bilbo Baggins              |
\----------------------------------------------------------------------------/


     +------------------------------------------------------------+
     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     | http://democracy.queensu.ca/~fletcher/Circle/list-faq.html |
     +------------------------------------------------------------+



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