Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6109329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:23:33+00:00 2026-05-23T14:23:33+00:00

The basic concept of memory leaking is a mismatch of a new/delete operation during

  • 0

The basic concept of memory leaking is a mismatch of a new/delete operation during code execution, either due to wrong coding practices or either in cases of errors when the delete operation is skipped.

But recently I was asked a question in an interview about other ways in which memory can leak.
I had no answer to it. What is it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:23:33+00:00Added an answer on May 23, 2026 at 2:23 pm

    Common dynamic memory problems are:

    • Dynamic memory allocation with new and not deallocating with delete.
    • Dynamic memory allocation with new[] and deallocating with delete.
    • Dynamic memory allocation new and deallocate it with free.
    • Dynamic memory allocation malloc and deallocate it with delete.

    In addition to memory leaks/memory corruption the last 3 scenarios will cause the dreaded Undefined Behavior.

    A few other potential memory leak causing scenarios that I can recollect are:

    • If a pointer, pointing to a dynamically allocated memory region is re-assigned a new value before being deallocated, it will lead to a dangling pointer and a memory leak.

    A code example:

    char *a = new[128];
        char *b = new[128];
        b = a;
        delete[]a;
        delete[]b; // will not deallocate the pointer to the original allocated memory.
    

    – Pointers in STL Containers

    A more common and often encountered scenario is, Storing pointers pointing to dynamically allocated types in STL containers. It is important to note that STL containers take ownership of deleting the contained object only if it is not a pointer type.
    One has to explicitly iterate through the container and delete each contained type before deleting the container itself. Not doing so causes a memory leak.
    Here is an example of such an scenario.

    – The Non virtual Base class destructor problem

    Deleting an pointer to Base class which points to any dynamically allocated object of derived class on heap. This results in an Undefined Behavior.

    An code example:

    class MyClass
    {
        public:
        virtual void doSomething(){}
    }; 
    class MyClass2 : public MyClass 
    { 
        private:  
            std::string str;  
        public: MyClass2( std::string& s) 
        {  
            str=s; 
        }  
        virtual void doSomething(){}
    };  
    
    int main()
    {  
         std::str hello("hello"); 
         MyClass * p = new MyClass2(hello);  
         if( p ) 
         { 
            delete p;  
         } 
         return 0;
    }
    

    In the example only the destructor MyClass::~MyClass() gets called and MyClass2::~MyClass2() never gets called. For appropriate deallocation one would need,

    MyClass::virtual ~MyClass(){}
    

    – Calling delete on a void pointer

    A code example:

    void doSomething( void * p ) 
    {
        //do something interesting
        if(p)
           delete p; 
    }
    
    int main()
    {
        A* p = new A();
        doSomething(p);
        return 0;
    }
    

    Calling delete on a void pointer as in above example, will cause a memory leak and a Undefined Behavior.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I made some code, for understanding the concept/basic of pointer: int a=1; int *b=&a;
What is the basic concept behind WaitHandle in C# .net threading? Whats is its
I know about the basic concept of virtual function and run-time call. But i
I understand it is a very basic concept in the oops. But still I
Names and objects have been simplified for clarity's sake. The basic concept remains the
I was reading through Apple's doc of Basic Memory Management Rules. I came across
This is a very basic concept, but something I have never been able to
So I'm comfortable with the basic concept of CQS, where you might have a
I have a basic concept question about tomcat cluster. That is, if I have
I am comparing two algorithms, Prim's and Kruskal's. I understand the basic concept of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.