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

  • Home
  • SEARCH
  • 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 6820985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:29:28+00:00 2026-05-26T21:29:28+00:00

For C and C++, linked list with a pointer pointing to its head node.

  • 0

For C and C++, linked list with a pointer pointing to its head node. But, all nodes are allocated on heap by malloc() or new. When the head pointer runs out of its scope e.g. its function exits, all nodes allocated on heap will be lost. Right? Is this a memory leak?

How C/C++ handle this kind of issue? It calls deallocator automatically? (e.g. free() or delete)?

  • 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-26T21:29:29+00:00Added an answer on May 26, 2026 at 9:29 pm

    The better way to handle this sort of thing is to use a standard container, instead of some homespun thing, unless you have good reason & know what you’re doing, and why…

    std::vector<>
    std::list<>

    But to choose a container, it’s important to know what you’re doing, what the lifetime is supposed to be.

    @Jack – no matter what you do, standard containers don’t magically take care of manually allocated objects for you. That is fundamentally impossible.

    You must change your approach to the problem to perceive the problem as “manually allocated objects.” Once you make this leap, and realize that this is a bad way to go, then you can choose between “they’re value-objects” or “they’re to be managed by shared_ptr”.

    EX1: using shared_ptr to hold new’d objects (this is the approach to use if copying around MyNode is a bad idea [performance, owned resources, preserved state]):

    void MyFunction()
    {
      typedef boost::shared_ptr<MyNode> NodePtr;
      std::list<NodePtr> my_list;
      my_list.push_back(NodePtr(new MyNode(args...)));
      my_list.push_back(NodePtr(new MyNode(args...)));
      ...
      // when this function exits, the nodes, which are owned by shared_ptr's
      // which are themselves owned by a stack instance of std::list<> 
      // will be automatically deleted, no leaks anywhere...
    }
    

    EX2: This is what you do if your nodes are cheap, can be treated as copyable objects (value semantics):

    void MyFunction()
    {
      std::vector<MyNode> my_list;
      my_list.push_back(MyNode(args...));
      my_list.push_back(MyNode(args...));
      ...
      // when this function exits, the nodes, which are shored directly as copies
      // in the vector container, will be automatically deleted, no leaks anywhere...
    }
    

    And if you really would rather manage the instances manually for some reason (usually you do this because the lifetimes are not really tied to a single container’s lifetime, but have some irregular lifecycle that cannot be encapsulated neatly by anything but a custom algorithm):

    void MyFunction()
    {
      std::list<MyNode*> my_list;
      my_list.push_back(new MyNode(args...));
      my_list.push_back(new MyNode(args...));
      ...
      // we must manually deallocate the nodes because nothing else has been given
      // responsibility anywhere (we're manually managing them)
      typedef std::list<MyNode*>::iterator iterator;
      for (iterator it = std::begin(my_list), end = std::end(my_list); it != end; ++it)
        delete *it;  // manually releases our allocated memory from the heap
      // the "head" is still deleted automatically because it is stack allocated
      // (the list object)
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey all. I'm doing a linked list exercise that involves dynamic memory allocation, pointers,
I am reading XOR linked list (from Wikipedia).But I am having some problems in
I create a linked list dynamically and initialize the first node in main(), and
Say you have a linked list structure in Java. It's made up of Nodes:
I want to initialize a linked list with pointer arguments like so: /* *
I have a simple doubly linked list example that I'm working on, but for
Ok so let's say we have a linked list of characters with a head
I'm writing a linked list and I want a struct's destructor (a Node struct)
We are all taught that you MUST free every pointer that is allocated. I'm
I have a generic linked list implementation with a node struct containing a void*

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.