Stack pointer gets corrupted

At the beginning of main I have two variables, namely:

unsigned char audioDataBuffer1[512];
unsigned char audioDataBuffer2[512];

Later in the code I use them as buffers for audio data from SD Card.

The problem is that the stack pointer is set too low and these big variables overwrite my stack pointer which is set automatically by Keil startup code. How is this possible?

I have noticed that the problem disappears when I set above variables to global, or when I use malloc instead.

I would appreciate if you could explain what goes wrong here and is there any reason I should not declare large variables like this?

Thank you.

Parents
  • Most always, it cleans up and calls main again.

    I will avoid saying that what you say is just plain wrong. It is very unlikely.

    You, as the creator of the system, should do something appropriate. Like fall through to code that triggers a watchdog reset.

    Unfortunately, from what I've seen, this is a very rare piece of forethought. Many professional and seasoned firmware designers neglect such details.

Reply
  • Most always, it cleans up and calls main again.

    I will avoid saying that what you say is just plain wrong. It is very unlikely.

    You, as the creator of the system, should do something appropriate. Like fall through to code that triggers a watchdog reset.

    Unfortunately, from what I've seen, this is a very rare piece of forethought. Many professional and seasoned firmware designers neglect such details.

Children
More questions in this forum