Messing with intptr_t

Discussion in 'C Programming' started by Richard Tobin, Oct 11, 2006.

  1. 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
     
    Richard Tobin, Oct 11, 2006
    #1
    1. Advertising

  2. Richard Tobin

    Eric Sosman Guest

    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
    lid
     
    Eric Sosman, Oct 11, 2006
    #2
    1. Advertising

  3. Richard Tobin

    SM Ryan Guest

    (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.
     
    SM Ryan, Oct 11, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Anders K. Jacobsen [DK]

    How to stop HTML View from messing up HTML code

    Anders K. Jacobsen [DK], Jan 21, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    685
    Anders K. Jacobsen [DK]
    Jan 22, 2005
  2. Jensen bredal

    IE messing up with font of web pages.

    Jensen bredal, Mar 21, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    320
    Patrice
    Mar 21, 2005
  3. intptr_t standard?

    , Aug 7, 2006, in forum: C Programming
    Replies:
    2
    Views:
    5,320
    Keith Thompson
    Aug 7, 2006
  4. king

    intptr_t

    king, Aug 7, 2006, in forum: C++
    Replies:
    5
    Views:
    737
    P.J. Plauger
    Aug 7, 2006
  5. Jeroen Schot
    Replies:
    4
    Views:
    1,525
    Marcin Grzegorczyk
    Oct 6, 2010
Loading...

Share This Page