Pointer doubt

S

Sachin

int *p, *q, temp=2;
p = (int*) malloc (sizeof(int));
q = p;
*p = temp;
free (p);
printf ("%d",*q);

In the above code, what will be the output and why? And also explain
what exactly happens to 'p' and 'q' once 'p' is freed.

Thanks in advance
Sachin
 
G

Giorgos Keramidas

int *p, *q, temp=2;
p = (int*) malloc (sizeof(int));
q = p;
*p = temp;
free (p);
printf ("%d",*q);

In the above code, what will be the output and why?

Demons will fly out of your nose for having invoked undefined behavior :p
And also explain what exactly happens to 'p' and 'q' once 'p' is
freed.

They are both undefined.
 
I

infobahn

Sachin said:
int *p, *q, temp=2;
p = (int*) malloc (sizeof(int));

Better: p = malloc(sizeof *p); /* but WHY malloc a single int? */

The reason it's better to drop the cast is that it can hide the
q = p;
*p = temp;

Oops. You used *p without checking whether p is NULL. malloc /can/ fail.
free (p);
printf ("%d",*q);

In the above code, what will be the output and why?

The output, if any, is undefined, as is the behaviour of the program
from this point on.
And also explain
what exactly happens to 'p' and 'q' once 'p' is freed.

Once p is freed, its value (which q shares, because of q = p;) is
indeterminate. It might have the same bit pattern as before, but that
is irrelevant. The *meaning* of p has changed from "points to a block
to which the program has permission to use" to "does not point to
anywhere special, and certainly not to a block that the program has
permission to use". Because q = p, the meaning of q has changed too.

Using a block after you freed it invokes consequences that the C
language definition shies away from; all bets are off, and there
is no way to predict correctly what this program will do. You
might take a guess, and you might even be right, but you cannot
*know* what the program will do (from the language definition
perspective), because it has become meaningless.
 
L

Lawrence Kirby

int *p, *q, temp=2;
p = (int*) malloc (sizeof(int));
q = p;
*p = temp;
free (p);
printf ("%d",*q);

In the above code, what will be the output and why? And also explain
what exactly happens to 'p' and 'q' once 'p' is freed.

The first thing to remember is that it isn't p that is being freed, it is
the object it points to. Once freed that object ceases to exist and any
pointer to that object becomes indeterminate - it is an error to access
the value of such a pointer. So after free(p) both p and q are
indeterminate pointers and the evaluation of q in the printf() call
invokes undefined behaviour. Other problem with the code have been covered
elsewhere.

Lawrence
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top