Does pointer assignment preserve bit pattern?

Discussion in 'C Programming' started by Ian Pilcher, Aug 4, 2003.

  1. Ian Pilcher

    Ian Pilcher Guest

    The more I think about pointers, the more my head hurts.

    From what I can tell, nothing in the standard guarantees that pointer
    assignment preserves bit patterns.

    Section 6.5.16.1, paragraph 2, states:

    In simple assignment (=), the value of the right operand is
    converted to the type of the assignment expression and replaces
    the value stored in the object designated by the left operand.

    Since we know that multiple bit patterns can represent the same "value",
    it's possible that the following function could return a non-zero value.

    int ptr_test(void)
    {
    char c;
    void *p, *q;

    p = &c;
    q = p;

    return memcmp(&p, &q, sizeof(void *));
    }

    Since I know you'll ask, I was thinking about implementing some simple
    functions modeled after Win32's HeapAlloc, HeapFree, etc. A "heap" in
    this case would just be a binary search tree, using the pointers
    returned by malloc (or calloc or realloc) as keys.

    This won't work, though, if I can't somehow compare pointers to
    different objects, which rules out a simple comparison.

    Am I missing something?

    Thanks!

    --
    ========================================================================
    Ian Pilcher
    ========================================================================
    Ian Pilcher, Aug 4, 2003
    #1
    1. Advertising

  2. Ian Pilcher

    Kevin Easton Guest

    Ian Pilcher <> wrote:
    > The more I think about pointers, the more my head hurts.
    >
    > From what I can tell, nothing in the standard guarantees that pointer
    > assignment preserves bit patterns.
    >
    > Section 6.5.16.1, paragraph 2, states:
    >
    > In simple assignment (=), the value of the right operand is
    > converted to the type of the assignment expression and replaces
    > the value stored in the object designated by the left operand.
    >
    > Since we know that multiple bit patterns can represent the same "value",
    > it's possible that the following function could return a non-zero value.
    >
    > int ptr_test(void)
    > {
    > char c;
    > void *p, *q;
    >
    > p = &c;
    > q = p;
    >
    > return memcmp(&p, &q, sizeof(void *));
    > }
    >
    > Since I know you'll ask, I was thinking about implementing some simple
    > functions modeled after Win32's HeapAlloc, HeapFree, etc. A "heap" in
    > this case would just be a binary search tree, using the pointers
    > returned by malloc (or calloc or realloc) as keys.
    >
    > This won't work, though, if I can't somehow compare pointers to
    > different objects, which rules out a simple comparison.
    >
    > Am I missing something?


    You can compare pointers to different objects - a pointer to an object
    is guaranteed to compare unequal to a pointer to any other object, and
    unequal to NULL.

    What you can't do is use (in comparisons, or otherwise) pointer values
    that are indeterminate - ones that *don't* point to an object.

    You can almost certainly do what you need by setting any pointer object
    that holds an indeterminate value (eg. after its value has been freed)
    to NULL.

    - Kevin.
    Kevin Easton, Aug 4, 2003
    #2
    1. Advertising

  3. Ian Pilcher

    Ian Pilcher Guest

    Kevin Easton wrote:
    > You can compare pointers to different objects - a pointer to an object
    > is guaranteed to compare unequal to a pointer to any other object, and
    > unequal to NULL.


    Yes, but I need to establish a consistent sorting order, and using
    relational operators with pointers to different objects is UB.

    This led me to consider using memcmp, but memcmp won't necessarily be
    reliable if pointer assignment isn't guaranteed to preserve bit
    patterns.

    --
    ========================================================================
    Ian Pilcher
    ========================================================================
    Ian Pilcher, Aug 4, 2003
    #3
  4. Ian Pilcher wrote:

    > The more I think about pointers, the more my head hurts.
    >
    > From what I can tell, nothing in the standard guarantees that pointer
    > assignment preserves bit patterns.
    >
    > Section 6.5.16.1, paragraph 2, states:
    >
    > In simple assignment (=), the value of the right operand is
    > converted to the type of the assignment expression and replaces
    > the value stored in the object designated by the left operand.
    >
    > Since we know that multiple bit patterns can represent the same "value",
    > it's possible that the following function could return a non-zero value.
    >
    > int ptr_test(void) {
    > char c;
    > void *p, *q;
    >
    > p = &c;
    > q = p;
    >
    > return memcmp(&p, &q, sizeof(void *));
    > }
    >
    > Since I know you'll ask, I was thinking about implementing some simple
    > functions modeled after Win32's HeapAlloc, HeapFree, etc. A "heap" in
    > this case would just be a binary search tree, using the pointers
    > returned by malloc (or calloc or realloc) as keys.
    >
    > This won't work, though, if I can't somehow compare pointers to
    > different objects, which rules out a simple comparison.
    >
    > Am I missing something?
    >
    > Thanks!


    If q = p then (q == p) is true and (q != p) if false.
    There are also two zeros in almost all floating-point representations.

    (-0.0 == +0.0) is true.
    E. Robert Tisdale, Aug 4, 2003
    #4
  5. [Crossposted - added comp.lang.c++ to the group list]


    On Mon, 04 Aug 2003 01:12:21 GMT, Ian Pilcher <> wrote:

    >Kevin Easton wrote:
    >> You can compare pointers to different objects - a pointer to an object
    >> is guaranteed to compare unequal to a pointer to any other object, and
    >> unequal to NULL.

    >
    >Yes, but I need to establish a consistent sorting order, and using
    >relational operators with pointers to different objects is UB.


    In C, yes, as far as I know.

    In C++ that (you can't compare pointers in general wrt. order) is also
    true at the language level, but not at the standard library level.

    So perhaps -- switch to C++?
    Alf P. Steinbach, Aug 4, 2003
    #5
  6. Ian Pilcher

    Dan Pop Guest

    In <> Jack Klein <> writes:

    >Pointer assignment preserves all _value_ bit values, assuming the
    >assignment is between pointers to the same type.


    Chapter and verse, please. Where does the standard say that each
    address is guaranteed to have a unique representation (in terms of value
    bits)?

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Dan Pop, Aug 4, 2003
    #6
  7. Ian Pilcher

    Dan Pop Guest

    In <TmhXa.57430$uu5.6008@sccrnsc04> Ian Pilcher <> writes:

    >The more I think about pointers, the more my head hurts.
    >
    >From what I can tell, nothing in the standard guarantees that pointer
    >assignment preserves bit patterns.


    This is correct. The same address (pointer value) can have multiple
    representations (as many as 4096 in the case of the 8086).

    Then again, pointers can have padding bits, although the standard
    doesn't explicitly say so. Pointer assignment need not replicate them.

    OTOH, the standard guarantees that copying the representation also copies
    the value, so, if this is important to you, you can use memcpy instead of
    pointer assignment.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Dan Pop, Aug 4, 2003
    #7
    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. Saurabh Aggrawal

    32 bit pointer to 64 bit pointer

    Saurabh Aggrawal, Dec 9, 2004, in forum: C++
    Replies:
    1
    Views:
    596
    msalters
    Dec 9, 2004
  2. Replies:
    3
    Views:
    1,743
    Timothy Bendfelt
    Jan 19, 2007
  3. Replies:
    9
    Views:
    968
    Juha Nieminen
    Aug 22, 2007
  4. Grzegorz Chrupala
    Replies:
    2
    Views:
    206
    Grzegorz Chrupala
    Jun 30, 2003
  5. Jeff.M
    Replies:
    6
    Views:
    172
    Lasse Reichstein Nielsen
    May 4, 2009
Loading...

Share This Page