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

copy to pointer memory ?

MC
Hi
I have the following piece of code.

int *p;
p = malloc(10);
int *j;
j = p;
free(p);

Is this legal ? will j still point to the same
memory that p pointed to earlier ?
Can I access that memory using j ?

I read somewhere that even though free is
called, the memory is still available.
so in my case j should still point to that
memory right ?

MC
Nov 13 '05 #1
25 9168
"MC" <ma*********@yahoo.com> wrote in
<bj**********@zcars0v6.ca.nortel.com>:
Hi
I have the following piece of code.

int *p;
p = malloc(10);
int *j;
j = p;
free(p);

Is this legal ? Yes.
will j still point to the same
memory that p pointed to earlier ? Yes, and p still points there, too.
Can I access that memory using j ? No. You are not allowed to use it, since you have already
free()'d it.

I read somewhere that even though free is
called, the memory is still available. This is not guaranteed by any means - it invokes undefined behaviour.
If you try to acces the memory, /anything/ can happen.
so in my case j should still point to that
memory right ? Right, but see above.
MC

Irrwahn
--
No sig today.
Nov 13 '05 #2
On Wed, 3 Sep 2003, MC wrote:
Hi
I have the following piece of code.

int *p;
p = malloc(10);
int *j;
j = p;
free(p);

Is this legal ? will j still point to the same
memory that p pointed to earlier ?
Can I access that memory using j ?

I read somewhere that even though free is
called, the memory is still available.
so in my case j should still point to that
memory right ?

MC


[ from the FAQ ]
--------------------------------------------------------------------
7.20: You can't use dynamically-allocated memory after you free it,
can you?

A: No. Some early documentation for malloc() stated that the
contents of freed memory were "left undisturbed," but this ill-
advised guarantee was never universal and is not required by the
C Standard.

Few programmers would use the contents of freed memory
deliberately, but it is easy to do so accidentally. Consider
the following (correct) code for freeing a singly-linked list:

struct list *listp, *nextp;
for(listp = base; listp != NULL; listp = nextp) {
nextp = listp->next;
free(listp);
}

and notice what would happen if the more-obvious loop iteration
expression listp = listp->next were used, without the temporary
nextp pointer.

References: K&R2 Sec. 7.8.5 p. 167; ISO Sec. 7.10.3; Rationale
Sec. 4.10.3.2; H&S Sec. 16.2 p. 387; CT&P Sec. 7.10 p. 95.

7.21: Why isn't a pointer null after calling free()?
How unsafe is it to use (assign, compare) a pointer value after
it's been freed?

A: When you call free(), the memory pointed to by the passed
pointer is freed, but the value of the pointer in the caller
probably remains unchanged, because C's pass-by-value semantics
mean that called functions never permanently change the values
of their arguments. (See also question 4.8.)

A pointer value which has been freed is, strictly speaking,
invalid, and *any* use of it, even if is not dereferenced, can
theoretically lead to trouble, though as a quality of
implementation issue, most implementations will probably not go
out of their way to generate exceptions for innocuous uses of
invalid pointers.

References: ISO Sec. 7.10.3; Rationale Sec. 3.2.2.3.
================================================== =================
Hope this helps,

-dj
Nov 13 '05 #3
MC wrote:

Hi
I have the following piece of code.

int *p;
p = malloc(10);
int *j;
j = p;
free(p);

Is this legal ?
In C99, yes. In C89, no: executable statements cannot
precede declarations. But a simple rearrangement makes it
legal in C89, too:

int *p;
int *j;
p = malloc(10);
j = p;
free (p);
will j still point to the same
memory that p pointed to earlier ?
No. More precisely, "It's impossible for a conforming
program to find out," meaning that any attempt to find out
invokes undefined behavior.
Can I access that memory using j ?
No. Build a house; that's the malloc() call. Write
down its address on a slip of paper called `p'. Make a
photocopy of that slip of paper and call it `j'. Give
`p' to your friendly local arsonist and have him burn
down the house at the indicated address. The fact that
you've still got `j' in your pocket doesn't mean the
burned-out house is habitable.
I read somewhere that even though free is
called, the memory is still available.
I read somewhere that the Earth is flat.
so in my case j should still point to that
memory right ?


