mercredi 4 mars 2015

Memcpy leading to block freed twice error

I have a map which I am filling as needed. To get the contents of the map out into another variable I am using memcpy.


If the memcpy line is commented, I see the output print correctly displayed. 100 => 1234 Fruits


If I uncomment the memcpy, I see the below error. The previously seen print does not get displayed as well.


block freed twice


Exited: ExitFailure 127



#include <iostream>
#include <map>
#include <string>

struct apple
{
int iValue;
std::string str1;
};

typedef std::map<int, apple> AppleMap;

int main()
{
AppleMap one;
apple structaa;
structaa.iValue = 1234;
structaa.str1 = "Fruits";
int value = 100;
one[value]=structaa;

AppleMap::iterator it = one.find(100);
if(it != one.end())
{
std::cout << it->first << " => " << it->second.iValue << '\t' << it->second.str1 << '\n';
}
else
{
std::cout << "Value Not Found\n";
}

apple structbb;
memcpy(&structbb, &(it->second), sizeof(apple));

return 0;
}


Any help on what I have done wrong?


Aucun commentaire:

Enregistrer un commentaire