Thread: Memory leak

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    What really annoyed me was why I couldn’t answer such a simple approach when it was questioned to me. You know how frustrating it is to just loose an opportunity just because I wasn't able to answer a simple answer :-/

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    6

    Smile

    Hi
    Code:
     void *debug_malloc(size_t bytes)
    {
       allocs++;
       return malloc(bytes);
    }
    In this code you are trying to increment the alloc value before allocating the actual memory for the pointer which gives wrong statistics when you analyse the debub prints finaly.same for free API also

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    6
    Hi Harish..
    What is your comment on this?
    Is this question asked in any interviews?
    what you replied for this?

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by vinothkumar View Post
    Hi
    Code:
     void *debug_malloc(size_t bytes)
    {
       allocs++;
       return malloc(bytes);
    }
    In this code you are trying to increment the alloc value before allocating the actual memory for the pointer which gives wrong statistics when you analyse the debug prints finaly.same for free API also
    I do agree that we could have checked the returned value of malloc before incrementing the alloc. Which would go along with your statistics. Although i guess, I just looking for vague how people can think on it.

    Although the malloc isn't thread safe, I would say having wrapper function, counter in the function to keep count on the allocations and deallocation and mutex to be sync with the multi thread would rather be the solution for all. Well there we go; all this i should thought in 30 sec time. Which I missed out, and a big loss in not getting into a big company.

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-28-2006, 01:06 PM
  2. Memory Leak Help
    By (TNT) in forum Windows Programming
    Replies: 3
    Last Post: 06-19-2006, 11:22 AM
  3. Do I have a memory leak? (SDL)
    By antex in forum Game Programming
    Replies: 3
    Last Post: 03-02-2006, 06:18 PM
  4. Is this a memory leak?
    By jasonhv in forum C++ Programming
    Replies: 5
    Last Post: 10-20-2005, 08:37 PM
  5. A memory leak (I think...)
    By Stevek in forum C++ Programming
    Replies: 7
    Last Post: 03-16-2003, 03:09 PM