Stack frame structure for function with sub-area

Below is the code that I used as a reference to understand how the area of ​​a sub-item (or) of a fictitious area (just {} ) present in a function affects the structure of the stack frame.

 #include <stdio.h> int main() { int main_scope=0; /*Scope and life time of the variable is throughout main*/ { //From below two statements I assume that there //is no seperate "stack frame" created for just //braces {}.So we are free access (scope exists) and //modify (lifetime exists) the variable "main_scope" //anywhere within main main_scope++; printf("main_scope++:(%d)\n",main_scope); //I expected this statement to throw an error saying //"Multiple definition for "main_scope".But it isn't???? int main_scope=2; printf("Value of redefined main_scope:(%d)\n",main_scope); } printf("Finally main_scope in %s:(%d)\n",__FUNCTION__,main_scope); return 0; } 

Output example

 main_scope++:(1) Value of redefined main_scope:(2) Finally main_scope in main:(1) 

Based on the above behavior, I assume the following.

  • For area {} there is no stack stack structure.
  • Thus, the auto variables declared / defined in main , and those in subparagraph {} , have the same frame stack.
  • Thus, variables declared / defined in main can be accessed anywhere in the function (even within the scope).
  • On the other hand, variables declared / defined in a subclause lose their scope outside the block. But its lifespan is valid as long as the stack stack is present.

Question: If the above points are correct, then why does the code not work when providing multiple definitions of the same variable, one inside main and the other in {} .

+4
source share
2 answers

The hardware stack does not matter here. It can grow only once for all local variables when entering the function and shrink only once when the function exits, or it can grow and shrink every time a new local variable is defined and destroyed when the nested {} remains for it.

What is the relevance of the "visibility" of variables.

 int main_scope=0; { main_scope++; printf("main_scope++:(%d)\n",main_scope); // the main_scope variable that was assigned 0 is the most recent // visible declaration of main_scope. int main_scope=2; // now, the previous declaration of main_scope is obscured by the new one, // and so, you're going to access the new one printf("Value of redefined main_scope:(%d)\n",main_scope); } printf("Finally main_scope in %s:(%d)\n",__FUNCTION__,main_scope); // the previous scope inside {} is left, so, main_scope is going to be // the one, to which we assigned 0 

It is perfectly legitimate to define an object in the inner / subzone with the same name as in the outer / superspace. The latter will be obscured by the duration of {}.

For reference only, there are some other places where variable definitions may arise, for example. inside the first expression of the for(;;) operator: for (int i = 0; i < 10; i++) ... This variable i will only be visible inside the body, and you can hide it there by specifying another i .

+2
source

The local variable hides the external variable main_scope .

 int main() { int i=1; { int i=2; printf("%d", i); /* Whenever you use i here, it always the local one that used. The outer i is hidden by the local i. But the local i will be deallocated once the scope exits.*/ } } 

completely legal in C. (Note that this is illegal in C ++!)

In your example, of course, the stack frame is created for the internal main_scope , but it will be freed after the program disconnects this internal area.

+1
source

Source: https://habr.com/ru/post/1439545/


All Articles