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 4 4062
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>
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
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
<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' This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Mannequin* |
last post by:
Hi all,
I'm working on a quick program to bring the Bible into memory from a
text file. Anyway, I have three questions to ask.
First, is my implementation of malloc () correct in the program to...
|
by: John |
last post by:
In the course of an assignment, I learned the hard way that I shouldn't try
to free a malloc'd member of a malloc'd structure after having freed that
structure (i.e., free( structure ); free(...
|
by: damian birchler |
last post by:
Hi
I'm wondering of what type a structure is. Of course, it is a
_structure_, but an array isn't an _array_ either. So of what type is
a structure? I'd say a pointer, am I right?
|
by: ranjeet.gupta |
last post by:
Dear All !!
Before i qoute my querry, I will like to qoute my analysis and my
Knowledge
Struct a {
int raw;
char data;
};
|
by: ulyses |
last post by:
I'm trying to put pointer to flexible array of structures in other
structure. I want to have pointer to array of pixels in screen
structure. Here is mine code, but I think it isn't quite all right:...
| |
by: Kavya |
last post by:
Here is the code
int main(){
struct node{
int a;
int b;
int c;
};
struct node s={3,5,6};
struct node *ptr=&s;
|
by: dtschoepe |
last post by:
Hi,
I have a homework project I am working on, so be forwarned, I'm new to
C programming. But anyway, having some trouble with a memory
allocation issue related to a char * that is a variable...
|
by: Jason Doucette |
last post by:
I am converting a C-style unit into a C++ class. I have an
implementation function that was defined in the .cpp file (so it was
hidden from the interface that exists in the .h file). It uses a...
|
by: rahul |
last post by:
How is the memory allocated for structures? I need to optimize the
memory usage and bit fields are not doing the trick.
Any details about the memory allocation for the structures would be a...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |