structure member allocation

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
 
N

nrk

Aaron said:
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));

Yes. You need to allocate memory for the structure pointer first, and only
then you can assign to members in the structure pointed to by it (makes
sense, doesn't it?). However note that:
a) conf = malloc(sizeof *conf);
is the clc preferred way of doing this malloc. This avoids coding the type
information of conf in more than one place unnecessarily.

b) conf->root = malloc(strlen(this_value) + 1);
conf->pid_file = malloc(strlen(that_value) + 1);
Do *NOT* *EVER* forget the terminating NUL at the end of c-style strings!!
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);

It doesn't have to be strictly in opposite order (as in you can switch the
above two statements around if you want).
...
free(conf);
}

However, you must free all the pointer members inside the structure that
before freeing the pointer to the structure itself.

-nrk.

<snip>
 
R

Ravi Uday

Aaron Walker said:
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:
Yes if you declare it as a pointer.
conf = malloc(sizeof(Config));
conf = malloc(sizeof *conf );/* make sure you include <stdlib.h> */

Note: Check on the return type of 'malloc' always.
conf->root = malloc(strlen(this_value));

What is 'this_value' declared as ? In case its a string, then -

conf->root = malloc(strlen(this_value)*sizeof *conf->root );

conf->pid_file = malloc(strlen(that_value));

conf->pid_file = malloc(strlen(that_value) * sizeof *conf->pid_file);

if I do need to do that way, then when I am free()ing, do I do it in the
opposite order?

Yes.
int main()
{
Config *conf = get_conf_data();
...
free(conf->root);
free(conf->pid_file);
...
free(conf);/* In case conf is a pointer. */
}

Or am I totally off-base here?
Post a least compilable code for us to comment more on that !
 
R

Richard Heathfield

Ravi said:
What is 'this_value' declared as ? In case its a string, then -

conf->root = malloc(strlen(this_value)*sizeof *conf->root );

conf->pid_file = malloc(strlen(that_value));

conf->pid_file = malloc(strlen(that_value) * sizeof *conf->pid_file);

Aren't you forgetting something?\0
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,273
Latest member
DamonShoem

Latest Threads

Top