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

The function myfunc() returns a pointer to either 'tree' or 'i', both of which are local variables to that function. When myfunc() returns, those local variables go out of scope and their memory is freed. The pointer stored in 'leaf' becomes a dangling pointer pointing to garbage memory. Dereferencing *leaf in main() accesses invalid memory. Option D is correct - it's a dangling pointer containing garbage.

AI explanation

myfunc returns the address of a local variable (tree, a parameter, or i, a local), both of which live on the stack frame of myfunc. Once the function returns, that stack frame is popped and reused, so the memory *leaf points to is no longer valid — it's a dangling pointer. Reading through leaf afterward yields undefined/garbage behavior rather than a reliable value of tree, node, or i.