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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:09:30+00:00 2026-05-22T22:09:30+00:00

In trying to patch memory leaks among other things in a side project I’ve

  • 0

In trying to patch memory leaks among other things in a side project I’ve totally confused myself with pointers and lists and maps and memory, etc.

I want to create a list of objects to use throughout the programs life. But I also want to use a map to quickly access individual objects from that list through their unique id. I figured I could have a map of pointers to the objects in the list to cut down on memory size.

Is this possible?

I’ve been working on test code that looks like:

list<cObject> mylist;
map<int, ciEntity*> mymap;

void main(void)
{
    int x = 0;
    class cObject *temp;

    for(x = 0; x < 10; x++)
    {
        temp = new cObject;
        temp->name = new char[25];
        strcpy(temp->name, "Test");
        temp->id=x;
        mylist.push_back(*temp);
        // now what with the map?
        delete temp;
    }

}  

I’ve had other ideas too messing around with the map declaration. I’ve tried using an iter to go through the list and then mymap[id]=iter or similar variations. I’ve had zero luck getting anything to work. I know my fundamentals aren’t where they should be in regards to working with memory. Any help is appreciated!

  • 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-22T22:09:31+00:00Added an answer on May 22, 2026 at 10:09 pm

    I would alter this a little bit, and use something like a std::shared_ptr<cObject> in both your list and your map. You can, for the map, use either an int, or possibly a std::string for the key-type, and then for the value-type, use the std::shared_ptr<cObject>

    So your code would look more like:

    using namespace std;
    
    list<shared_ptr<cObject> > mylist;
    map<int, shared_ptr<cObject> > mymap;
    
    void main(void)
    {
        int x = 0;
    
        for(x = 0; x < 10; x++)
        {
            std::shared_ptr<cObject> temp = shared_ptr(new cObject);
            temp->name = new char[25];
            strcpy(temp->name, "Test");
            temp->id=x;
            mylist.push_back(temp);
    
            // now what with the map?
            mymap[x] = temp;
    
            //no need to delete temp since it's a managed pointer-type
        }
    
    }
    

    If for some reason your compiler doesn’t have std::shared_ptr, you can also get it from boost.

    The nice thing with this approach is that your list and map are now pointing to the same object, so if you change the object in the list, the changes will also be reflected in the map. Also the shared_ptr object will manage the life-time of the pointer through reference counting, so once there are no more references to the pointer, it will call delete on the pointer without you having to worry about cleaning up the pointers from each container (and avoiding ownership issues of the pointers as well).

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

Sidebar

Related Questions

I spent the last 4 hours trying to set up Eclipse TPTP memory profiling
I'm trying to find a least-resistance path from C# to C++, and while I
I am trying to return the physical file path of a database's mdf/ldf files.
I am trying to get the path to a folder in my website root
I am currently trying out this code: NSString *path = [[NSBundle mainBundle] pathForResource:@dream ofType:@m4a];
In a WinApp I am simply trying to get the absolute path from a
I'm trying to call an Antlr task in my Ant build.xml as follows: <path
Where would set_include_path(../../); point to? I am trying to run some PHP code on
I'm trying to compile a simple program, with #include <gtkmm.h> The path to gtkmm.h
Trying to setup an SSH server on Windows Server 2003. What are some good

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.