Thread: Memory Leak question

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    8

    Memory Leak question

    Rewrite the following statements accurately and explain why the given version is inaccurate and your version is accurate

    "Dereferencing memory leaks leads to dangling pointers.”

    Can somebody please help me how to write this sentense using right terminology. I am very confused here as we never dereference memory.

    Thanks.

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by sangit View Post
    Rewrite the following statements accurately and explain why the given version is inaccurate and your version is accurate

    "Dereferencing memory leaks leads to dangling pointers.”

    Can somebody please help me how to write this sentense using right terminology. I am very confused here as we never dereference memory.

    Thanks.
    My advice is to completely ignore the original statement as it is intentionally wrong and confusing. Do you know what a memory leak is? Do you know what a dangling pointer is? If so, just write about these two concepts and compare/contrast them. The concepts are different, but related.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    8

    Memory Leak

    Can anybody please tell me how memory leaks and dangling pointers are two aspects of the same problem?

    Thanks

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by sangit
    Can anybody please tell me how memory leaks and dangling pointers are two aspects of the same problem?
    Well... what is a memory leak and what is a dangling pointer? How do they come about?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Describe what you understand of it - then we can fill you in. That's first of all going to make it clear to us what you understand, and avoid us explaining what you already understand, and second, it will show that you are actually willing to work on solving the assignment, rather than simply trying to get someone to do it for you.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    8
    memory leak causes because of loosing the access path of it through the pointer,which was previously pointing to that location and now its value is changed. In other words, the pointer which was pointing to it became dangling and this caused this memory leak. In both cases the problem is related to memory ,but I don't understand how come the same problem and what is the problem?

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    That sounds like a pretty good (albeit a bit verbose) way to rewrite that sentence to make it true.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    You're on the right track.

    >> In other words, the pointer which was pointing to it became dangling and this caused this memory leak.

    Ultimately, you're going to have to pin down your definition of 'dangling' - depending on who you ask, you may get somewhat different answers. One could technically consider a NULL pointer to be dangling (it isn't really a valid address, after all), but I think that the overall consensus is that it is not.

    The bottom line is, a memory leak is strictly an accounting issue, while dangling memory is an matter of validity.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by sangit View Post
    In other words, the pointer which was pointing to it became dangling and this caused this memory leak.
    No -- by definition, a dangling pointer is pointing to FREE memory. So it cannot be a memory leak. You are seeing two sides of the same coin. In one case, you freed memory and then accessed it. In the other case, you did not free the memory, and did not access it.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Or to put it differently, you get a memory leak if you don't free memory that you should, and dangling pointers if you free memory that you shouldn't.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    You can't logically dereference a memory leak, as that would imply that you still have a valid pointer to derefence, in which case it hasn't leaked (yet). By time its a memory leak, its already been dereferenced, which is why it leaked, because it was dereferenced without being deallocated.

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    "Dereferenced" doesn't mean what your post implies. Although "a reference has been removed" might be a valid interpretation of the word, it's so strongly against common usage that only confusion can result.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  2. Memory Leak Help
    By (TNT) in forum Windows Programming
    Replies: 3
    Last Post: 06-19-2006, 11:22 AM
  3. Memory Leak
    By Berticus in forum C++ Programming
    Replies: 5
    Last Post: 07-20-2005, 05:11 PM
  4. Memory Address of Array Question
    By Zeusbwr in forum C++ Programming
    Replies: 3
    Last Post: 10-24-2004, 09:58 AM
  5. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 01:41 PM