I am really curious about how alloca()
function works and therefore, I have written a simple test program as follows:
int test() {
int a = 0;
int e;
char tmp2[a]; //alloca
int d;
char* tmp3 = new char[2];
tmp2[100] = 1;
return 0;
}
int main(int argc, char** argv) {
test();
return 0;
}
According to the document, alloca()
will allocate memory in stack. I run the program using gdb and figure out that (char*)&tmp2 - (char*)a = -44
which mean there are 44 bytes between them while the distances between the address of e-a
, d-e
, tmp3-d
are 4 bytes. I really can not understand about how compiler can allocate a variable length array in stack and hope that someone can tell me what the meaning of the 44 bytes is.