delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/02/17/15:40:26

Message-Id: <3.0.6.32.19990217153959.009b3100@pop.netaddress.com>
X-Sender: pderbysh AT pop DOT netaddress DOT com
X-Mailer: QUALCOMM Windows Eudora Light Version 3.0.6 (32)
Date: Wed, 17 Feb 1999 15:39:59 -0500
To: djgpp AT delorie DOT com
From: Paul Derbyshire <pderbysh AT usa DOT net>
Subject: Re: Problem with Stack Overflow
In-Reply-To: <7adai7$ji5@journal.concentric.net>
Mime-Version: 1.0
Reply-To: djgpp AT delorie DOT com

At 06:46 PM 2/16/99 PST, you 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?

IIRC, sizeof on an array returns its length in terms of sizeof(char), so
this is causing x to go from 0 all the way to 39 (as an int is 4 bytes
under DJGPP). So it's overwriting the end of the array and if it's a local
variable, smashing the stack, otherwise scrogging the heap.

-- 
   .*.  "Clouds are not spheres, mountains are not cones, coastlines are not
-()  <  circles, and bark is not smooth, nor does lightning travel in a
   `*'  straight line."    -------------------------------------------------
        -- B. Mandelbrot  |http://surf.to/pgd.net
_____________________ ____|________     Paul Derbyshire     pderbysh AT usa DOT net
Programmer & Humanist|ICQ: 10423848|

- Raw text -


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