A
Aaron Walker
I am trying to write this piece of code that reads a config file, in the
format:
ENTRY value
and I want to read the config file, then read the "value" into the
appropriate member of the following structure:
struct conf_data {
char *root;
char *pid_file;
char *log_dir;
char *local_addr;
unsigned int port;
unsigned short child_min;
unsigned short child_max;
};
typedef struct conf_data Config;
I want the 4 char pointers to be dynamically allocated at runtime,
depending on the length of the "value" in the config file.
I have a fuction, get_conf_data that is run from main():
Config *get_conf_data(void)
{
Config *conf;
...
return conf;
}
My question is, do I need to allocate memory for the structure first
then the members, like:
conf = malloc(sizeof(Config));
conf->root = malloc(strlen(this_value));
conf->pid_file = malloc(strlen(that_value));
if I do need to do that way, then when I am free()ing, do I do it in the
opposite order?
int main()
{
Config *conf = get_conf_data();
...
free(conf->root);
free(conf->pid_file);
...
free(conf);
}
Or am I totally off-base here?
Thanks,
Aaron
format:
ENTRY value
and I want to read the config file, then read the "value" into the
appropriate member of the following structure:
struct conf_data {
char *root;
char *pid_file;
char *log_dir;
char *local_addr;
unsigned int port;
unsigned short child_min;
unsigned short child_max;
};
typedef struct conf_data Config;
I want the 4 char pointers to be dynamically allocated at runtime,
depending on the length of the "value" in the config file.
I have a fuction, get_conf_data that is run from main():
Config *get_conf_data(void)
{
Config *conf;
...
return conf;
}
My question is, do I need to allocate memory for the structure first
then the members, like:
conf = malloc(sizeof(Config));
conf->root = malloc(strlen(this_value));
conf->pid_file = malloc(strlen(that_value));
if I do need to do that way, then when I am free()ing, do I do it in the
opposite order?
int main()
{
Config *conf = get_conf_data();
...
free(conf->root);
free(conf->pid_file);
...
free(conf);
}
Or am I totally off-base here?
Thanks,
Aaron