The Gateway to Computer Science Excellence
0 votes
56 views
Explain the behaviour of following code:

int main()

{

int *j=0;

{

int i=10;

j=&i;

}

printf("%d",*j);

}

 

a. output is 10. j pointed to address of i, so it was not freed.

b. output may be 10 or garbage in given execution

c. output is 10. i becomes invisible outside of its block scope, but lives as long as function scope

d. output is 0.
in Algorithms by Active (1.8k points) | 56 views

1 Answer

+2 votes
When a variable goes out of scope, it's memory location is no longer valid. Accessing it causes undefined behaviour in C.
by Veteran (431k points)

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,737 questions
57,314 answers
198,356 comments
105,081 users