Thread: Receursive, Good or bad???

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    22

    Receursive, Good or bad???

    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....

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Sounds like homework to me... Check your book. I gaurantee the answers are in there.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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.

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    22
    Sppedy5, what book re you referring to ? Do you have any idea???

    Thanks Prelude...

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    22
    >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?

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    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.

  7. #7
    Registered User
    Join Date
    Apr 2004
    Posts
    22
    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....

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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.

  9. #9
    Registered User
    Join Date
    Apr 2004
    Posts
    22
    >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'.

  10. #10
    Registered User
    Join Date
    Apr 2004
    Posts
    22

    Memory Cleanup in C

    Anybody wants to take it further.....I want to know, how will one do memory cleanup in C?

    Thanks in advance

  11. #11
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    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.

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Oh this one is GOOD!
    By sean in forum A Brief History of Cprogramming.com
    Replies: 74
    Last Post: 11-11-2008, 02:30 AM
  2. Something about probablility
    By mike_g in forum A Brief History of Cprogramming.com
    Replies: 116
    Last Post: 03-13-2008, 05:33 PM
  3. Question about atheists
    By gcn_zelda in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 08-11-2003, 11:50 AM
  4. data loss bad bad bad
    By RoD in forum Tech Board
    Replies: 4
    Last Post: 05-01-2003, 12:06 PM
  5. good news and bad news
    By Garfield in forum A Brief History of Cprogramming.com
    Replies: 25
    Last Post: 10-27-2001, 07:31 AM