Re: pointer to void property



"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.

Best regards.
pointer.
--
Irrwahn Grausewitz (irrwahn35@xxxxxxxxxx)
welcome to clc : http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc frequent answers: http://benpfaff.org/writings/clc
.



Relevant Pages

  • [RFC v8][PATCH 08/12] Infrastructure for shared objects
    ... and also stored in a hash table. ... * @ptr: pointer to object ... * @type: object type ... * fresh unique object reference. ...
    (Linux-Kernel)
  • [RFC v9][PATCH 07/13] Infrastructure for shared objects
    ... and also stored in a hash table. ... * @ptr: pointer to object ... * @type: object type ... * fresh unique object reference. ...
    (Linux-Kernel)
  • Re: null function pointer?
    ... >> mean a pointer to an object type or to an incomplete type. ... >> more than one interpretation is possible. ... > (void *)0 doesn't point to any object. ...
    (comp.lang.c)
  • Re: void pointers
    ... I gave your suggestion a try but my compiler returns the following ... complete object type ... way that makes sense is an array of void pointers. ...
    (comp.lang.c)
  • Re: Where do pointers point to?
    ... >> absolute guarantee. ... > having the void* type. ... you can pass a void* to a function requiring any form of pointer ... between pointer to any object type and any pointer to function type. ...
    (comp.lang.c)