About Pointers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Tassos Souris
    New Member
    • Aug 2008
    • 152

    About Pointers

    The standard says that a pointer is a reference to an object of a referenced type T.
    Also, pointers that reference different types the standard says that may not be "equivalent "...
    After seeing some defect reports (e.g http://www.open-std.org/jtc1/sc22/wg14/www/docs/dr_051.html) and considering what the standard says i have the following question:
    What value does a pointer store? Almost all books (and in universities) they say a pointer stores the address of an object. But if that is a case how can the above problems rise??
    If you know post links to articles and tutorials only on this aspect of pointers.

    Thanks in advance
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    What value does a pointer store? Almost all books (and in universities) they say a pointer stores the address of an object.
    You are confusing what a pointer stores conceptually and what a pointer stores actually.

    What it stores conceptually is the memory address of the object. On many platforms what it stores actually is the memory address of the object. However the standard makes no requirements on what it actually stores so it does not necessarily need to be the memory address of the object or does not need to be limited to the memory address of the object.

    Imagine a platform where somehow the size of the object as well as the location is also encoded into its pointer type and that the hardware can detect an access via a pointer to a location outside the object pointed to and raise a hardware exception.

    All of that is with-in the standard and would cause the problem you linked to.

    The point is you do not need to know what a pointer stores, only how it may be used in a standard compliant way or a platform compliant way if you are writing non-portable code.

    In the same way you do not need to know the actual value of a NULL pointer. Did you think it was 0? Well again on many platforms it is but what the standard says is that the value 0 used in the context of a pointer shall be converted to the platforms representation of a NULL pointer which is not required to have an absolute value of 0.

    Comment

    • Tassos Souris
      New Member
      • Aug 2008
      • 152

      #3
      Thanks Banfa!

      One more question... Can the following be false in a situation?
      Code:
      T obj;
      T *ptr = &obj;
      assert( ptr == &obj );
      Or does the & comply with the way pointers store the data they need?

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        No that is always true, of course & complies it is the dereference operator, it provides the point to the object.

        Comment

        • Tassos Souris
          New Member
          • Aug 2008
          • 152

          #5
          got it..
          thanks Banfa!

          i am studying those things now and really there are some "dark corners"...

          Comment

          • Tassos Souris
            New Member
            • Aug 2008
            • 152

            #6
            So, is:
            Code:
            int array[ 10 ];
            int *x = &array[ 0 ];
            ++x;
            undefined behavior; since as you said the size of the object may matter.... so ++x may fail...
            if so, the fact that all string handling works with char * is because all objects can be represented as an array of characters?

            Thanks in advance

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              That is not undefined behaviour for 2 reasons.

              1. You only increment x once, it is still inside the bounds of array.

              2. You never dereference x, that is you don't try to access what it points to so even if it pointed out of bounds you wouldn't make an out of bounds access.

              Comment

              • Tassos Souris
                New Member
                • Aug 2008
                • 152

                #8
                I am refering to the fact that you said that a pointer may record the size of the object it points to . So if i say int *x then it may record that x points to one integer. So when i do ++x a failure may occur cause i did not said that x points to a sequence of integers but only to one integer...
                Well, now i am confused... :P damned good old days where i knew that a pointer stores only the address.. :P

                Comment

                • whodgson
                  Contributor
                  • Jan 2007
                  • 542

                  #9
                  One pointer (no pun) may not have been fully covered relative to C style strings namely:
                  char* p......where p is a pointer with a char type.
                  In this case the output operator '<<' behaves differently than in the normal pointer context, in that it outputs the entire char string to which the pointer points. More usually << will simply output the address of the pointer.

                  Comment

                  • Banfa
                    Recognized Expert Expert
                    • Feb 2006
                    • 9067

                    #10
                    I see, but x does point to an array of 10 integers, the code you wrote above is guaranteed to work, x is still pointing with-in the object.

                    You have made an assumption about how such a system would work that is wrong. You have assumed that the system determins the size of the object pointed at in a simple manor. However the C standard has precedence here it states that the code you wrote must work so it must work. That implies that for the the hypothetical system I suggested where the size of the object pointed to is stored along with the pointer its compiler would have to be capable of detecting that the object in question in the code above is the array and not the single entry that you referenced.

                    On the other hand if you can ascertain that the problem of the compiler being capable of determining object size correctly in all cases is not soluble then it comes most likely that it is not possible to build the hypothetical system I suggested.

                    Comment

                    • Tassos Souris
                      New Member
                      • Aug 2008
                      • 152

                      #11
                      Ok, i understand. So the object being referenced and information for it is obtained when the & operator is applied... So, yes since i did x = &array[ 0 ] i said that x will point to an array...

                      Thanks Banfa!

                      Comment

                      Working...