What I am doing wrong with c++ maps? (scary memory errors)
<p>I am new to C++ and I'm having trouble using a map correctly in class that I'm writing. Basically, when I make a new Block object in a test program and call its write method, the program totally shits itself and gives me a double free or corruption error. What is weird is that everything works fine if I uncomment that one line in the Block constructor. I'm guessing I'm missing some basic c++ knowledge but I'm not finding anything useful on google.</p>

<p>Block.h (includes not shown but they are there):</p>

<pre><code>namespace SSDSim{
    class Block{
        public:
            Block(uint block_num);
            ~Block(void);
            void read(uint page_num);
            void write(uint page_num, void *data);
            void erase(void);
        private:
            uint block_num;
            std::map&lt;uint, void *&gt; page_data;
    };
}
</code></pre>

<p>Block.cpp:</p>

<pre><code>#include "Block.h"

using namespace std;
using namespace SSDSim;

Block::Block(uint block){
    //page_data[4]= (void *) 0xfeedface;
    block_num= block;
}

...

void Block::write(uint page_num, void *data){
    if (page_data.find(page_num) == page_data.end()){
        page_data[page_num]= data;
    } else{
        cerr&lt;&lt;"Invalid write\n";
        exit(1);
    }
}
</code></pre>

<p>test.cpp:</p>

<pre><code>#include &lt;iostream&gt;
#include "../Block.h"

using namespace std;
using namespace SSDSim;

int main(void){
    Block b= Block(0);
    b.write(0, (void *) 0xdeadbeef);
    b.read(0);
//  b.read(4);
    return 0;
}
</code></pre>
