A
Alfonso Morra
Hi,
I have the ff data types :
typedef enum {
VAL_LONG ,
VAL_DOUBLE ,
VAL_STRING ,
VAL_DATASET
}ValueTypeEnum ;
typedef union {
long lval ;
double fval ;
char* sval ;
void* ptr ;
} Value ;
typedef struct {
int magic ;
int version ;
}Header ;
typedef struct {
char label[20] ;
id int ;
}Key ;
typedef struct {
Header *hdr ;
char *subject ;
int subject_len ;
Key key ;
ValueTypeEnum type ;
Value value ;
int text_len ;
int size ;
}MotherStruct ;
If I have a variable declared as ff:
MotherStruct *pMS = calloc(1,sizeof(MotherStruct*)) ;
1). Do I have to allocate memory seperately for each individual nested
pointer in the structure (i.e. hdr and subject?). I guess the answer is
yes - but I just need to be sure (see question 3 below).
2). How do I calculate the size (in bytes) of the structure MotherStruct
- so that I can use a function like memcpy to copy this memory block to
another memory block ?
3). Finally, (this is related in a way to the first question). If I just
call free on pMS is all the memory that was allocated freed (is the
answer the same if I have to issue seperate calls to allocate memory to
the individual nested pointers - i.e. will a single call to free(pMs)
free any memory allocated to pointers nested within the structure - OR
do I need to free each of the nested pointers before finally freeing the
memory block pointed to by pMS ?
Tkx
I have the ff data types :
typedef enum {
VAL_LONG ,
VAL_DOUBLE ,
VAL_STRING ,
VAL_DATASET
}ValueTypeEnum ;
typedef union {
long lval ;
double fval ;
char* sval ;
void* ptr ;
} Value ;
typedef struct {
int magic ;
int version ;
}Header ;
typedef struct {
char label[20] ;
id int ;
}Key ;
typedef struct {
Header *hdr ;
char *subject ;
int subject_len ;
Key key ;
ValueTypeEnum type ;
Value value ;
int text_len ;
int size ;
}MotherStruct ;
If I have a variable declared as ff:
MotherStruct *pMS = calloc(1,sizeof(MotherStruct*)) ;
1). Do I have to allocate memory seperately for each individual nested
pointer in the structure (i.e. hdr and subject?). I guess the answer is
yes - but I just need to be sure (see question 3 below).
2). How do I calculate the size (in bytes) of the structure MotherStruct
- so that I can use a function like memcpy to copy this memory block to
another memory block ?
3). Finally, (this is related in a way to the first question). If I just
call free on pMS is all the memory that was allocated freed (is the
answer the same if I have to issue seperate calls to allocate memory to
the individual nested pointers - i.e. will a single call to free(pMs)
free any memory allocated to pointers nested within the structure - OR
do I need to free each of the nested pointers before finally freeing the
memory block pointed to by pMS ?
Tkx