It's impossible to tell. Try to use the memory `j'
formerly pointed to (it's been burned, remember?) and there's
no telling what might happen.

Recommended reading: Section 7 of the comp.lang.c Frequently
Asked Questions (FAQ) list

http://www.eskimo.com/~scs/C-faq/top.html

--
Er*********@sun.com
Nov 13 '05 #4
On Wed, 3 Sep 2003 12:12:07 -0700
"MC" <ma*********@yahoo.com> wrote:
int *p;
p = malloc(10);
int *j;
j = p;
free(p);

Is this legal ? will j still point to the same
memory that p pointed to earlier ?
Can I access that memory using j ?
Is it legal? No:

int *p;
p = malloc(10);
int *j;
Is illegal in C;

int *p;
int *j;
p = malloc (10);
Is legal.

Other than that, it's fine;
p is malloced, j = p so they both point to the same chunk, the chunk is freed
(note that free() frees the chunk p is pointing to, not p itself), so both j and
p are now pointing to unallocated space. Using them for anything besides
assigning them a new value may lead to undefined behaviour.
I read somewhere that even though free is
called, the memory is still available.
Sure it is. if you call malloc() again, chances are you get the same memory
again. I wouldn't design anything that counts on this though.
Seriously, once you free() a malloced chunk, dereferencing the pointer leads
to undefined behaviour. Just think of it as there being no way to get it back.
so in my case j should still point to that
memory right ?


No. j and p are pointing to the same address. The memory at that address was
freed. It's lost. Save yourself a lot of trouble in the future and accept it. If
you want to keep the memory, don't free it.
--
char*x(c,k,s)char*k,*s;{if(!k)return*s-36?x(0,0,s+1):s;if(s)if(*s)c=10+(c?(x(
c,k,0),x(c,k+=*s-c,s+1),*k):(x(*s,k,s+1),0));else c=10;printf(&x(~0,0,k)[c-~-
c+"1"[~c<-c]],c);}main(){x(0,"^[kXc6]dn_eaoh$%c","-34*1'.+(,03#;+,)/'///*");}
Nov 13 '05 #5
Eric Sosman <Er*********@sun.com> scribbled the following:
MC wrote:

Hi
I have the following piece of code.

int *p;
p = malloc(10);
int *j;
j = p;
free(p);

Is this legal ?
In C99, yes. In C89, no: executable statements cannot
precede declarations. But a simple rearrangement makes it
legal in C89, too: int *p;
int *j;
p = malloc(10);
j = p;
free (p); will j still point to the same
memory that p pointed to earlier ? No. More precisely, "It's impossible for a conforming
program to find out," meaning that any attempt to find out
invokes undefined behavior. Can I access that memory using j ?

No. Build a house; that's the malloc() call. Write
down its address on a slip of paper called `p'. Make a
photocopy of that slip of paper and call it `j'. Give
`p' to your friendly local arsonist and have him burn
down the house at the indicated address. The fact that
you've still got `j' in your pocket doesn't mean the
burned-out house is habitable.


A further comment about this...

#include <stdlib.h>
int main(void) {
char *p = malloc(100);
if (p != NULL) {
free(p);
p; /* ILLEGAL! */
p = NULL; /* entirely legal;
}
return 0;
}

This is because using the value of a freed pointer, if it's not
NULL, in any context whatsoever, is undefined behaviour. Using
the freed pointer itself to assign it a new value, though, is
legal.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"It sure is cool having money and chicks."
- Beavis and Butt-head
Nov 13 '05 #6
Pieter Droogendijk <gi*@binky.homeunix.org> writes:
I read somewhere that even though free is called, the memory is still
available.
Sure it is.


This is not guaranteed by the standard. A conforming implementation would
be free to return the memory, and even the address space, to the under-
lying operating system.

So while the memory is certainly no longer available to the application,
it might even cease to exist when `free' is called.
Seriously, once you free() a malloced chunk, dereferencing the pointer
leads to undefined behaviour.


Not only dereferencing the pointer, but *any* use of the pointer value
might lead to undefined behavior.

Martin
Nov 13 '05 #7
"Martin Dickopp" <ex*************@zero-based.org> wrote:
This is not guaranteed by the standard. A conforming
implementation would be free to return the memory, and even
the address space, to the under-lying operating system.
C99 7.20.3.2#2 "The free function causes the space pointed
to by ptr to be deallocated, that is, made available for
further allocation."

If it's given back to the operating system, there would be no
guarantee it will be "made available for further allocation."
Not only dereferencing the pointer, but *any* use of the
pointer value might lead to undefined behavior.


Can you print it with printf("%p\n", (void*)p);? Or is the
conversion to (void*) invalid for an invalid pointer value?

At the least you should be able to look at its bytes through
a pointer to unsigned char.

--
Simon.
Nov 13 '05 #8
Simon Biber wrote:

"Martin Dickopp" <ex*************@zero-based.org> wrote:
This is not guaranteed by the standard. A conforming
implementation would be free to return the memory, and even
the address space, to the under-lying operating system.
C99 7.20.3.2#2 "The free function causes the space pointed
to by ptr to be deallocated, that is, made available for
further allocation."

If it's given back to the operating system, there would be no
guarantee it will be "made available for further allocation."
Not only dereferencing the pointer, but *any* use of the
pointer value might lead to undefined behavior.


Can you print it with printf("%p\n", (void*)p);? Or is the
conversion to (void*) invalid for an invalid pointer value?


No and yes, respectively. After the free(), the pointer
value becomes indeterminate (6.2.4, paragraph 2). Note that
"becomes indeterminate" does not necessarily imply a change
of representation; the bits in the pointer object before and
after the free() may be identical. The value is indeterminate
anyhow -- the example often given is of an architecture whose
pointers include a tag of some sort referencing a piece of the
hardware environment; change that hardware environment as part
of the free() operation, and the tag bits that used to mean
something now mean nonsense.
At the least you should be able to look at its bytes through
a pointer to unsigned char.


Yes, this is fine. There's no contradiction, because byte-
by-byte inspection makes no use of the pointer value, just as
byte-by-byte inspection of floating-point object that happens
to contain a signalling NaN makes no use of the f-p value.
Byte-by-byte inspection looks at the representation of an
object, but not at its value.

--
Er*********@sun.com
Nov 13 '05 #9
[Note to comp.std.c readers: This is a discussion in comp.lang.c about
whether a conforming implementation may return memory deallocated with
`free' to an underlying operating system. I'm cross-posting as the
discussion now touches a question of how to interpret the standard
and/or whether there is a defect in the standard.]

"Simon Biber" <sb****@optushome.com.au> writes:
"Martin Dickopp" <ex*************@zero-based.org> wrote:
This is not guaranteed by the standard. A conforming
implementation would be free to return the memory, and even
the address space, to the under-lying operating system.
C99 7.20.3.2#2 "The free function causes the space pointed
to by ptr to be deallocated, that is, made available for
further allocation."

If it's given back to the operating system, there would be no
guarantee it will be "made available for further allocation."


I think this does not necessarily mean "made available for further
allocation by the currently running process."

If it does, I would call that a defect. Think of a program which runs in
a multi-process environment and requires a large amount of memory for a
short time. I would certainly hope that it is not the intention of the
standard committee that a conforming implemention is /forbitten/ from
making the memory available to other processes after it is deallocated.
Not only dereferencing the pointer, but *any* use of the
pointer value might lead to undefined behavior.


Can you print it with printf("%p\n", (void*)p);?


No. According to 6.2.4#2, "[t]he value of a pointer becomes indeterminate
when the object it points to reaches the end of its lifetime." 3.17.2
defines an "indeterminate value" as "either an unspecified value or a trap
representation", and 6.2.6.1#5 explains that if a trap representation "is
read by an lvalue expression that does not have character type, the
behavior is undefined."
At the least you should be able to look at its bytes through a pointer
to unsigned char.


Yes, that doesn't access the value, but the representation of the pointer.
You can always access the representation of an object.

Martin
Nov 13 '05 #10
Eric Sosman wrote:
No. Build a house; that's the malloc() call. Write
down its address on a slip of paper called `p'. Make a
photocopy of that slip of paper and call it `j'. Give
`p' to your friendly local arsonist and have him burn
down the house at the indicated address. The fact that
you've still got `j' in your pocket doesn't mean the
burned-out house is habitable.

Better analogy. You were renting the house, then gave up the lease. Now,
maybe somebody else lives there, maybe it's a crack house and you get
shot walking up to the door, maybe it got leveled for a new mall, maybe
it's empty and it SEEMS like you can use like old times, then the new
owners come home . . .


Brian Rodenborn
Nov 13 '05 #11
Irrwahn Grausewitz wrote:

"MC" <ma*********@yahoo.com> wrote in
<bj**********@zcars0v6.ca.nortel.com>:
Hi
I have the following piece of code.

int *p;
p = malloc(10);
int *j;
j = p;
free(p);

Is this legal ?

Yes.
will j still point to the same
memory that p pointed to earlier ?

Yes, and p still points there, too.


The value of p is indeterminate.
You can't say anything else about the value of p
at that point in code.

--
pete
Nov 13 '05 #12
Martin Dickopp wrote:
[Note to comp.std.c readers: This is a discussion in comp.lang.c about
whether a conforming implementation may return memory deallocated with
`free' to an underlying operating system. I'm cross-posting as the
discussion now touches a question of how to interpret the standard
and/or whether there is a defect in the standard.]


Actually the committee has already discussed this question.
The returned addresses are in principle available for later
reallocation, but there is of course no requirement that
the program will ever see that region again. It *might*
see it. The reason this is mentioned at all is merely as a
warning that the same pointer values might be obtained from
separate allocations with nonoverlapping lifetimes; but a
strictly conforming program can detect that only by
examining the pointer representations (e.g. as array of
unsigned char), not their values (since the earlier value
has become indeterminate as a result of free()ing).

Nov 13 '05 #13
"Douglas A. Gwyn" <DA****@null.net> writes:
Martin Dickopp wrote:
[Note to comp.std.c readers: This is a discussion in comp.lang.c about
whether a conforming implementation may return memory deallocated with
`free' to an underlying operating system. I'm cross-posting as the
discussion now touches a question of how to interpret the standard
and/or whether there is a defect in the standard.]


Actually the committee has already discussed this question.
The returned addresses are in principle available for later
reallocation, but there is of course no requirement that
the program will ever see that region again. It *might*
see it.


Thank you for the clarification.

Martin
Nov 13 '05 #14
On 2003-09-04, pete <pf*****@mindspring.com> wrote:
Irrwahn Grausewitz wrote:

"MC" <ma*********@yahoo.com> wrote in
<bj**********@zcars0v6.ca.nortel.com>:
>Hi
> I have the following piece of code.
>
> int *p;
> p = malloc(10);
> int *j;
> j = p;
> free(p);
>
> Is this legal ?

Yes.
> will j still point to the same
> memory that p pointed to earlier ?

Yes, and p still points there, too.


The value of p is indeterminate.
You can't say anything else about the value of p
at that point in code.


Are you sure? :) p won't change since it is passed by value into
function and the function can't modify p. So p stays exactly the same as
before call to 'free' :) *p might change of course.

--
Andrei
Nov 13 '05 #15
Andrei Voropaev <av****@mail.ru> writes:
On 2003-09-04, pete <pf*****@mindspring.com> wrote:
Irrwahn Grausewitz wrote:

"MC" <ma*********@yahoo.com> wrote in
<bj**********@zcars0v6.ca.nortel.com>:

>Hi
> I have the following piece of code.
>
> int *p;
> p = malloc(10);
> int *j;
> j = p;
> free(p);
>
> Is this legal ?
Yes.

> will j still point to the same
> memory that p pointed to earlier ?
Yes, and p still points there, too.
The value of p is indeterminate.
You can't say anything else about the value of p
at that point in code.


Are you sure? :)


I'm sure that Pete is sure. :)
p won't change since it is passed by value into function and the
function can't modify p. So p stays exactly the same as before call to
'free' :)
The value of `p' doesn't have to change to become indeterminate. See
6.2.4#2: "[...] The value of a pointer becomes indeterminate when the
object it points to reaches the end of its lifetime."
*p might change of course.


A strictly conforming program cannot examine the value `p'. Specifically,
`*p' is not a valid expression. As undefined behavior goes, it /might/
just yield a changed value, but equally well something completely
different could happen.

Martin
Nov 13 '05 #16


Eric Sosman wrote:

Can I access that memory using j ?
No. Build a house; that's the malloc() call. Write
down its address on a slip of paper called `p'. Make a
photocopy of that slip of paper and call it `j'. Give
`p' to your friendly local arsonist and have him burn
down the house at the indicated address. The fact that
you've still got `j' in your pocket doesn't mean the
burned-out house is habitable.


I like that analogy ...
I read somewhere that even though free is
called, the memory is still available.


I read somewhere that the Earth is flat.


.... but here you are wrong. The earth is a sphere
and we live on the inner side of the sphere.
Proof: look at your shoes. The tip and the heel
point upwards as expected for an object having
contact with the inner side of a sphere. If we
would live on the outer side, they would point
downward :-) :-)

--
Karl Heinz Buchegger
kb******@gascad.at
Nov 13 '05 #17
Pieter Droogendijk <gi*@binky.homeunix.org> wrote in message news:<20030903214417.6339ed90.gi*@binky.homeunix.o rg>...
....
int *p;
p = malloc(10);
int *j;
Is illegal in C;


Not under the current C standard. See section 6.8.2:

"Syntax
1 compound-statement:
{ block-item-listopt }

block-item-list:
block-item
block-item-list block-item

block-item:
declaration
statement
"

The restriction that all declarations in a given block had to preceed
all of the statements in that block was removed in C99.
Nov 13 '05 #18
"pete" <pf*****@mindspring.com> wrote:
Irrwahn Grausewitz wrote:
"MC" <ma*********@yahoo.com> wrote:
Will j still point to the same memory that p
pointed to earlier?


Yes, and p still points there, too.


The value of p is indeterminate.
You can't say anything else about the value of p
at that point in code.


#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(void)
{
int i;
void *p = malloc(1);
unsigned char initial[sizeof p], final[sizeof p];

memcpy(initial, &p, sizeof p);
free(p);
memcpy(final, &p, sizeof p);
for(i = 0; i < sizeof p; i++)
if(initial[i] != final[i])
printf("The value changed\n");
return 0;
}

1. Does this program have undefined behaviour?
2. Can it ever output "The value changed\n"?

--
Simon.
Nov 13 '05 #19
Simon Biber wrote:

"pete" <pf*****@mindspring.com> wrote:
Irrwahn Grausewitz wrote:
"MC" <ma*********@yahoo.com> wrote:
> Will j still point to the same memory that p
> pointed to earlier?

Yes, and p still points there, too.


The value of p is indeterminate.
You can't say anything else about the value of p
at that point in code.


#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(void)
{
int i;
void *p = malloc(1);
unsigned char initial[sizeof p], final[sizeof p];

memcpy(initial, &p, sizeof p);
free(p);
memcpy(final, &p, sizeof p);
for(i = 0; i < sizeof p; i++)
if(initial[i] != final[i])
printf("The value changed\n");
return 0;
}

1. Does this program have undefined behaviour?
2. Can it ever output "The value changed\n"?


The value of an object is determined not only by the bit pattern,
but also by the type of the identifier used to access it.
It was pointer value, wasn't it ?
#include <stdio.h>

int main(void)
{
signed char object = -1;

if (*(signed char*)&object != *(unsigned char*)&object) {
puts("Is the value of object unequal to the value of object?");
}
return 0;
}
Inequality of bit patterns,
doesn't even imply inequality for pointers.
Despite your crafted string, all that your program does,
is determine whether the bit pattern has changed.

--
pete
Nov 13 '05 #20
Simon Biber <sb****@optushome.com.au> wrote:
"pete" <pf*****@mindspring.com> wrote:
Irrwahn Grausewitz wrote:
> "MC" <ma*********@yahoo.com> wrote:
> > Will j still point to the same memory that p
> > pointed to earlier?
>
> Yes, and p still points there, too.
The value of p is indeterminate.
You can't say anything else about the value of p
at that point in code.


#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(void)
{
int i;
void *p = malloc(1);
unsigned char initial[sizeof p], final[sizeof p];

memcpy(initial, &p, sizeof p);
free(p);
memcpy(final, &p, sizeof p);
for(i = 0; i < sizeof p; i++)
if(initial[i] != final[i])
printf("The value changed\n");
return 0;
}

1. Does this program have undefined behaviour?


No.
2. Can it ever output "The value changed\n"?


Much verbiage has been expended over this, with no satisfactory
resolution.

It comes down to whether you believe that being able to access the
representation of an object as an array of unsigned chars means that
the bytes that make up an object are themselves fully-fledged unsigned
char objects - that is, declaring "void *p" is a way of declaring an
array of sizeof p unsigned char objects.

- Kevin.

Nov 13 '05 #21
On Sun, 7 Sep 2003 14:18:23 UTC, Kevin Easton
<kevin@-nospam-pcug.org.au> wrote:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(void)
{
int i;
void *p = malloc(1);
unsigned char initial[sizeof p], final[sizeof p];

memcpy(initial, &p, sizeof p);
free(p);
memcpy(final, &p, sizeof p);
for(i = 0; i < sizeof p; i++)
if(initial[i] != final[i])
printf("The value changed\n");
return 0;
}

1. Does this program have undefined behaviour?


No.


Wrong. You can't use a pointer after free()ing it. This is undefined
behavior.

Using the address of a pointer don't make undefined behavior and the
compiler should give a diagnostic.

2. Can it ever output "The value changed\n"?


May be, may be not. As there is undefined behavior it can - it can do
anything else.
--
Tschau/Bye
Herbert

eComStation 1.1 Deutsch Beta ist verügbar
Nov 13 '05 #22
"The Real OS/2 Guy" <os****@pc-rosenau.de> wrote:
On Sun, 7 Sep 2003 14:18:23 UTC, Kevin Easton
<kevin@-nospam-pcug.org.au> wrote:
> #include <stdio.h>
> #include <string.h>
> #include <stdlib.h>
>
> int main(void)
> {
> int i;
> void *p = malloc(1);
> unsigned char initial[sizeof p], final[sizeof p];
>
> memcpy(initial, &p, sizeof p);
> free(p);
> memcpy(final, &p, sizeof p);
> for(i = 0; i < sizeof p; i++)
> if(initial[i] != final[i])
> printf("The value changed\n");
> return 0;
> }
>
> 1. Does this program have undefined behaviour?
No.


Wrong. You can't use a pointer after free()ing it. This is undefined
behavior.

Wrong. The pointer is not used. Well, the address of a pointer
variable is calculated, which is obviously well defined. The sizeof
operator is evaluated at translation time.

Consider:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int i, *p, **pp;
size_t sz;

p = malloc( sizeof (int) ); /* Error checking omitted! */
sz = sizeof p; /* No UB */
sz = sizeof *p; /* No UB */
pp = &p; /* No UB */
i = *p; /* UB!!! */
i = **pp; /* UB!!! */
*p = **pp; /* UB!!! */

return 0;
}

Using the address of a pointer don't make undefined behavior and the
compiler should give a diagnostic. Well, a compiler /may/ complain about virtually everything, but why
/should/ it, in this case?
> 2. Can it ever output "The value changed\n"?


May be, may be not. As there is undefined behavior it can - it can do
anything else.


Maybe, maybe not. But it's not a question of UB as there is no UB.
--
Close your eyes and press escape three times.
Nov 13 '05 #23
Irrwahn Grausewitz <ir*****@freenet.de> wrote:
<SNIP>
size_t sz;

p = malloc( sizeof (int) ); /* Error checking omitted! */ Hehehehe, me dumba, I forgot to add:

free( p );
sz = sizeof p; /* No UB */

<SNIP>
--
I can't see it from here, but it looks good to me.
Nov 13 '05 #24
Irrwahn Grausewitz <ir*****@freenet.de> writes:
"The Real OS/2 Guy" <os****@pc-rosenau.de> wrote:
On Sun, 7 Sep 2003 14:18:23 UTC, Kevin Easton
<kevin@-nospam-pcug.org.au> wrote:
> #include <stdio.h>
> #include <string.h>
> #include <stdlib.h>
>
> int main(void)
> {
> int i;
> void *p = malloc(1);
> unsigned char initial[sizeof p], final[sizeof p];
>
> memcpy(initial, &p, sizeof p);
> free(p);
> memcpy(final, &p, sizeof p);
> for(i = 0; i < sizeof p; i++)
> if(initial[i] != final[i])
> printf("The value changed\n");
> return 0;
> }
>
> 1. Does this program have undefined behaviour?

No.


Wrong. You can't use a pointer after free()ing it. This is undefined
behavior.

Wrong. The pointer is not used.


The /value/ of the pointer is not accessed, the /representation/ is.
That's okay; it's always allowed to access the representation of an
object, even if the value of the object is a trap representation.

As to whether the program can ever print "The value changed\n": A similar
question is asked in Defect Report #260, which is still open. However, the
Committee discussion and proposed response indicates that the Committee
seems to be inclined towards answering no.

Martin
Nov 13 '05 #25
The Real OS/2 Guy <os****@pc-rosenau.de> wrote:
On Sun, 7 Sep 2003 14:18:23 UTC, Kevin Easton
<kevin@-nospam-pcug.org.au> wrote:
> #include <stdio.h>
> #include <string.h>
> #include <stdlib.h>
>
> int main(void)
> {
> int i;
> void *p = malloc(1);
> unsigned char initial[sizeof p], final[sizeof p];
>
> memcpy(initial, &p, sizeof p);
> free(p);
> memcpy(final, &p, sizeof p);
> for(i = 0; i < sizeof p; i++)
> if(initial[i] != final[i])
> printf("The value changed\n");
> return 0;
> }
>
> 1. Does this program have undefined behaviour?


No.


Wrong. You can't use a pointer after free()ing it. This is undefined
behavior.


Please point to where the quoted program uses an indeterminate pointer
value.

- Kevin.

Nov 13 '05 #26

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

Similar topics

2
by: Alex | last post by:
Entering the following in the Python shell yields >>> help(dict.copy) Help on method_descriptor: copy(...) D.copy() -> a shallow copy of D >>>
3
by: Douwe | last post by:
I try to build my own version of printf which just passes all arguments to the original printf. As long as I keep it with the single argument version everything is fine. But their is also a version...
4
by: Simon Schaap | last post by:
Hello, I have encountered a strange problem and I hope you can help me to understand it. What I want to do is to pass an array of chars to a function that will split it up (on every location where...
7
by: simkn | last post by:
Hello, I'm writing a function that updates an array. That is, given an array, change each element. The trick is this: I can't change any elements until I've processed the entire array. For...
8
by: downwitch | last post by:
Either I don't understand (entirely possible), or there's no way to copy parts of a class hierarchy from one instance to another. Say I have a class called Foo, and it contains, among other...
8
by: rKrishna | last post by:
I was trying to understand the real need for copy constructors. From literature, the main reason for redfinition of copy constructor in a program is to allow deep copying; meaning ability to make...
9
by: hfinster | last post by:
Hello, could somebody please shed light on the following problem with g++ (4.03 and 3.3.6 as well)? Obviously, the copy constructor is not executed, if I assign the result of a function call to...
20
by: royashish | last post by:
Hi all , A question to the C++ fraternity , Why the Copy constructor ? Was suggested in Effective C++ , In Case the Object contains a Char* , a assignment operator = makes the two object...
7
by: pereges | last post by:
which one do you think is better ? I need to make my program efficient and in some places I have passed the copy of a variable which makes life some what easy while writing huge expressions but...
6
by: Peng Yu | last post by:
Hi, I'm wondering if the following assignment is lazy copy or not? Thanks, Peng std::vector<intv. v.push_back(1); v.push_back(2);
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: 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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.