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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:16:01+00:00 2026-05-24T01:16:01+00:00

This crashes at runtime. std::map<std::string, MyClass> myMap; myValue = new MyClass(); myMap[myKey] = *myValue;

  • 0

This crashes at runtime.

std::map<std::string, MyClass> myMap;
myValue = new MyClass();
myMap["myKey"] = *myValue;

I have 2 requirements:

  1. That instances of MyClass are held on the heap (hence use of new);
  2. That I be able to reference these via an associative array (hence use of std::Map).

Why can I not use the dereference operator succesfully in the example? How can I fulfill both at once?

PS. I’m using gcc.

  • 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-24T01:16:01+00:00Added an answer on May 24, 2026 at 1:16 am

    If you lose the scope of myValue then it’s a memory leak. So it’s better to store the MyClass* in your map.

    std::map<std::string, MyClass*> myMap;
    myValue = new MyClass();
    myMap["myKey"] = myValue;
    

    In given example, also make sure you delete the element while erasing or removing from the map<>. You can use smart pointer (e.g. boost::shared_ptr) if you don’t want to worry about memory management.

    Also, from your given example I don’t know why it should crash while dereferencing *myClass. Are you doing some weird stuff in copy constructor MyClass::MyClass(const MyClass&) ?

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

Sidebar

Related Questions

std::string x(x); This crashes very badly on my compiler. Does this mean I should
I have a DLL with some COM objects . Sometimes, this objects crashes and
This question comes from my experience with the following question: https://stackoverflow.com/questions/492748/new-responses-icon-on-so-crashes-ie7-closed In that question,
The problem is that I always get runtime crashes at any method invoked at
so I have a UILabel subclass that only crashes every so often on first
I have an assignment that I need to create a custom C type string
I've been struggling with this for a while now, and this code crashes with,
my app crashes often in this for-loop: for (int a = 0; a <=
I'm trying to get this simple program to work on windows, but it crashes:
I have this simple c++ program #include <cstdlib> #include <iostream> #include <math.h> #include <stdlib.h>

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.