Re: pointer to void property




Irrwahn Grausewitz wrote:
> "aegis" <aegis@xxxxxxxxxxxxxxxxx> wrote:
> >Given the following:
> >
> >int a = 10;
> >
> >int *p;
> >void *p1;
> >unsigned char *p2;
> >
> >p = &a;
> >
> >p1 = p;
> >
> >p2 = p1;
> >
> >is a guarantee made that I can access the object representation
> >via 'p2'?
> >
> >Will the value assigned between different pointer to object types
> >through use of a generic pointer preserve this value?
> >
> >I know it is guaranteed to be preserved if it is assigned from a
> >pointer to an object type to a void pointer and back again to said
> >pointer to object type. But will the value assigned between different
> >pointer to object types through use of a generic pointer, preserve this
> >value?
>
> As long as you stick with a character pointer, yes. Consider:
>
> ISO/IEC 9899:1999 (E)
>
> 6.2.5p26
> A pointer to void shall have the same representation and alignment
> requirements as a pointer to a character type.
>
> 6.3.2.3p1
> A pointer to void may be converted to or from a pointer to any
> incomplete or object type. [...]
>
> 6.3.2.3p7
> A pointer to an object or incomplete type may be converted to a
> pointer to a different object or incomplete type. If the resulting
> pointer is not correctly aligned for the pointed-to type, the
> behavior is undefined. Otherwise, when converted back again, the
> result shall compare equal to the original pointer. When a pointer
> to an object is converted to a pointer to a character type, the
> result points to the lowest addressed byte of the object. Successive
> increments of the result, up to the size of the object, yield
> pointers to the remaining bytes of the object.
>
> Note that (AFAICT) it's not guaranteed to work with, e.g., a
> pointer-to-int and a pointer-to-double. But casting from any
> pointer-to-sometype to pointer-to-void is safe, as is casting from any
> pointer-to-void to a pointer-to-charactertype.

I wasn't asking about pointer-to-int to pointer-to-double
nor about pointer-to-sometype to pointer-to-void but specifically about
pointer-to-sometype to pointer-to-void to
pointer-to-[signed|unsigned]char


With something involving pointer-to-int to pointer-to-void to
pointer-to-double, the result from what I can see, may not be correctly
aligned.

--
aegis

.



Relevant Pages