I am writing an executable for an embedded device (an STM32) in C. After some debugging, I have reduced it to this function:
char * parse(char * start)
{
int i = 0;
char command[20];
print(start);
}
For some reason, when I call this function, the argument start is corrupted. Now I can get it to work if I comment out the command initialisation:
char * parse(char * start)
{
int i = 0;
// char command[20];
print(start);
}
With command commented out, it all works fine. I was thing that maybe I might be running out of RAM. But this is program is tiny, and upon checking the stack pointer register, I can confirm that I have a lot of RAM space left.
What could be going wrong here? A broken compiler? (I’m using a recompiled version of GCC for the ARM called Yagarto.)
More than likely you’re in the area of undefined behaviour because you’ve done something wrong elsewhere in your program. The fact that it works under some circumstances in no way makes undefined behaviour acceptable 🙂
Possibly, you’ve overwritten memory or your string is not null terminated, or any of a hundred other reasons.