Comparing pointers

Discussion in 'C Programming' started by J Caesar, Jun 5, 2007.

  1. J Caesar

    J Caesar Guest

    In C you can compare two pointers, p<q, as long as they come from the
    same array or the same malloc()ated block. Otherwise you can't.

    What I'd like to do is write a function
    int comparable(void *p, void *q)
    that will take any two pointers and decide whether they can be compared
    or not.

    I really can't think how to do this - any suggestions?

    JC.
     
    J Caesar, Jun 5, 2007
    #1
    1. Advertising

  2. On Tue, 05 Jun 2007 23:44:57 +0200, J Caesar wrote:

    > In C you can compare two pointers, p<q, as long as they come from the
    > same array or the same malloc()ated block. Otherwise you can't.


    Really!? How have I managed to do so all these years?

    Pointers are just addresses - usually 32-, these days more and
    more, 64-bit unsigned integers. Which you can always compare at will.
    Usually you'll want for them to be pointers to the same type of data, of
    course.


    >
    > What I'd like to do is write a function int comparable(void *p, void *q)
    > that will take any two pointers and decide whether they can be compared
    > or not.
    >
    > I really can't think how to do this - any suggestions?
    >
    > JC.
     
    Ivar Rosquist, Jun 5, 2007
    #2
    1. Advertising

  3. "J Caesar" <> wrote in message
    news:...
    > In C you can compare two pointers, p<q, as long as they come from the
    > same array or the same malloc()ated block. Otherwise you can't.
    >
    > What I'd like to do is write a function
    > int comparable(void *p, void *q)
    > that will take any two pointers and decide whether they can be compared
    > or not.
    >
    > I really can't think how to do this - any suggestions?
    >

    It can't be done. Or rather, it can't be done portably. To do it
    non-portably is probably either trivial or the compiler has no issues with
    pointers to different blocks.

    The "no comparison" rule is a kludge to allow for funny architectures. It
    becomes a nuisance when you need to know whether a pointer comes from a
    certain block or not. However you don't need that information very
    frequently, and on balance it is probably better to make the compiler easier
    to implement or code to run faster.

    --
    Free games and programming goodies.
    http://www.personal.leeds.ac.uk/~bgy1mm
     
    Malcolm McLean, Jun 5, 2007
    #3
  4. In article <>,
    J Caesar <> wrote:
    >In C you can compare two pointers, p<q, as long as they come from the
    >same array or the same malloc()ated block. Otherwise you can't.
    >
    >What I'd like to do is write a function
    >int comparable(void *p, void *q)
    >that will take any two pointers and decide whether they can be compared
    >or not.
    >
    >I really can't think how to do this - any suggestions?


    You're right, you can't. But why do you want to? What are you trying
    to achieve?

    Note that you can compare pointers for equality even if they are
    from different objects.

    -- Richard

    --
    "Consideration shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.
     
    Richard Tobin, Jun 5, 2007
    #4
  5. "Ivar Rosquist" <> wrote in message
    news:p...
    > On Tue, 05 Jun 2007 23:44:57 +0200, J Caesar wrote:
    >
    >> In C you can compare two pointers, p<q, as long as they come from the
    >> same array or the same malloc()ated block. Otherwise you can't.

    >
    > Really!? How have I managed to do so all these years?
    >

    It is undefined behaviour. Realistically a flat memory architecture is just
    going to do a starightforwards subtraction of bits. However it could trap or
    return funny results on some machines.

    --
    Free games and programming goodies.
    http://www.personal.leeds.ac.uk/~bgy1mm
     
    Malcolm McLean, Jun 5, 2007
    #5
  6. J Caesar

    Flash Gordon Guest

    J Caesar wrote, On 05/06/07 22:44:
    > In C you can compare two pointers, p<q, as long as they come from the
    > same array or the same malloc()ated block. Otherwise you can't.
    >
    > What I'd like to do is write a function
    > int comparable(void *p, void *q)
    > that will take any two pointers and decide whether they can be compared
    > or not.
    >
    > I really can't think how to do this - any suggestions?


    Your initial thought is correct, you can't do the check in standard C.
    --
    Flash Gordon
     
    Flash Gordon, Jun 5, 2007
    #6
  7. In article <>,
    Ivar Rosquist <> wrote:
    >On Tue, 05 Jun 2007 23:44:57 +0200, J Caesar wrote:


    >> In C you can compare two pointers, p<q, as long as they come from the
    >> same array or the same malloc()ated block. Otherwise you can't.


    > Really!? How have I managed to do so all these years?


    Non-portably.

    > Pointers are just addresses - usually 32-, these days more and
    >more, 64-bit unsigned integers. Which you can always compare at will.
    >Usually you'll want for them to be pointers to the same type of data, of
    >course.


    Pointers are not necessarily just addresses: pointers may be opaque
    with storage silently managed "under the hood", or pointers may
    include type information, or pointers may include not have the
    address information in numeric order (e.g., Cray put the character
    offset bits in the upper byte). Or, as was common on VMS, pointers
    might be the address of "descriptors" -- a block of data that
    describes the type and array and virtual memory address of the
    actual storage. And when you are working with a Harvard architecture,
    a pointer to a function could have exactly the same numeric value
    as a pointer to an object: since C does not provide any operations
    that translate between function pointers and object pointers, there
    is no conflict in having a function pointer with the same value
    as an object pointer, with the code knowing which kind of instructions
    are needed for the correct kind of access.
    --
    All is vanity. -- Ecclesiastes
     
    Walter Roberson, Jun 5, 2007
    #7
  8. J Caesar

    J Caesar Guest

    On 5 Jun 2007 at 22:12, Richard Tobin wrote:
    > You're right, you can't. But why do you want to? What are you trying
    > to achieve?
    >
    > Note that you can compare pointers for equality even if they are
    > from different objects.


    This gives one method: write a wrapper to malloc to store a pointer to
    each block allocated together with the size of the block. Then do an
    exhaustive search over all pointers to all elements of all blocks
    allocated to find which blocks p and q are in (only needs you to compare
    each pointer for equality with p or q, which is legal). And you're done.

    I see two problems: 1) very inefficient if large tracts of memory are
    allocated; 2) fails if p or q point inside statically allocated arrays
    rather than malloc()ed blocks.

    JC.

    > -- Richard
    >
     
    J Caesar, Jun 5, 2007
    #8
  9. In article <>,
    christian.bau <> wrote:

    >> > In C you can compare two pointers, p<q, as long as they come from the
    >> > same array or the same malloc()ated block. Otherwise you can't.


    >> Really!? How have I managed to do so all these years?


    >By ignoring the C Standard and by avoiding any C implementations with
    >segmented memory.


    Which shows poor judgment but good taste.

    -- Richard



    --
    "Consideration shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.
     
    Richard Tobin, Jun 5, 2007
    #9
  10. On Jun 5, 11:02 pm, Ivar Rosquist <> wrote:
    > On Tue, 05 Jun 2007 23:44:57 +0200, J Caesar wrote:
    > > In C you can compare two pointers, p<q, as long as they come from the
    > > same array or the same malloc()ated block. Otherwise you can't.

    >
    > Really!? How have I managed to do so all these years?


    By ignoring the C Standard and by avoiding any C implementations with
    segmented memory.
     
    christian.bau, Jun 5, 2007
    #10
  11. J Caesar <> writes:
    > In C you can compare two pointers, p<q, as long as they come from the
    > same array or the same malloc()ated block. Otherwise you can't.
    >
    > What I'd like to do is write a function
    > int comparable(void *p, void *q)
    > that will take any two pointers and decide whether they can be compared
    > or not.
    >
    > I really can't think how to do this - any suggestions?


    I don't believe there's any way to do this in standard C. Attempting
    to compare the pointers invokes undefined behavior; there's no way to
    detect that before running into it.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jun 6, 2007
    #11
  12. J Caesar

    Ian Collins Guest

    christian.bau wrote:
    > On Jun 5, 11:02 pm, Ivar Rosquist <> wrote:
    >> On Tue, 05 Jun 2007 23:44:57 +0200, J Caesar wrote:
    >>> In C you can compare two pointers, p<q, as long as they come from the
    >>> same array or the same malloc()ated block. Otherwise you can't.

    >> Really!? How have I managed to do so all these years?

    >
    > By ignoring the C Standard and by avoiding any C implementations with
    > segmented memory.
    >

    Segmented or virtual memory.

    --
    Ian Collins.
     
    Ian Collins, Jun 6, 2007
    #12
  13. J Caesar

    Vinoj Guest

    On Jun 6, 2:44 am, J Caesar <> wrote:
    > In C you can compare two pointers, p<q, as long as they come from the
    > same array or the same malloc()ated block. Otherwise you can't.
    >
    > What I'd like to do is write a function
    > int comparable(void *p, void *q)
    > that will take any two pointers and decide whether they can be compared
    > or not.
    >
    > I really can't think how to do this - any suggestions?
    >
    > JC.


    Typecast the pointers to 'unsigned int' before comparing them.


    Thanks, Regards,
    Vinoj
     
    Vinoj, Jun 6, 2007
    #13
  14. J Caesar

    Ian Collins Guest

    Vinoj wrote:
    > On Jun 6, 2:44 am, J Caesar <> wrote:
    >> In C you can compare two pointers, p<q, as long as they come from the
    >> same array or the same malloc()ated block. Otherwise you can't.
    >>
    >> What I'd like to do is write a function
    >> int comparable(void *p, void *q)
    >> that will take any two pointers and decide whether they can be compared
    >> or not.
    >>
    >> I really can't think how to do this - any suggestions?
    >>

    >
    > Typecast the pointers to 'unsigned int' before comparing them.
    >

    Cast, not typecast. This is bad advice for at least three reasons:

    Appropriate pointers can be compared without any casts.

    On many systems, including the one I'm typing this on, pointers are
    larger than unsigned int.

    Casting hides the intent.

    --
    Ian Collins.
     
    Ian Collins, Jun 6, 2007
    #14
  15. J Caesar

    Vinoj Guest

    On Jun 6, 1:42 pm, Ian Collins <> wrote:
    > Vinoj wrote:
    > > On Jun 6, 2:44 am, J Caesar <> wrote:
    > >> In C you can compare two pointers, p<q, as long as they come from the
    > >> same array or the same malloc()ated block. Otherwise you can't.

    >
    > >> What I'd like to do is write a function
    > >> int comparable(void *p, void *q)
    > >> that will take any two pointers and decide whether they can be compared
    > >> or not.

    >
    > >> I really can't think how to do this - any suggestions?

    >
    > > Typecast the pointers to 'unsigned int' before comparing them.

    >
    > Cast, not typecast. This is bad advice for at least three reasons:
    >
    > Appropriate pointers can be compared without any casts.
    >
    > On many systems, including the one I'm typing this on, pointers are
    > larger than unsigned int.
    >
    > Casting hides the intent.
    >
    > --
    > Ian Collins.


    I am yet to buy a 64-bit system. Due to the lack of OS support I am
    unable to buy one. I just want to run Linux on a 64-bit system. Can
    you please provide me a pointer, where I can readily get IA-64 Linux
    Distributable. It seems there is no support for DOS in 64 bit OS, so
    I cannot get a DOS box on Windows if I have a Dual Boot? I just dont
    want to go for a expensive server models that take up my room space
    where I already I have 2 machines one Pentium 4 HT and one Celeron,
    for device driver WinDBG and such stuff.

    Thanks, Regards,
    Vinoj
     
    Vinoj, Jun 6, 2007
    #15
  16. Vinoj said:

    > On Jun 6, 2:44 am, J Caesar <> wrote:
    >> In C you can compare two pointers, p<q, as long as they come from the
    >> same array or the same malloc()ated block. Otherwise you can't.
    >>
    >> What I'd like to do is write a function
    >> int comparable(void *p, void *q)
    >> that will take any two pointers and decide whether they can be
    >> compared or not.
    >>
    >> I really can't think how to do this - any suggestions?
    >>
    >> JC.

    >
    > Typecast the pointers to 'unsigned int' before comparing them.


    Presuming you mean "cast", how would that help to establish whether they
    can be compared /without/ the cast? And if your purpose is to avoid the
    question altogether by making sure they can *always* be compared,
    consider the possibility that the cast loses information - for example,
    imagine a system with 16-bit unsigned ints and 20-bit pointers. (No,
    that wasn't an accidental choice.)

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
     
    Richard Heathfield, Jun 6, 2007
    #16
  17. J Caesar

    pete Guest

    Ian Collins wrote:
    >
    > Vinoj wrote:
    > > On Jun 6, 2:44 am, J Caesar <> wrote:
    > >> In C you can compare two pointers, p<q, as long as they come from the
    > >> same array or the same malloc()ated block. Otherwise you can't.
    > >>
    > >> What I'd like to do is write a function
    > >> int comparable(void *p, void *q)
    > >> that will take any two pointers and decide whether they can be compared
    > >> or not.
    > >>
    > >> I really can't think how to do this - any suggestions?
    > >>

    > >
    > > Typecast the pointers to 'unsigned int' before comparing them.
    > >

    > Cast, not typecast. This is bad advice for at least three reasons:
    >
    > Appropriate pointers can be compared without any casts.
    >
    > On many systems, including the one I'm typing this on, pointers are
    > larger than unsigned int.
    >
    > Casting hides the intent.


    #4 The result of the comparison of the resulting 'unsigned int'
    values, doesn't imply which pointer has the higher address.

    --
    pete
     
    pete, Jun 6, 2007
    #17
  18. Vinoj <> writes:
    [...]
    > I am yet to buy a 64-bit system. Due to the lack of OS support I am
    > unable to buy one. I just want to run Linux on a 64-bit system. Can
    > you please provide me a pointer, where I can readily get IA-64 Linux
    > Distributable.


    Your question has nothing to do with C, or even with this discussion.
    Try a Linux-specific newsgroup.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jun 6, 2007
    #18
  19. On Wed, 6 Jun 2007 00:32:13 +0200 (CEST), J Caesar
    <> wrote:

    >On 5 Jun 2007 at 22:12, Richard Tobin wrote:
    >> You're right, you can't. But why do you want to? What are you trying
    >> to achieve?
    >>
    >> Note that you can compare pointers for equality even if they are
    >> from different objects.

    >
    >This gives one method: write a wrapper to malloc to store a pointer to
    >each block allocated together with the size of the block. Then do an
    >exhaustive search over all pointers to all elements of all blocks
    >allocated to find which blocks p and q are in (only needs you to compare
    >each pointer for equality with p or q, which is legal). And you're done.


    p or q may be interior pointers to a malloc'd block.

    Just comparing them for equality with the base address won't work.
    You need to compare base <= p < (base+size), at which point you're
    back to undefined behavior if p is not a pointer into the block.

    There isn't any way to do it portably unless you control allocation.
    If you use your own heap functions and arrange that all allocations
    are made from an array under your control, then you can safely compare
    any pointers into those blocks.

    But it still won't help in the general case. Technically, by the
    standard, comparing unrelated pointers in any way (even for equality)
    produces undefined behavior. The exception is that any pointer may be
    compared against the constant NULL ... but then the value of NULL is
    implementation defined and _not_ guaranteed to be zero.


    People have been writing technically non-portable pointer code since
    the beginning. Most have just been lucky that the platforms they
    targeted had memory layouts which coincidentally corresponded to the
    definition of a C array and pointers were simple addresses. As Walter
    Roberson mentioned earlier, there are systems in which the notion of a
    pointer is more complicated. There are even some systems in which
    memory is not organized as an array.

    George
    --
    for email reply remove "/" from address
     
    George Neuner, Jun 6, 2007
    #19
  20. J Caesar

    Clark Cox Guest

    On 2007-06-06 12:28:36 -0700, George Neuner <gneuner2/@comcast.net> said:

    > But it still won't help in the general case. Technically, by the
    > standard, comparing unrelated pointers in any way (even for equality)
    > produces undefined behavior.


    No, comparing for equality is specifically allowed by the standard.

    > The exception is that any pointer may be
    > compared against the constant NULL ... but then the value of NULL is
    > implementation defined and _not_ guaranteed to be zero.



    --
    Clark S. Cox III
     
    Clark Cox, Jun 6, 2007
    #20
    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. agent349
    Replies:
    5
    Views:
    9,702
    asingh0412
    Mar 10, 2009
  2. jalina
    Replies:
    2
    Views:
    290
    Fei Liu
    Apr 24, 2006
  3. carlosp

    Comparing pointers

    carlosp, Mar 12, 2009, in forum: C++
    Replies:
    6
    Views:
    420
    carlosp
    Mar 12, 2009
  4. Charlie Zender

    Comparing pointers to NULL

    Charlie Zender, Aug 7, 2009, in forum: C Programming
    Replies:
    55
    Views:
    1,396
    Bartosz Wroblewski
    Aug 11, 2009
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    711
Loading...

Share This Page