Feature request for "friendship" of pointers in "C"
Holger Lamm
holger@e-gitt.net
Tue Mar 17 21:28:02 GMT 2020
Hi,
Am 02.02.20 um 08:44 schrieb aotto:
> Hi, the following scenario has a "definition hole" in the "C" language
>
> code example:
>
> -------------------------
> struct base {
> ...
> };
>
> struct A {
> struct base obj;
> ...
> } aObj;
>
> struct B {
> struct base obj;
> ...
> } bObj;
>
> void method_base (struct base * hdl, ...);
>
> method_base(&aObj, ...)
> method_base(&bObj, ...)
> ------------------------
>
> - a POINTER to "A" is also a valid POINTER to "base"
> - a POINTER to "B" is also a valid POINTER to "base"
No. The order of elements of structures in memory is implementation
defined and not guaranteed to be the order of enumeration in the definition.
The ONLY valid and portable way to express the call is
method_base(&aObj.obj, ...)
method_base(&bObj.obj, ...)
> - a POINTER to "base" is NOT a valid POINTER to "A" and "B"
of course not. It is a pointer to one element within a structure, not to
the structure itself.
Just my 2 cents,
Holger
More information about the Gcc
mailing list