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.