Re: Pointers in C



rafalp wrote:
Clark S. Cox III wrote:
I'm not making anywhere the assumption that they are adjacent. My point
was to explain to the OP that for two given objects o1, o2 of the
same type

&o1 - &o2 := (addressof(o1) - addressof(o2)) / sizeof(objects)

That's all.

Even that isn't guaranteed.

(&o1 - &o2) could be (42) and ((char*)&o1 - (char*)&o2) could be
('bananas'). Subtracting pointers to two objects that aren't part of
some other object (i.e. array, structure, union, etc.) is undefined.


I don't understand what you mean. (&o1 - &o2) and ((char*)&o1 -
(char*)&o2) give different results even if o1 and o2 are the part of the
same array unless sizeof(o1) == sizeof(char).

If they are *not* part of the same array, there are no restrictions on
what either subtraction could yield, nor is there any guarantee that the
two results are related in any way (hence the "42" and "'bananas'").


--
Clark S. Cox III
clarkcox3@xxxxxxxxx
.



Relevant Pages

  • Re: Insufficient guarantees for null pointers?
    ... guaranteed to point to the subobject at the beginning of the array, ... for a pointer arithmetic to be defined there must be ... Why should conversion to void* change the pointer's limits? ... the Standard didn't explicitly guarantee in general what the result of ...
    (comp.std.c)
  • Re: setSize ArrayList, when will it come?
    ... And then upon some other event the size might jump ... and the element at position m-1 is set to non null. ... that the array might become gradually less sparse. ... there is no guarantee that they are close together or not. ...
    (comp.lang.java.programmer)
  • Re: Memory layout question
    ... The elements of the array are guaranteed to be contiguous in memory ... There is no guarantee that the alignment of an_array will satisfy the ... >For what versions of standard C is this kluge required to ...
    (comp.lang.c)
  • Re: Byte Alignment of Character to 2 byte
    ... > aligned in the structure array, so that its compatible with the lower ... While standard wchar_t isn't guaranteed to be 2 bytes or aligned in any ... Whether it is appropriate depends on how the "lower layer calls" are ... use something like mallocto guarantee alignment portably. ...
    (comp.lang.c)
  • Re: Read only array of int thread safe ?
    ... That's not to say that read-only use of an array is not thread-safe. ... To guarantee protection of the array against writes, ... This doesn't change the thread safety of it, ...
    (microsoft.public.dotnet.languages.csharp)