Path: utzoo!utgpu!attcan!lsuc!ncrcan!ziebmef!ross From: ross@ziebmef.uucp (Ross Ridge) Newsgroups: comp.sys.cbm Subject: Re: C Language for Commodore 64/128 Summary: zero-page variables Keywords: Looking for C compiler Message-ID: <1988Oct8.021541.10093@ziebmef.uucp> Date: 8 Oct 88 06:15:40 GMT References: <1520@nunki.usc.edu> <1825@uokmax.UUCP> <6135@watcgl.waterloo.edu> Reply-To: ross@watcsc.uucp (Ross Ridge) Organization: Ziebmef Public Access BBS/Unix Lines: 62 In article <6135@watcgl.waterloo.edu> bphollander@watcgl.waterloo.edu (Brian Hollander) writes: >When a new function is invoked it clears out however many bytes of >zero page it needs by pushing the old stuff to the C stack, not the CPU >stack. If you pass the address of a zero page stored variable to one >of YOUR functions (library routines do not clear out that part of zero >page) and that function uses enough zero page variables to push the >old one to the stack, then the address is no longer valid. It still >points to the zero page address where it used to be. > >This example should demonstrate the problem. > >main() >{ > int value; > > value = 5; > foo(&value); > printf("value = %d\n",value); >} > >foo(value) >int *value; >{ > int var1,var2,var3; /* a bunch of locals only one is needed here */ > > *value = 20; >} > >When this is run value should be 20 but it will be 5. > >I have not tested this specific example but others that are similar. This >means that this example will work fine on someone else's computer because: >1 - They are using a version where this bug has been fixed. >2 - My C64 was having a bad day (occurs very often) >3 - I'm seeing things. This realy isn't so much a bug as an undocumentated feature. C Power (all versions) has in effect a new storage class (which I like to call) "zero-page". By default the first 32 bytes of (what would be automatic) variables in a function have this class. "Zero-page" storage-class is sort of cross between register and automatic storage classes. If you think of these varibles this way, you can go along way in avoiding problems >If anyone else comes across useful information (undocumented functions, >hard to find bugs) PLEASE post it to the net. It may save me some long >sleepless nights! Two solutions to the above problem: Declare the varible explicitly as automatic (ie. auto int a;) Compile with "cc -a file.c" [ Ignore the below signature, mail any replies to: ross@watcsc !watmath!watcsc!ross ] -- l/ uunet!attcan!lsuc!ncrcan!ziebmef!ross // [OO] or just call me Ross for short... [oo] -()- -()- db 6502 assembly forever! //