Multiple choice technology security

What value is stored in *leaf in the program given below?

int *myfunc(int tree) {   
    int *node;   
    int i=rand();   
    if(0==tree/pow(2,i))       
        node=&tree;   
    else       
        node=&i;   
    return node; 
}  

int main(int argc, char * argv[]) {   
    int *leaf;   
    leaf=myfunc(7); 
}

  1. value of tree

  2. value of node

  3. value of i

  4. garbage-- its a dangling pointer

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

myfunc returns a pointer to a local variable (tree or i). Both tree and i are local to myfunc and go out of scope when the function returns. The pointer in main becomes a dangling pointer pointing to freed stack memory. Dereferencing *leaf would access garbage values.

AI explanation

Both branches of myfunc return the address of a variable that is local to the function call — either the parameter tree or the local variable i — and both go out of scope the instant the function returns. The returned pointer therefore points to a stack location that's no longer valid, making *leaf a dangling pointer whose dereferenced value is unpredictable garbage.