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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:54:37+00:00 2026-05-28T04:54:37+00:00

Possible Duplicate: What is The Rule of Three? How exactly does std::pair call destructors

  • 0

Possible Duplicate:
What is The Rule of Three?

How exactly does std::pair call destructors for its components? I am trying to add instances of a class to an std::map, but I am getting errors regarding the destructor of my class.

I have narrowed down my question/problem to the following extremely simple example.

Below, my_class merely creates an int array at construction, and deletes it at destruction. Somehow I am getting a “double delete” error:

//my_class.h
class my_class {
  public:
    int an_int;
    int *array;

    //constructors:
    my_class()
    {
      array = new int[2];
    }
    my_class(int new_int) : an_int(new_int)
    {
      array = new int[2];
    }

    //destructor:
    ~my_class()
    {
      delete[] array;
    }
};  //end of my_class

Meanwhile, over in main.cpp…

//main.cpp
int main(int argc, char* argv[])
{
  std::map<int, my_class>   my_map;

  my_map.insert( std::make_pair<int, my_class> (1, my_class(71) ) );

  return 0;
} // end main

Compilation goes fine, but this generates the following runtime error:

*** glibc detected *** ./experimental_code: double free or corruption (fasttop):

Or, with valgrind:

==15258== Invalid free() / delete / delete[] / realloc()
==15258==    at 0x40249D7: operator delete[](void*) (vg_replace_malloc.c:490)
==15258==    by 0x8048B99: main (my_class.h:38)
==15258==  Address 0x42d6028 is 0 bytes inside a block of size 8 free'd
==15258==    at 0x40249D7: operator delete[](void*) (vg_replace_malloc.c:490)
==15258==    by 0x8048B91: main (my_class.h:38)

(line numbers are off because I cut out comments and stuff)

I must be missing something about std::pair…?

Thanks to all in advance!

  • 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-28T04:54:38+00:00Added an answer on May 28, 2026 at 4:54 am

    When you add my_class to stl containers the copy constructor is called. As you don’t define one it does a memberwise copy and two my_class objects are created that point to the same int array, When these are deleted the same int array might be deleted twice

    Please take a look at Rule of three

    In C++11 also look at move constructor if you are worried about the efficiency.

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

Sidebar

Related Questions

Possible Duplicate: How does the Google Did you mean? Algorithm work? Suppose you have
Possible Duplicate: Properties vs Methods Is there any rule or general best practice as
Possible Duplicate: Css and javascript Not Loading in rewrite rule After using only rewrite
Possible Duplicate: Learning to write a compiler I looked around trying to find out
Possible Duplicate: Is there some ninja trick to make a variable constant after its
Possible Duplicate: Reading through file using ifstream I'm trying to find a way to
Possible Duplicate: What good does zero-fill bit-shifting by 0 do? (a >>> 0) I
Possible Duplicate: What does the @ symbol before a variable name mean in C#?
Possible Duplicate: Rule of thumb for choosing an implementation of a Java Collection? I
Possible Duplicate: css rule to disable text selection highlighting On my homepage i have

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.