Thread: Pointer Comparison

  1. #1
    Registered User
    Join Date
    Apr 2015
    Posts
    10

    Pointer Comparison

    Hello!

    I've read that there are, generally, different implementations of pointers: the most familiar implementation uses bare memory addresses. Others may use offsets (from a fixed memory address) or double indirection.


    If a pointer is implemented using offset or double indirection, how do two pointers compare? In other words, does operator== compare the values of its pointer operands? Or does operator== compare the actual memory addresses calculated from the values of its pointer operands?


    I'm asking this question because I've been thinking what if two pointers, having different values, point to the same object: does operator== return true or false in this case?


    Thanks for help...

  2. #2
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Quote Originally Posted by atran View Post
    I'm asking this question because I've been thinking what if two pointers, having different values, point to the same object: does operator== return true or false in this case?
    True.
    Code:
    #include <stdio.h>
    
    int main()
    {
        int *x, *y;
        int a = 10;
        x = &a;
        y = &a;
    
        printf("%s", x == y ? "true":"false");
    
        return 0;
    }

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by atran View Post
    if two pointers, having different values, point to the same object
    How could they?

    It is like asking - could some integer variables containing different values store same number.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Apr 2015
    Posts
    10
    Because if, according to some implementation, a pointer value is an offset (not an address), then it's possible for two distinct pointer values (that is, each of which is an offset from a different fixed memory address) to point to the same object.


    I don't really know, that is why I am asking...

  5. #5
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Because if, according to some implementation, a pointer value is an offset
    IF??? Which implementation?

    None that I know.

    C & C++ are languages controlled by very explicit Open Standards. Some compilers do not adhere to the Standards the way they should, but I have never heard of this.

  6. #6
    Registered User
    Join Date
    Apr 2015
    Posts
    10
    According to this book C Programming: A Modern Approach, a pointer is not necessarily the same as an address. Here is one reason:
    The book explains, although not in detail since this is not a book on architecture, that there are CPUs which "can execute programs in several modes," in one of which (called real mode) addresses are represented by an offset or a segment: offset pair.
    Last edited by atran; 06-27-2015 at 08:34 AM.

  7. #7
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    The implementation must normalize the pointers (so that two pointers that point to the same object have the same value) before the comparison.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by atran
    If a pointer is implemented using offset or double indirection, how do two pointers compare? In other words, does operator== compare the values of its pointer operands? Or does operator== compare the actual memory addresses calculated from the values of its pointer operands?


    I'm asking this question because I've been thinking what if two pointers, having different values, point to the same object: does operator== return true or false in this case?
    Refer to the C standard:
    Quote Originally Posted by C11 Clause 6.5.9 Paragraphs 6, 7 and Note 109
    Two pointers compare equal if and only if both are null pointers, both are pointers to the same object (including a pointer to an object and a subobject at its beginning) or function, both are pointers to one past the last element of the same array object, or one is a pointer to one past the end of one array object and the other is a pointer to the start of a different array object that happens to immediately follow the first array object in the address space. [Note 109]

    For the purposes of these operators, a pointer to an object that is not an element of an array behaves the same as a pointer to the first element of an array of length one with the type of the object as its element type.

    Note 109: Two objects may be adjacent in memory because they are adjacent elements of a larger array or adjacent members of a structure with no padding between them, or because the implementation chose to place them so, even though they are unrelated. If prior invalid pointer operations (such as accesses outside array bounds) produced undefined behavior, subsequent comparisons also produce undefined behavior.
    So, it does not matter how are pointers implemented; if the implementation conforms to the standard, then the pointers will compare with the same result for the same corresponding operands.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Apr 2015
    Posts
    10
    Thank you for your replies.

    I'm having trouble understanding the C11 standard draft. Especially the following:
    Two pointers compare equal if and only if [...] both are pointers to the same object (including a pointer to an object and a subobject at its beginning).
    Is there any implicit type conversion when two pointers of different types are compared?
    Code:
    #include <stdio.h>
    
    int main(void) {
        short i = 256;          // 0000 0001 0000 0000
        short *p = &i;          // p points to i
        char *q = (char *) p;   // q points to the first byte of i (ie 0000 0000)
    
        printf("%d\n", p == q);
        return 0;
    }
    The code outputs 1 (true), why not 0 (false)?
    The two pointers do not point to the same object...

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by atran
    Is there any implicit type conversion when two pointers of different types are compared?
    Examining the paragraph before the ones that I quoted earlier:
    Quote Originally Posted by C11 Clause 6.5.9 Paragraph 5
    Otherwise, at least one operand is a pointer. If one operand is a pointer and the other is a null pointer constant, the null pointer constant is converted to the type of the pointer. If one operand is a pointer to an object type and the other is a pointer to a qualified or unqualified version of void, the former is converted to the type of the latter.
    It looks like the answer is no, or perhaps it results in implementation defined or undefined behaviour. This is easily avoided, of course:
    Code:
    printf("%d\n", (char*)p == q);
    Quote Originally Posted by atran
    The code outputs 1 (true), why not 0 (false)?
    The two pointers do not point to the same object...
    The "first byte of i" can be interpreted as the subobject of i at its beginning, when i is viewed as an array/structure of bytes.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Apr 2015
    Posts
    10
    When I tried the same code with -pedantic-errors, the compilation failed with the following error message:
    error: comparison of distinct pointer types lacks a cast
    So, two pointers compare equal if one points to the object and the other points to the subobject at the beginning of that object. Can you come up with an example in C? (I have not studied structs, unions, etc... yet)

    I assume the same (or similar) rule applies to C++:
    Code:
    // D is a derived class of B
    B* b = new D();
    D* d = (D*) b;
    printf("%d\n", b == d);
    The program outputs 1 (true). Is it true because b points to the subobject at the beginning of the instance object of class D?

    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Comparison between pointer and integer
    By Thedon in forum C Programming
    Replies: 7
    Last Post: 11-12-2011, 01:09 PM
  2. comparison pointer and integer
    By plodos in forum C Programming
    Replies: 19
    Last Post: 01-09-2009, 08:51 AM
  3. Function pointer comparison
    By matsp in forum C Programming
    Replies: 14
    Last Post: 08-28-2008, 10:05 AM
  4. pointer comparison failing
    By Bleech in forum C Programming
    Replies: 4
    Last Post: 08-11-2007, 06:33 PM
  5. comparison between pointer and integer
    By bazzano in forum C Programming
    Replies: 3
    Last Post: 03-07-2006, 01:15 PM

Tags for this Thread