473,508 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

freeing memory in shortest time

Hello,

I have a pointer to a main structure which again consists of
structures, enums, char, int, float and again complex structures.

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). Is there any idea to free all the contents of the structure in
shortest possible time.
Mar 5 '08 #1
11 1978
Or probably should i switch to static memory instead??? In that case
what care should be taken.

Pl advice
Mar 5 '08 #2
vivek wrote:
Hello,

I have a pointer to a main structure which again consists of
structures, enums, char, int, float and again complex structures.

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). Is there any idea to free all the contents of the structure in
shortest possible time.
You might try making the structure static. Or you might try having a
separate cache of all the pointers to dynamic memory for each structure
element. This will consume additional memory, but will eliminate the
need to traverse the structure to free all it's components.

In anycase, I doubt that traversing the structure is going to be your
most serious bottleneck, particularly if you do it all at once.

Mar 5 '08 #3
vivek said:
Hello,

I have a pointer to a main structure which again consists of
structures, enums, char, int, float and again complex structures.

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). Is there any idea to free all the contents of the structure in
shortest possible time.
Well, exit(0) will do it. But I'm curious to know why you think it's taking
a lot of time. Computers are pretty fast nowadays. Is this really the
bottleneck for your program? What did profiling tell you?

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Mar 5 '08 #4
Oh, it is for an embedded system, where that freeing the structure
takes about 0.4 ms, which is a lot of time for an embedded
system...Thats why!!!
Mar 5 '08 #5
"you might try having a
separate cache of all the pointers to dynamic memory for each structure
element. This will consume additional memory, but will eliminate the
need to traverse the structure to free all it's components.

In anycase, I doubt that traversing the structure is going to be your
most serious bottleneck, particularly if you do it all at once"
Can you explain in detail about the caching, please? I could not
follow that part.

Mar 5 '08 #6
vivek wrote:
Oh, it is for an embedded system, where that freeing the structure
takes about 0.4 ms, which is a lot of time for an embedded
system...Thats why!!!
Okay. Some of your options are:

1. Don't free anything. Obviously this might not be feasible.
2. Use static or automatic objects. Static objects will persist
throughout the program lifetime while auto objects will be destroyed
once their scope is exited.
3. Use something like alloca. See the recent threads on this. Objects
allocated with alloca persist throughout the function and need no
explicit call to free.
4. Modify your data structures so that dynamic memory is minimised
or "clumped" together, thus minimising the number calls to free.

There may be other strategies too, though we can't tell you which one
might be suitable since the choice depends on various factors of your
program and it's host machine and what's expected of them.

Generally speaking, if you want realtime performance then malloc/free is
pretty much ruled out, but I would suggest that you actually verify
that they *are* the problem and that the time consumed *is*
unacceptable before considering other strategies.

Mar 5 '08 #7
Of course everything else is ok with the program
functionalities...except for the time...If time is ok...thats
it..project over...

and thank u so much santhosh
Mar 5 '08 #8
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
Mar 5 '08 #9
The structures are nested.

I have measured the time taken for this specific code section to run.

The lifetime of the structures would be like this.

Receive some communication data, use these structures to decode data -
allot memory...after decoding, process it ..then free memory..

then prepare a reply, encode the date(create these structures-allot
memory), send the data and free memory.
Mar 5 '08 #10
santosh wrote:
vivek wrote:
>I have a pointer to a main structure which again consists of
structures, enums, char, int, float and again complex structures.

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). Is there any idea to free all the contents of
the structure in shortest possible time.

You might try making the structure static. Or you might try
having a separate cache of all the pointers to dynamic memory
for each structure element. This will consume additional memory,
but will eliminate the need to traverse the structure to free
all it's components.
Something is seriously wrong. The type has nothing to do with
freeing. You simply free the object. The compiler knows what the
type, and thus the size, of that object is.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.

--
Posted via a free Usenet account from http://www.teranews.com

Mar 5 '08 #11
"vivek" <gv********@gmail.comwrote in message
news:48**********************************@34g2000h sz.googlegroups.com...
Hello,

I have a pointer to a main structure which again consists of
structures, enums, char, int, float and again complex structures.
None of these require freeing individually. Only if there are pointers to
other things (perhaps the complex structures you mentioned).
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).
Why do have to determine the data type at runtime? Do you have elements
which sometimes are pointers and sometimes something else? Or only some
pointers need freeing?
>Is there any idea to free all the contents of the structure in
shortest possible time.
Perhaps post the struct definition here so we can see the problem areas.
But, if the struct will always be the same size and is reused frequently,
perhaps just keep it allocated -- unless you are very short of memory. But
even there might be tricks to share the memory without
allocating/deallocating.

--
Bart
Mar 5 '08 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
22959
by: disco | last post by:
I am working on this example from a book "C Primer Plus" by Prata 4th edition - p. 672. There is no erata on this problem at the publisher's website. 1) Is it a violation of copyright laws to...
11
2261
by: Rodrigo Dominguez | last post by:
there are sometimes that I use third party libraries, I use some functions that returns char * or structs, etc. sometimes the memory that is returned by those libraries, when I try to free this...
6
2747
by: Fernando Cacciola | last post by:
Help me out here please: While watching Brad Abraham's MSDN TV talk about the Dispose pattern, refering to: public virtual void Dispose ( bool disposing ) { if ( disposing ) { <-- WHAT...
4
36510
by: Atul Sureka | last post by:
Hi, I want to free the object memory in C# - like we do using 'delete' keyword in C++. Lets say I have an object of some class and I want to explicitly free the memory. C# do not have any free...
5
3140
by: Amogh | last post by:
Hi, My question is related to setting freed pointers to NULL. After freeing a pointer: 1) Should the freeing routine also be responsible for setting the pointer to null? 2) Or, should the...
2
2249
by: Chris | last post by:
I have a Bayesian simulation package that keeps running into memory allocation problems. I have a feeling this has something to do with Python (2.5.1.1) not freeing memory. The program essentially...
3
1650
by: Berteun Damman | last post by:
Hello, I have programmed some python script that loads a graph (the mathemical one with vertices and edges) into memory, does some transformations on it, and then tries to find shortest paths in...
66
3635
by: karthikbalaguru | last post by:
Hi, Will 'free' return the memory Immediately to the OS ? Thx in advans, Karthik Balaguru
25
4680
by: Andreas Eibach | last post by:
Hi again, one of the other big woes I'm having... typedef struct perBlockStru /* (structure) Long words per block */ { unsigned long *lword; } lwperBlockStru_t;
0
7132
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7336
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7063
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7504
tracyyun
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...
0
5640
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5059
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3211
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...
0
3196
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
773
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.