473,387 Members | 1,456 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,387 software developers and data experts.

using a freed pointer

Consider the following code

char *p1 = malloc(10);
char *p2 = malloc(10);

if (p1 p2)
puts("bigger");

free(p1);
if (p1 p2)
puts("bigger");

is this a case of UB on the second comparison? Since p1 does contain a valid
value even though there's no valid object there anymore.
Oct 17 '06 #1
8 1523
Serve Laurijssen wrote:
Consider the following code

char *p1 = malloc(10);
char *p2 = malloc(10);

if (p1 p2)
puts("bigger");

free(p1);
if (p1 p2)
puts("bigger");

is this a case of UB on the second comparison? Since p1 does contain a valid
value even though there's no valid object there anymore.
Yes, it is UB. As an example:

Consider a platform that has separate data and address registers. Also
consider that this platform has virtual memory, and that free() unmaps
the passed-in pointer from the address space. The mere act of loading
the p1 pointer into a register in order to compare it to another
platform could cause a bus error, crash the program, etc.
--
Clark S. Cox III
cl*******@gmail.com
Oct 17 '06 #2
Clark S. Cox III wrote:
Serve Laurijssen wrote:
>Consider the following code

char *p1 = malloc(10);
char *p2 = malloc(10);

if (p1 p2)
puts("bigger");

free(p1);
if (p1 p2)
puts("bigger");

is this a case of UB on the second comparison? Since p1 does contain a valid
value even though there's no valid object there anymore.

Yes, it is UB. As an example:

Consider a platform that has separate data and address registers. Also
consider that this platform has virtual memory, and that free() unmaps
the passed-in pointer from the address space. The mere act of loading
the p1 pointer into a register in order to compare it to another
platform could cause a bus error, crash the program, etc.
Strictly you can't compare pointers from different objects, I'm
sure someone else on here will be able to say whether p1 p2
is implementation defined or undefined even without the free,
but it is one of the two.

--
imalone
Oct 17 '06 #3
"Clark S. Cox III" wrote:
>
Serve Laurijssen wrote:
Consider the following code

char *p1 = malloc(10);
char *p2 = malloc(10);

if (p1 p2)
puts("bigger");

free(p1);
if (p1 p2)
puts("bigger");

is this a case of UB on the second comparison? Since p1 does contain a valid
value even though there's no valid object there anymore.

Yes, it is UB. As an example:

Consider a platform that has separate data and address registers. Also
consider that this platform has virtual memory, and that free() unmaps
the passed-in pointer from the address space. The mere act of loading
the p1 pointer into a register in order to compare it to another
platform could cause a bus error, crash the program, etc.
Isn't it UB even if p1 isn't freed, because p1 and p2 do not point
within the same object?

Consider the old segmented architecture of the 8088 CPU, and assume
that you are compiling in a mode that uses "far" pointers. If the
mallocs happen to return:

p1 = 0x1000:0xff00 (absolute address 0x1ff00)
and
p2 = 0x2000:0x0010 (absolute address 0x20010)

The code generated by the compiler can (correctly, as far as the
Standard is concerned) claim that "p1 p2" is true, because the
offset 0xff00 is greater than the offset 0x0010.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>

Oct 17 '06 #4
"Serve Laurijssen" <se*@n.tkwrote:
Consider the following code

char *p1 = malloc(10);
char *p2 = malloc(10);

if (p1 p2)
puts("bigger");

free(p1);
if (p1 p2)
puts("bigger");

is this a case of UB on the second comparison? Since p1 does contain a valid
value even though there's no valid object there anymore.
It's UB both times because of comparing two pointers to different
objects using a relational operator; it's also UB the second time for
the reason you mention.
I wouldn't wonder if the way some popular implementations handle this UB
(especially the first case) is by pretending that it's legal and making
up a reasonable-looking answer; I wouldn't wonder, either, if the next
implementation crashed your program with a bus error or segfault.

Richard
Oct 17 '06 #5
Kenneth Brody wrote:
"Clark S. Cox III" wrote:
>Serve Laurijssen wrote:
>>Consider the following code

char *p1 = malloc(10);
char *p2 = malloc(10);

if (p1 p2)
puts("bigger");

free(p1);
if (p1 p2)
puts("bigger");

is this a case of UB on the second comparison? Since p1 does contain a valid
value even though there's no valid object there anymore.
Yes, it is UB. As an example:

Consider a platform that has separate data and address registers. Also
consider that this platform has virtual memory, and that free() unmaps
the passed-in pointer from the address space. The mere act of loading
the p1 pointer into a register in order to compare it to another
platform could cause a bus error, crash the program, etc.

