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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T01:03:56+00:00 2026-06-17T01:03:56+00:00

I have a private field std::map<std::string, std::multiset<GraphObject*>>* the_Map; How can I allocate memory for

  • 0

I have a private field

std::map<std::string, std::multiset<GraphObject*>>* the_Map;

How can I allocate memory for it and insert a GraphObject?
Do I have to use the new operator?

the_Map = new map<string,multiset<GraphObject*>>;

And how can I insert a new GraphObject?
It’s part of a datastructure and I really need a pointer to a map.

  • 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-06-17T01:03:57+00:00Added an answer on June 17, 2026 at 1:03 am

    How can I allocate memory for it and insert a GraphObject?

    It doesn’t want to be a pointer at all; just make the map itself a member of the class and memory allocation will happen automatically.

    Correctly inserting an object is rather fiddly, since you’re storing pointers there too. If it doesn’t need to be a pointer, then storing objects would make your life much easier. If it really does have to be a pointer (e.g. because GraphObject is a polymorphic base class), I would recommend storing smart pointers: std::unique_ptr, or std::tr1::shared_ptr or boost::shared_ptr if you’re stuck in the past.

    If you really, really need to use raw pointers for some insane reason, then the closest you can get to an exception-safe insertion is probably:

    GraphObject * object = new Whatever(...);
    try {
        the_Map[key].insert(object);
    } catch(...) {
        delete object;
        throw;
    }
    

    or if you don’t care about the possiblity of memory leaks on insertion failure:

    the_Map[key].insert(new Whatever(...));
    

    Also don’t forget to delete each object when you remove it; that won’t happen automatically.

    I really need a pointer to a map.

    No you don’t. But if you really believe you do, and want to ignore everyone’s advice not to, then you’ll need an actual map to point to. I would recommend that you make this map a member of the class, so that its lifetime is managed automatically.

    If you really want to make life difficult for whoever maintains the code, then I suppose you could allocate one with new. In that case, remember to delete it when you’ve finished with it; probably in the class destructor. And if you do that, remember the Rule of Three and implement or delete the copy constructor and copy-assignment operator, since the default implementations will do the wrong thing.

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

Sidebar

Related Questions

like in java I have: Class.getSuperClass().getDeclaredFields() how I can know and set private field
I have a strange problem which I can't fix: A field: private boolean[][][] gaps;
I have a class IPAddressString that has: a private field of type String called
I have a simple class called String which has as a private field a
I have a class containing a SortedList<string, Data> as private field, where Data is
Normally when I have a private field inside a class or a struct, I
I have a private static readonly field in my class: public class MyClass {
If I have a boolean field like: private static final boolean DEBUG = false;
Why can NHibernate create a proxy for classes with properties that have private setters
I have a class derived from base class which has a private field. How

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.