3

Possible Duplicate:
In C, do braces act as a stack frame?

int main()
{
   int i=10;

   {
       int i=100;
       printf("%d", i);
   }
}

Will the internal "{" and "}" create a new stack frame?

Community
  • 1
  • 1
Nayan
  • 49
  • 1
  • 2
    You can try to look at [THIS][1] It's pretty clear. [1]: http://stackoverflow.com/questions/2759371/in-c-do-braces-act-as-a-stack-frame – DonCallisto Jan 19 '12 at 13:51
  • 2
    @DonCallisto FYI, in comments, you create links like this: `[link_title](http://link-url/)` - this also works in questions. – Richard J. Ross III Jan 19 '12 at 13:54

3 Answers3

6

This is totally implementation dependent, but for implementations out there, the answer is no. The two i variables will typically be implemented by two separate variables in the same stack frame, although in this particular case, the first i might be omitted altogether.

Creating a stack frame (on i386) is only needed when you call a subroutine (even if it were only for the return address). This doesn't happen in your case.

jpalecek
  • 47,058
  • 7
  • 102
  • 144
2

Only if the compiler is feeling an itch. There's certainly no language requirement involved. to be more specific, I can't think of any reason that any compiler would feel an urge to push a frame here except to optimize storage for a very large number of locals. The compiler is perfectly capable of managing the names without a runtime frame.

bmargulies
  • 97,814
  • 39
  • 186
  • 310
1

Perhaps. Perhaps not. The lanaguage does not require it, so the compiler is free to do whatever it wishes.

Raedwald
  • 46,613
  • 43
  • 151
  • 237