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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:24:17+00:00 2026-05-11T06:24:17+00:00

I am observing strange behaviour of std::map::clear(). This method is supposed to call element’s

  • 0

I am observing strange behaviour of std::map::clear(). This method is supposed to call element’s destructor when called, however memory is still accessible after call to clear().

For example:

struct A {   ~A() { x = 0; }   int x; };  int main( void ) {   std::map< int, A * > my_map;   A *a = new A();   a->x = 5;   my_map.insert( std::make_pair< int, *A >( 0, a ) );    // addresses will be the same, will print 5   std::cout << a << ' ' << my_map[0] << ' ' << my_map[0]->x << std::endl;    my_map.clear();    // will be 0   std::cout << a->x << std::endl;    return 0; } 

The question is, why is variable a still accessible after its destructor was called by map::clear()? Do I need to write delete a; after calling my_map.clear() or is it safe to overwrite the contents of a?

Thanks in advance for your help, sneg

  • 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. 2026-05-11T06:24:18+00:00Added an answer on May 11, 2026 at 6:24 am

    std::map does not manage the memory pointed to by the pointer values – it’s up to you to do it yourself. If you don’t want to use smart pointers, you can write a general purpose free & clear function like this:

    template <typename M> void FreeClear( M & amap )      for ( typename M::iterator it = amap.begin(); it != amap.end(); ++it ) {         delete it->second;     }     amap.clear(); } 

    And use it:

    std::map< int, A * > my_map; // populate FreeClear( my_map ) 

    ;

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

Sidebar

Related Questions

I started dealing with NSOperations and (as usual with concurrency) I'm observing strange behaviour.
I'm observing a very strange behaviour with an entity class and loading an object
I'm observing some strange behavior when using the F# interactive interpreter. Running the following
I am trying to get key-value-observing to work for an NSMutableArray. Below is the
I'm experiencing a strange issue where my Visual Studio 2005 C++ program crashes the
My JS saves some string data to JSON using stringify(), but observing the outputted
I'm trying to fix a problem that only recently happens on a number of
i have the below url http://bit.ly/cDdh1c When you place the above url in a
I'm trying to make a 4x4 sudoku solver in Python (I'm only a beginner!)
My question is essentially a simple one, though I'm looking for as in-depth an

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.