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 3422346
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:12:20+00:00 2026-05-18T06:12:20+00:00

I have the code: std::list<Node *> lst; //…. Node * node = /* get

  • 0

I have the code:

std::list<Node *> lst;
//....
Node * node = /* get from somewhere pointer on my node */;
lst.remove(node);

Does the std::list::remove method call the destructor (and free memory) of each removed element? If so, how I can avoid 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-18T06:12:21+00:00Added an answer on May 18, 2026 at 6:12 am

    Yes, removing a Foo* from a container destroys the Foo*, but it will not release the Foo. Destroying a raw pointer is always a no-op. It cannot be any other way! Let me give you several reasons why.

    Storage class

    Deleting a pointer only makes sense if the pointee was actually allocated dynamically, but how could the runtime possibly know whether that is the case when the pointer variable is destroyed? Pointers can also point to static and automatic variables, and deleting one of those yields undefined behavior.

    {
        Foo x;
        Foo* p = &x;
    
        Foo* q = new Foo;
    
        // Has *q been allocated dynamically?
        // (The answer is YES, but the runtime doesn't know that.)
    
        // Has *p been allocated dynamically?
        // (The answer is NO, but the runtime doesn't know that.)
    }
    

    Dangling pointers

    There is no way to figure out whether the pointee has already been released in the past. Deleting the same pointer twice yields undefined behavior. (It becomes a dangling pointer after the first delete.)

    {
        Foo* p = new Foo;
    
        Foo* q = p;
    
        // Has *q already been released?
        // (The answer is NO, but the runtime doesn't know that.)
    
        // (...suppose that pointees WOULD be automatically released...)
    
        // Has *p already been released?
        // (The answer WOULD now be YES, but the runtime doesn't know that.)
    }
    

    Uninitialized pointers

    It is also impossible to detect whether a pointer variable has been initialized at all. Guess what happens when you try to delete such a pointer? Once again, the answer is undefined behavior.

        {
            Foo* p;
    
            // Has p been properly initialized?
            // (The answer is NO, but the runtime doesn't know that.)
        }
    

    Dynamic arrays

    The type system does not distinguish between a pointer to a single object (Foo*) and a pointer to the first element of an array of objects (also Foo*). When a pointer variable is destroyed, the runtime cannot possibly figure out whether to release the pointee via delete or via delete[]. Releasing via the wrong form invokes undefined behavior.

    {
        Foo* p = new Foo;
    
        Foo* q = new Foo[100];
    
        // What should I do, delete q or delete[] q?
        // (The answer is delete[] q, but the runtime doesn't know that.)
    
        // What should I do, delete p or delete[] p?
        // (The answer is delete p, but the runtime doesn't know that.)
    }
    

    Summary

    Since the runtime cannot do anything sensible with the pointee, destroying a pointer variable is always a no-op. Doing nothing is definitely better than causing undefined behavior due to an uninformed guess 🙂

    Advice

    Instead of raw pointers, consider using smart pointers as the value type of your container, because they take responsibility for releasing the pointee when it is no longer needed. Depending on your need, use std::shared_ptr<Foo> or std::unique_ptr<Foo> . If your compiler does not support C++0x yet, use boost::shared_ptr<Foo>.

    Never, I repeat, NEVER EVER use std::auto_ptr<Foo> as the value type of a container.

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

Sidebar

Related Questions

I have following piece of code: customObject* object; std::list<customObject> objects; for(int i = 0;
I have this code #include <iostream> using namespace std; int main(int argc,char **argv) {
I have the below code in stdafx.h. using namespace std; typedef struct { DWORD
I have std::list<Info> infoList in my application that is shared between two threads. These
I have code like this: class MapIndex { private: typedef std::map<std::string, MapIndex*> Container; Container
i have following code #include <iostream> #include <set> #include <string> using namespace std; template<class
The following code leads to a memory leakage: std::list<float*> vertices; float* v; for (int
When I compile my code for a linked list, I get a bunch of
I have code that references a web service, and I'd like the address of
I have code to create another row (div with inputs) on a button click.

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.