#include <stdlib.h>
#include <stdio.h>

typedef struct _Foo_
{
    // variables
    int number;
    char character;
    
    // array of float
    int arrSize;
    float* arr;
    
    // pointer to another instance
    void* myTwin;
} Foo;

Foo* createFoo (int arrSize, Foo* twin);
Foo* destroyFoo (Foo* foo);

int main ()
{
    Foo* a = createFoo (2, NULL);
    Foo* b = createFoo (4, a);
    
    destroyFoo(a);
    destroyFoo(b);
    
	printf("success");
    return 0;
}

Foo* createFoo (int arrSize, Foo* twin)
{
    Foo* t = (Foo*) malloc(sizeof(Foo));
    
    // initialize with default values
    t->number = 1;
    t->character = '1';
    
    // initialize the array
    t->arrSize = (arrSize>0?arrSize:10);
    t->arr = (float*) malloc(sizeof(float) * t->arrSize);
    
    // a Foo is a twin with only one other Foo
    t->myTwin = twin;
    if(twin) twin->myTwin = t;
    
    return t;
}

Foo* destroyFoo (Foo* foo)
{
    if (foo)
    {
        // we allocated the array, so we have to free it
        free(foo->arr);

        // to avoid broken pointer, we need to nullify the twin pointer
        if(foo->myTwin) ((Foo*)foo->myTwin)->myTwin = NULL;
    }

    free(foo);

    return NULL;
}