Isn't it UB even if p1 isn't freed, because p1 and p2 do not point
within the same object?
Indeed, I forgot about that issue. However, to the OP: in answer to your
question, the following is still undefined.

char *p1 = malloc(10); //Assume that malloc succeeds
char *p2 = p1 + 5;

if(p1 p2)
puts("bigger");

free(p1);

/*Even attempting to compare p1 or p2 results in UB */
if(p1 p2)
puts("bigger");

--
Clark S. Cox III
cl*******@gmail.com
Oct 17 '06 #6

Serve Laurijssen wrote:
Consider the following code

char *p1 = malloc(10);
char *p2 = malloc(10);

if (p1 p2)
puts("bigger");

free(p1);
if (p1 p2)
puts("bigger");

is this a case of UB on the second comparison? Since p1 does contain a valid
value even though there's no valid object there anymore.
Several issues:

(1) I doubt if C assigns any meaning to pointer ordering, so why
should you expect p1 p2 to mean anything even if p1 and p2 are still
allocated?

(2) After a free(p1) I can't think of anything you can do with p1
other than setting it to another value. Even comparing it for
equality to another pointer sounds really iffy, and probably useless
to boot.

Oct 17 '06 #7
"Ancient_Hacker" <gr**@comcast.netwrites:
Serve Laurijssen wrote:
>Consider the following code

char *p1 = malloc(10);
char *p2 = malloc(10);

if (p1 p2)
puts("bigger");

free(p1);
if (p1 p2)
puts("bigger");

is this a case of UB on the second comparison? Since p1 does contain a valid
value even though there's no valid object there anymore.

Several issues:

(1) I doubt if C assigns any meaning to pointer ordering, so why
should you expect p1 p2 to mean anything even if p1 and p2 are still
allocated?
Why speculate?

C certainly does assign a meaning to pointer ordering. If it didn't,
the ">" operator wouldn't be legal for pointer operands.

Relational operators ("<", "<=", ">", ">=") on pointer values are
meaningful only if both point into the same object; otherwise the
behavior is undefined. See C99 6.5.8p5 for details; Google n1124.pdf
for a copy of the standard (plus TC1 and TC2).
(2) After a free(p1) I can't think of anything you can do with p1
other than setting it to another value. Even comparing it for
equality to another pointer sounds really iffy, and probably useless
to boot.
After free(p1), the value of p1 is indeterminate; referring to that
value in any way (potentially) invokes undefined behavior. (I added
the weasel-word "potentially" because of some subtle issues involving
indeterminate values vs. trap representations vs. unspecified values;
there's been a discussion recently in comp.std.c. Bottom line: Don't
do that.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Oct 17 '06 #8
Serve Laurijssen wrote:
>
Consider the following code

char *p1 = malloc(10);
char *p2 = malloc(10);

if (p1 p2)
puts("bigger");

free(p1);
if (p1 p2)
puts("bigger");

is this a case of UB on the second comparison?
Since p1 does contain a valid
value even though there's no valid object there anymore.
p1 does not contain a valid value
because there's no valid object there anymore.

The value of a pointer to an object type, can only be either:
1 an adress of an object or one past
2 a null pointer
3 indeterminate

--
pete
Oct 18 '06 #9

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

Similar topics

30
by: jimjim | last post by:
Hello, This is a simple question for you all, I guess . int main(){ double *g= new double; *g = 9; delete g; cout<< sizeof(g)<<" "<<sizeof(double)<<" "<<sizeof(*g)<<" "<<*g<<" "<<endl; *g =...
73
by: David Scarlett | last post by:
Is the following code safe? SomeType *a, *b; /* Some code.... */ free(a);
72
by: ravi | last post by:
I have a situation where i want to free the memory pointed by a pointer, only if it is not freed already. Is there a way to know whether the memory is freed or not?
5
by: Thor W Hammer | last post by:
Hello all, Is it possible to find out if a script is freed? This is actual when having a pointer to a function and should determine if it is freed so we don't call it and get error.. TWH
7
by: ballpointpenthief | last post by:
Hello, I'm thinking that it would be a good idea to start using the return code for main() to return the amount of memory still allocated (which would hopefully be zero) to ensure all memory has...
9
by: chikkubhai | last post by:
Why is the result different for the following set of two code snippets Code without using this pointer #include <string> #include <iostream> using namespace std; struct X { private:
10
by: frakie | last post by:
Hi 'body, is there a method to check if a pointer is pointing a freed memory location?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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:
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...

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.