c - Allocate and free memory of an array from structure -


i have following structure:

typedef struct {     char *name[10];     char *msg[100]; } log; 

how can free name , msg arrays log structure? know free used dynamic allocation, , dynamic allocation doesn't work in structures. can do?

tryed give error:

typedef struct {     char *name[] = (char *)malloc(sizeof(char) * 10);     char *msg[] = (char *)malloc(sizeof(char) * 100); } log; 

who can me? thanks!

declaring structure doesn't allocate memory members. memory allocated when instance of structure created.

typedef struct {     char *name[10];     char *msg[100]; } log;   

doesn't allocate memory name , msg, declare log new data (user defined) type. when create instance of

log log_flie; 

memory allocated name , msg. can allocate memory (dynamically) elements of data member

for(int = 0; < 10; i++)     log_file.name[i] = malloc(n);  //n desired size   

similarly can msg.

to free dynamically allocated memory, call free

for(int = 0; < 10; i++)     free(log_file.name[i]);     

if want allocate memory name , msg dynamically follows

typedef struct {     char **name;     char **msg; } log;   

then

log log_file; log_file.name = malloc(10 * sizeof(char *)); log_file.msg = malloc(100 * sizeof(char *));   

Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -