468,513 Members | 1,750 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,513 developers. It's quick & easy.

Messing with intptr_t

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
Oct 10 '06 #1
2 4774
Richard Tobin wrote:
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.
There's nothing obviously wrong, as far as I can see. The
same thing can be done without reliance on intptr_t by mucking
with the individual `unsigned char' constituents of `p'.

Fans of garbage collection like to label this sort of thing
"perverse," with the implication that no reasonable program would
indulge in such whimsy and so only unreasonable progams would be
broken by their garbage collectors. And yet, there are plenty
of programs that make constructive use of low-order pointer bits
that they "know" will be zeroes, using them to store a handy flag
or a small type code or something. Such programs are already not
portable (the "knowledge" of low-order zeroes is not portable),
but can be extremely practical -- and would be utterly destroyed
by the C-ish garbage collectors I've heard of.

--
Eric Sosman
es*****@acm-dot-org.invalid
Oct 11 '06 #2
ri*****@cogsci.ed.ac.uk (Richard Tobin) wrote:
# Is the following program legal? (I think it is, though it relies on
# the existence of intptr_t.)

Twiddling pointers as integers as a long and hallowed history
in C. And yes it runs havoc with Boehm style collectors.

The classical example is to stored two xored pointers in one
link location. If you have the current cell X and the previous
P and next cell N, one 'optimisation' is to store a single link
field in X which is P^N. Then if you traverse from P to N you
compute N = X->link ^ P; from N to X, P = X->link^N.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
She broke your heart and inadvertently drove men to deviant lifestyles.
Oct 11 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by D. Alvarado | last post: by
1 post views Thread by leegold2 | last post: by
10 posts views Thread by Xah Lee | last post: by
4 posts views Thread by Trent L | last post: by
3 posts views Thread by Anders K. Jacobsen [DK] | last post: by
5 posts views Thread by king | last post: by
2 posts views Thread by nikanth | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.