R
Richard Tobin
Is the following program legal? (I think it is, though it relies on
the existence of intptr_t.)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
int main(void)
{
char *p;
intptr_t i;
if(!(p = malloc(10)))
abort();
strcpy(p, "hello");
i = (intptr_t)(void *)p;
i ^= 63;
p = 0;
/* plugh */
i ^= 63;
p = (char *)(void *)i;
printf("%s\n", p);
return 0;
}
This is suggested, of course, by the thread on garbage collection.
It's just the sort of program that is likely to break with a collector
of the Boehm type, because at the point commented "plugh" there is
probably nothing resembling a pointer to the malloc()ed memory, so if
there were another malloc() there it might re-use the same memory.
-- Richard
the existence of intptr_t.)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
int main(void)
{
char *p;
intptr_t i;
if(!(p = malloc(10)))
abort();
strcpy(p, "hello");
i = (intptr_t)(void *)p;
i ^= 63;
p = 0;
/* plugh */
i ^= 63;
p = (char *)(void *)i;
printf("%s\n", p);
return 0;
}
This is suggested, of course, by the thread on garbage collection.
It's just the sort of program that is likely to break with a collector
of the Boehm type, because at the point commented "plugh" there is
probably nothing resembling a pointer to the malloc()ed memory, so if
there were another malloc() there it might re-use the same memory.
-- Richard