delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/02/16/23:10:52

Sender: nate AT cartsys DOT com
Message-ID: <36CA40B7.CB9807E@cartsys.com>
Date: Tue, 16 Feb 1999 20:08:23 -0800
From: Nate Eldredge <nate AT cartsys DOT com>
X-Mailer: Mozilla 4.08 [en] (X11; I; Linux 2.2.1 i586)
MIME-Version: 1.0
To: djgpp AT delorie DOT com
Subject: Re: Problem with Stack Overflow
References: <7adai7$ji5 AT journal DOT concentric DOT net>
Reply-To: djgpp AT delorie DOT com

Paradox wrote:
> 
> I'm having a problem with a Stack Overflow ( General Protection Error) . I
> Symifyed my program and was told that my errors were }s. I checked over my
> code and found out that all my problems were related to one function.
> 
> void unequip_other_weapons(void) {
>   int x;
>   for(x=0;x<sizeof(PS.weapons_array); x++) {
>     if(PS.weapons_array[x] == 2) { PS.weapons_array[x] = 1; }
>   }
> }
> 
> weapons_array is declared as int weapons_array[10];
> 
> Can someone tell me what the problem is, and how I can fix it?

sizeof(weapons_array) == 40 (10 * sizeof(int), which is 4).  Iterating
to element 40 of the array is wrong.

You meant to limit x at
(sizeof(PS.weapons_array)/sizeof(PS.weapons_array[0])).  (More
change-resistant than hardcoding int.)
-- 

Nate Eldredge
nate AT cartsys DOT com

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019