By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,148 Members | 1,339 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,148 IT Pros & Developers. It's quick & easy.

structure member allocation

P: n/a
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

Nov 13 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
nrk
Aaron Walker wrote:
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>
Nov 13 '05 #2

P: n/a
Aaron Walker <ka*****@REMOVETHIScfl.rr.com> wrote in message news:<5a*********************@twister.tampabay.rr. com>...
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 !

Thanks,
Aaron

Nov 13 '05 #3

P: n/a
Ravi Uday wrote:
Aaron Walker <ka*****@REMOVETHIScfl.rr.com> wrote in message
news:<5a*********************@twister.tampabay.rr. com>...

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);


Aren't you forgetting something?\0
--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #4

P: n/a
<snip>
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);


Aren't you forgetting something?\0


Yes, I have..You need to add '1' inside the malloc to account for '\0'
Nov 13 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.