🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.sys.ibm.pc.misc
Article #2391

Re: Turbo C variable changing?

#2391
From: Pete Halverson
Date: Wed, 15 Aug 1990 14:52
37 lines
1735 bytes
In article <90227.090858JJL101@psuvm.psu.edu> J.J. Lehett writes:
>
>      I have integer variables declared as index, and temp[2],
>
>    well when I start into a loop such as :
>
>      for (index = 1; index <= 2; index++)
>        { temp[index] =  u[index] + v[index];
>        }
>
>      that the statement line also changes the value of index.

Almost certainly caused by the out-of-bounds array reference:

   1) arrays in C always begin at element 0

   2) temp is defined to be 2 elements long

   3) the iteration runs from 1 to 2 (should be from 0 to 1)

   4) assigning something to temp[2] will cause strange behavior, in this
      case modifying the value of index due (most likely) to the way the
      compiler arranged the variables on the stack.

Changing this to

  for (index = 0; index < 2; index++)
  {
     temp[index] =  u[index] + v[index];
  }

should eliminate the problem.
--
===============================================================================
Pete Halverson                        		    INET: halverson@crd.ge.com 
GE Corporate R&D Center	                      UUCP: uunet!crd.ge.com!halverson
Schenectady, NY                     "Money for nuthin' and your MIPS for free"
Technical Details
Message-ID: <11050@crdgw1.crd.ge.com>
Path: arc1-ca.pugleaf.net!utzoo!attcan!uunet!crdgw1!altair!halvers
References: <90227.090858JJL101@psuvm.psu.edu>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!uunet!crdgw1!altair!halvers
From: halvers@altair.crd.ge.com (Pete Halverson)
Newsgroups: comp.sys.ibm.pc.misc,comp.lang.c
Subject: Re: Turbo C variable changing?
Message-ID: <11050@crdgw1.crd.ge.com>
Date: 15 Aug 90 14:52:42 GMT
References: <90227.090858JJL101@psuvm.psu.edu>
Sender: news@crdgw1.crd.ge.com
Reply-To: halverson@crd.ge.com (Pete Halverson)
Followup-To: comp.sys.ibm.pc.misc
Organization: General Electric Corporate R&D Center
Lines: 37