copy to pointer memory ?

K

Kevin Easton

Simon Biber said:
pete said:
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 != final)
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.
 
T

The Real OS/2 Guy

#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 != final)
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.


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

Irrwahn Grausewitz

The Real OS/2 Guy said:
#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 != final)
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?
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.
 
M

Martin Dickopp

Irrwahn Grausewitz said:
The Real OS/2 Guy said:
#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 != final)
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
 
K

Kevin Easton

The Real OS/2 Guy said:
#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 != final)
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.
 

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

Forum statistics

Threads
474,079
Messages
2,570,574
Members
47,206
Latest member
Zenden

Latest Threads

Top