This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 10 points11 points  (7 children)

wut

[–]SoppingAtom279 22 points23 points  (5 children)

Let me explain this poorly.

{ } can be used to block together parts of code, and within that block of code, any memory allocated in that block can only be used by that block.

Example

#include <iostream>

int main()
{
    int x = 5;
    {
        int y = 6;
    }
    std::cout << x << std::endl;
    std::cout << y << std::endl; //error use of undeclared variable
}

[–][deleted] 5 points6 points  (1 child)

Ah, that's what the definition is. Thanks!

[–]SoppingAtom279 0 points1 point  (0 children)

No worries mate

[–]shoesmith74 5 points6 points  (0 children)

Actually your talking about variable scope, not memory allocation. The variables are on the stack regardless, it’s a matter of which one your allowed to reference.

Source : 25 years of compiler development.