When block area variables are selected

If I have a function c

int foo(int input)
{
    int x = 5;
    if( input == 0 ){
        int y = 6;
    } else {
        int z = 7;
    }
}

I know that the stack pointer is set up when we enter the function, and this makes space for the operator int x. And I know that ythey zexist only within their respective blocks. But when and how is space allocated to them?

+4
source share
2 answers

It is up to the compiler if space lasts, at least for the life of the variable.

, . , . , , , .

: , C ( ++) . , , .

+6
int foo(int input)
{ // BLOCK 1
    int x = 5;
    if( input == 0 )
    { // BLOCK 2
        int y = input * (x + 6);
        // other code here
    }
    else
    { // BLOCK 3
        int z = input + x;
        // other code here
    }
}

, :

x BLOCK 1 (BLOCK 2 BLOCK 3). y BLOCK 2. z BLOCK 3.

, , . , ( ), (, ), , , - .

0

All Articles