Hi Guy,

It took me a while to understand what is being proven by the code
below.  I guess that it shows that double initialization is
unnecessary.  Yes, it is true, but in my original post the issue is when
struct_C is not the direct data pointer in the object, but one or more
levels deeper.  We need some other struct_D as the direct data pointer in
the object:

    ....
    typedef struct
    {
        struct_C* data3;
    } struct_D;

    struct_D* c;

    VALUE res = Data_Make_Struct(rb_cObject, struct_D, 0, free, c);
    c->data3 = ALLOC (struct_C);
    c->data3->data1 = ALLOC (struct_A);    /* may rb_gc_mark() here
while data2 is still undefined */
    ....

One good advice from Paul Brannan is instead of double initialization, we
can reverse the order of creation of data (inner first, then outer).

Regards,

Bill
===========================================================================
ts <decoux / moulon.inra.fr> wrote:
> pigeon% cat aa.c
> #include "ruby.h"

> typedef struct
> {
>     int *x;
> } struct_A;

> typedef struct
> {
>     int *x;
> } struct_B;

> typedef struct
> {
>     struct_A *data1;
>     struct_B *data2;
> } struct_C;

> void Init_aa() 
> {
>     struct_C *c;
>     VALUE res = Data_Make_Struct(rb_cObject, struct_C, 0, free, c);
>     if (c->data1 == NULL) {
>         rb_warn("c->data1 is NULL");
>     }
>     if (c->data2 == NULL) {
>         rb_warn("c->data2 is NULL");
>     }
> }
> pigeon% 

> pigeon% ruby -raa -e 1
> warning: c->data1 is NULL
> warning: c->data2 is NULL
> pigeon% 


> Guy Decoux