On 5 Mar, 08:47, vivek <gvivek2...@gmail.comwrote:
I have a pointer to a main structure which again consists of
structures, enums, char, int, float and again complex structures.
are these nested structures or pointed to?
/* headers ommitted */
struct Main1_s
{
struct Nested_s nested;
};
struct Main2_s
{
struct Pointed_s *pointed;
};
void func (void)
{
struct Main1_s *main1_s;
struct Main2_s *main2_s;
/* should check for malloc() failures */
main1_s = malloc(sizeof *main1_s);
main2_s = malloc(sizeof *main2_s);
main2_s->pointed = malloc(sizeof (*main2_s->pointed));
/* do stuff */
/* cleanup */
delete (main1_s);
/* nested freed automatically */
delete (main2_s->pointed);
delete (main2_s);
}
When i free all the contents of the main structure, it takes me a lot
of time (since i have to loop determining the data type and freeing
it).
what sort of time? Do you mean the program takes a long time?
How do you know, have you timed it? Or is it taking a long
time to write the program? Why do you need a loop to determine the
data type. We need to see some code!
Is there any idea to free all the contents of the structure in
shortest possible time.
not sure what you mean. Could you use automatic storage?
void func (void)
{
struct Main1_s main1_s;
/* do stuff */
}
no mallocs therefore no deletes
and then vivek wrote:
Or probably should i switch to static memory instead??? In that case
what care should be taken.
I'm not sure. What lifetime does your structure need? Statics can
occupy storage unnecessarily (they go away only when the program
ends).
You may make them accessible from too much of the program.
You might have problems that there is only one copy of the struct
when logically you need many.
I need to know more about what you are doing.
Can you post a simpified example?
--
Nick Keighley
The Dinosaurs have come and gone, we Theriodonts remain