malloc - how do I allocate memory for some of the structure elements -


i want allocate memory elements of structure, pointers other small structs.how allocate , de-allocate memory in best way?

ex:

 typedef struct _some_struct {     pdatatype1   pdatatype1;     pdatatype2   pdatatype2;     pdatatype3   pdatatype3;      .......      pdatatype12   pdatatype12; } some_struct, *psome_struct; 

i want allocate memory pdatatype1,3,4,6,7,9,11.can allocate memory single malloc? or best way allocate memory these elements , how free whole memory allocated?

there is trick allows single malloc, has weighed against doing more standard multiple malloc approach.

if [and only if], once datatypen elements of some_struct allocated, not need reallocated in way, nor any other code free on of them, can following [the assumption pdatatypen points datatypen]:

psome_struct alloc_some_struct(void) {     size_t siz;     void *vptr;     psome_struct sptr;      // note: optimizes down single assignment     siz = 0;     siz += sizeof(datatype1);     siz += sizeof(datatype2);     siz += sizeof(datatype3);     ...     siz += sizeof(datatype12);      sptr = malloc(sizeof(some_struct) + siz);      vptr = sptr;     vptr += sizeof(some_struct);      sptr->pdatatype1 = vptr;     // either initialize struct pointed sptr->pdatatype1 here or     // caller should -- likewise others ...     vptr += sizeof(datatype1);      sptr->pdatatype2 = vptr;     vptr += sizeof(datatype2);      sptr->pdatatype3 = vptr;     vptr += sizeof(datatype3);      ...      sptr->pdatatype12 = vptr;     vptr += sizeof(datatype12);      return sptr; } 

then, when you're done, free(sptr).

the sizeof above should sufficient provide proper alignment sub-structs. if not, you'll have replace them macro (e.g. sizeof) provides necessary alignment. (e.g.) 8 byte alignment, like:

#define sizeof(_siz) (((_siz) + 7) & ~0x07) 

note: while possible this, , more common things variable length string structs like:

struct mystring {     int my_strlen;     char my_strbuf[0]; };  struct mystring {     int my_strlen;     char *my_strbuf; }; 

it debatable whether it's worth [potential] fragility (i.e. forgets , realloc/free on individual elements). cleaner way embed actual structs rather pointers them if single malloc high priority you.

otherwise, the [more] standard way , 12 individual malloc calls and, later, 12 free calls.

still, is viable technique, particularly on small memory constrained systems.

here [more] usual way involving per-element allocations:

psome_struct alloc_some_struct(void) {     void *vptr;     psome_struct sptr;      sptr = malloc(sizeof(some_struct));      // either initialize struct pointed sptr->pdatatype1 here or     // caller should -- likewise others ...     sptr->pdatatype1 = malloc(sizeof(datatype1));     sptr->pdatatype2 = malloc(sizeof(datatype2));     sptr->pdatatype3 = malloc(sizeof(datatype3));     ...     sptr->pdatatype12 = malloc(sizeof(datatype12));      return sptr; }  void free_some_struct(psome_struct sptr) {      free(sptr->pdatatype1);     free(sptr->pdatatype2);     free(sptr->pdatatype3);     ...     free(sptr->pdatatype12);      free(sptr); } 

Comments

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -