473,396 Members | 2,034 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Pointer doubt

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
Nov 14 '05 #1
3 1031
sa********@gmail.com (Sachin) writes:
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.

Nov 14 '05 #2
Sachin wrote:

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
bug of failing to #include <stdlib.h> to get a prototype for
malloc.
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.
Nov 14 '05 #3
On Tue, 22 Feb 2005 23:10:17 -0800, Sachin wrote:
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
Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
28
by: Wonder | last post by:
Hello, I'm confused by the pointer definition such as int *(p); It seems if the parenthesis close p, it defines only 3 integers. The star is just useless. It can be showed by my program: ...
8
by: toton | last post by:
HI, One more small doubt from today's mail. I have certain function which returns a pointer (sometimes a const pointer from a const member function). And certain member function needs reference...
2
by: toton | last post by:
Hi, This is continuation of topic pointer & reference doubt. http://groups.google.com/group/comp.lang.c++/browse_thread/thread/df84ce6b9af561f9/76304d7d77f6ccca?lnk=raot#76304d7d77f6ccca But I...
9
by: junky_fellow | last post by:
Hi, To print the pointer using printf(), we convert it to (void *) . printf("%p",(void *)ptr); My question is how printf() determine which type of pointer is passed to it and prints its value...
6
by: reji_thomas | last post by:
Hi, I have a doubt in the following code: struct xyz { int x; long l; float f; };
156
by: Lame Duck | last post by:
Hi Group! I have a vector<floatvariable that I need to pass to a function, but the function takes a float * arguement. That's OK, I can convert by doing &MyVector.front(), but when I get back a...
30
by: somenath | last post by:
Hi All, I have one doubt regarding pointer .I have one small code as mentioned bellow . #include<stdio.h> #include<stdlib.h> int main (void) { int *p; int *ptr= malloc(sizeof(int *));
8
by: somenath | last post by:
Hi All, I have a doubt regarding the pointer assignment . Please have a look at the following program . #include<stdio.h> #include<stdlib.h> #define NAMESIZE 10 #define SAFE_FREE(t) if(t)\...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.