Re: [cc65] C64: local block variables corrupting stack

From: Greg King <greg.king41verizon.net>
Date: 2008-10-15 11:35:26
From "Dirk Jagdmann"; on Friday, September 26, 2008; at 04:40 PM -0400
> 
> I've stumbled accross an issue with local-block-scoped variables.  The
> following test program should loop indefinitely, because variable "a"
> should be 8 all the time.  However, when compiling with cc65 2.12.0,
> on the second loop, "a" is 16.
> 
> #include <conio.h>
> int main()
> {
>   unsigned char a=8;
>  jump_to_top:
>   cprintf("a should be eight: %u\r\n", a);
>   if(a==8)
>     {
>       unsigned char XXX;
>       goto jump_to_top;
>     }
>   return 0;
> }
> 
> If I remove the XXX variable from the inner block,
> or move it to the main block, the program behaves correctly.

This programming trick, which fakes a "goto", does work properly!

#include <conio.h>
int main(void)
{
  unsigned char a=8;

  for (;;)
  {
    cprintf("a should be eight: %hhu\r\n", a);
    if (a == 8)
    {
      unsigned char XXX;
      continue;    /* This takes advantage of the loop; ... */
    }
    break;    /* ... while this cancels the loop. */
  }
  return 0;
}

Use "continue" to jump backward; use "break" to jump forward.

----------------------------------------------------------------------
To unsubscribe from the list send mail to majordomo@musoftware.de with
the string "unsubscribe cc65" in the body(!) of the mail.
Received on Wed, 15 Oct 2008 05:35:26 -0400

This archive was generated by hypermail 2.1.8 : 2008-10-15 19:53:47 CEST