Jump to content
  • Advertisement
Sign in to follow this  
Steno

Quick C++ question on scope resolution

This topic is 2786 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

If I write:

[code lang=cpp]
void main() { // main scope
int j = 0;
{ // new scope
int j = 1; // does this get pushed to the stack?
// do stuff
}

return 0;
} // end main scope


I'm guessing the inner scope gets pushed and popped from the stack (or heap) like any normal function call?

Share this post


Link to post
Share on other sites
Advertisement
Implementation defined. If you're curious about what your particular compiler does in a particular situation, look at the generated assembly.

Share this post


Link to post
Share on other sites
It depends on the compiler, and optimization level.
With optimization enabled, GCC may eliminate the "push" operation as much as possible and try to reuse current stack.

Why do you care about it? Hack the binary code? :rolleyes:

Share this post


Link to post
Share on other sites
Conceptually, pretty much.

As above, in practice the compiler can do anything, as long as it ends up with the same result as if it were performing those conceptual steps.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!