c++ - What happens to statically allocated memory after its scope ends? -


void printarray() {   int a[4] = {4,3,1,5};   for(int i=0; i<4; i++)     cout<<a[i]; } 

what happens memory allocated pointer variable 'a' , 4-integer block pointed 'a' after function's call completed? memory of block , pointer variable de-allocated or create sort of memory leak?

a not static variable automatic variable, draft c99 standard section 6.2.4 storage durations of objects paragraph 4 says:

an object identifier declared no linkage , without storage-class specifier static has automatic storage duration.

in paragraphs 3 describes lifetime of static lifetime of program , in paragraph 5 says:

for such object not have variable length array type, lifetime extends entry block associated until execution of block ends in way. [...]

so in other words automatic variable it's lifetime extends it's scope, in case as scope function printarray , storage associated released after scope exited.

for c++ relevant section draft standard 3.7.3 automatic storage duration paragraph 1 says:

block-scope variables explicitly declared register or not explicitly declared static or extern have automatic storage duration. storage these entities lasts until block in created exits.


Comments