Just to clarify about your "last byte" example. When you add the second variable, which pushes you over the RAM limit, you're going to fail the static compilation. compcode will yell at you and tell you that you are over memory limits; go ahead and try to allocate a 256 MB global array and see what it says.
I agree that rsg appears to have answered your question fairly correctly, though one thing I'll comment just to make you aware: when you allocate a variable in a function, this allocation happens within the stack of the current task, in the current stack frame. I realize what I'm about to say is getting a little tangential, but feel I should add some further information.
For primitives (aka, non-(C++)objects) allocation itself does not do anything; all it does is affect what offset the output assembly uses to access the "variable". This means that your original example
Code: Select all
char buff[1024];
void MyFunction(void)
{
char buffer[4096];
buffer[0] = 0;
}
void main(void)
{
MyFunction();
}
will compile and run just fine. Until someone tries to use a byte beyond 'buffer[1023]'. While you declared a 4096 byte array, this declaration did not 'allocate' any memory; it is simply stuck in the local stack frame in the 1024 byte stack. When someone goes to use a byte beyond 'buffer[1023]', you've got a stack overflow and now they're writing to someone else's memory. The compiler has no way of knowing that you will use this function in that manner and thus cannot see that this will blow up. (If you wonder why it can't determine this, refer to The Halting Problem; it's uncomputable).