What are pros and cons of recursive function and can we use them in embedded system, which runs under low memory?
And I also would like to know, how would one avoid memory leakage in C?
Thanks in advance....
What are pros and cons of recursive function and can we use them in embedded system, which runs under low memory?
And I also would like to know, how would one avoid memory leakage in C?
Thanks in advance....
Sounds like homework to me... Check your book. I gaurantee the answers are in there.
>and can we use them in embedded system, which runs under low memory?
Recursion wouldn't be my first choice of algorithm for a low memory system. Stack frames tend to take up plenty of space. If you can get away with a non-recursive solution that doesn't need a stack then do so. Otherwise you'll need to weigh the the pros and cons of defining your own stack, thus only stacking what you need, but not knowing in advance how big it must be (resulting in wasted space for the stack itself) or relying on the simulated stack of recursion where it is only as big as the depth of the recursion, but you stack everything instead of just what you need to solve the problem (resulting in wasted space for the stacked items).
>how would one avoid memory leakage in C?
By careful coding and intelligent testing/debugging. The simplest way is to increment a counter with every malloc and decrement with every free. If the counter is 0 at the end then theoretically, all memory that you allocated has been freed.
My best code is written with the delete key.
Sppedy5, what book re you referring to ? Do you have any idea???
Thanks Prelude...
>By careful coding and intelligent testing/debugging. The >simplest way is to increment a counter with every malloc and >decrement with every free. If the counter is 0 at the end then >theoretically, all memory that you allocated has been freed.
I understand that careful testing or debugging can be used to avoid leakage.
Let me phrase the question in other way, how do code in order to avoid leakage, if debugging and testing for memory leakage is not an option?
For example, increment on malloc and decrement on free gives me some non-zero value at the end. What would have been a problem that gives non-zero value and how to avoid that?
If it's greater than zero then you forgot to free something. If it's less than zero then you freed too many times.
By careful coding I mean using a convention such as writing the call to free when you write the call to malloc, making sure that any cleanup functions are called if malloc and free are not used in the same function, and just keeping a close watch on your memory usage in general. There's no fix-all that will save you from memory leaks, you just have to be careful.
My best code is written with the delete key.
How about implementing Garbage collector in C? How will it work? I heard GC( Garbage Collector) is low priority thread running in background and find out which pointer is not freed and frees automatically.
Is there any algorithm that finds unused pointer and freeing up the same? ANd on the top,how does GC finds pointers which are not being used?
Anu pointers to this is greatly appreciated.
Thanks,
Be Happy and always....
>How about implementing Garbage collector in C? How will it work?
It depends on how complex you want it to be. The simplest garbage collection scheme I can think of in C would be maintaining a free list of memory and doling out chunks where needed. Then when the program terminates, the free list is released. You can add complex logic to determine whether a chunk isn't being used anymore and return it to the free list before termination though.
>Is there any algorithm that finds unused pointer and freeing up the same?
Yes. A google search will turn up quite a few of them too.
>ANd on the top,how does GC finds pointers which are not being used?
It depends on the method being used.
My best code is written with the delete key.
>How about implementing Garbage collector in C? How will it work?
It depends on how complex you want it to be. The simplest garbage collection scheme I can think of in C would be maintaining a free list of memory and doling out chunks where needed. Then when the program terminates, the free list is released. You can add complex logic to determine whether a chunk isn't being used anymore and return it to the free list before termination though.
I agree, say my application is starving for memory at run time. And there is leakage and how would I free the unused memory so that my appplication can run normally.
Please give some suggestion on different methods of memory clean up in 'C'.
Anybody wants to take it further.....I want to know, how will one do memory cleanup in C?
Thanks in advance
There really isn't a way to go "oh I have a memory leak at 0x4383403 let me clean it up".
One particular method you could use is to have a list of pointers that were recieved from memory allocations and the number of pointers pointing to each location. When the number of pointers reaches 0 you free that location.
>say my application is starving for memory at run time. And there is leakage and how would I free the unused memory so that my appplication can run normally.
If you're not using the memory then it should have already been freed. In applications where memory is scarce you should be releasing any memory that you allocate the instant you don't need it anymore as well as relying more on logic than data structures where possible. Finding and fixing a leak is exactly the same as I explained above: careful coding and well designed debug code.
Your problem might not be that memory is getting tight, but that the chunk of memory assigned to your application has become too fragmented to be useful. That's where you need to consider redesigning your memory management code to prevent fragmentation.
My best code is written with the delete key.