jeudi 26 mars 2015

Allocating memory for nested structure pointer


I am using a C code generator that is creating header files with following structures:



typdef struct Place {
struct Forest {
int trees;
} *Forest;
} Place ;


And using them in a c++ project.


When I try to access Place.Forest->trees, I get a segfault because Place.Forest is a dangling pointer.


I can't properly malloc it because:


Place.Forest = malloc(sizeof(Place.Forest));


Will just return the size of the pointer.


How do I allocate memory for Forest without changing Place or un-nesting Forest?


Modifying the structures is impracticable due to the large amount of code that is being automatically generated.




Aucun commentaire:

Enregistrer un commentaire