I was testing the code below on my STM32 board, but I could't understand how the array was being allocated in the stack.
I'm assuming that there are 200 bytes of memory in STM32.
Here is the code:
void test1()
{
char data[1000]={0};
}
void test2()
{
char data[1000];
data[999] = 50;
lcdAdjustBacklight(data[999]);//just adjust back light
}
So, when I add test1()
into my main()
function, the program will crash when run on the MCU. Obviously, the stack is overflow. However, when I add test2()
into main()
, the MCU processes the program just fine. I wonder how the stack allocation works for an uninitialized array.
Edit: I finally figured it out. I checked the assembly code, in test1
the compiler allocated the memory, indeed. But in test2
, the compiler optimized the array, so it does not allocated memory for it.