where are static buffers allocated?

2019-06-24 05:34发布

问题:

lets say I have a file test.c that contains:

char buffer1[1024];

int somefunction()
{
      char buffer2[1024];
      // do stuff
}

now I know buffer2 is allocated on the stack on the frame belonging to somefunction calls, but where is buffer1 allocated ?

回答1:

These variables are typically on BSS (variables which don't have explicit initialization in the source code, so they get the value 0 by default) or data segment (initialized datas). Here, buffer1 is uinitialized, so it will probably be allocated on BSS segment, which starts at the end of the data segment.

From bravegnu website:



回答2:

buffer1 has memory reserved in the static(bss/data) memory section of the program. That's where all statics and globals exist.

It is a third memory segment, like the stack and the heap.



回答3:

An array declared statically would have different storage specification from an array declared locally. As you said, a local array buffer2 will be (usually)created on stack while a static array buffer1 will be (usually)created on ./bss or ./data segments.



标签: c static