Click to See Complete Forum and Search --> : memleak in map


adapanaidu
April 17th, 2007, 04:31 AM
#include <iostream>
#include <map>
using namespace std;

int main()
{
map < int , string > sampMap;
map < int , string > sampMap1;
sampMap[1] = "Adams";
sampMap[2] = "Peter";
map < int , map< int , string >> tempMap;
tempMap[1] = sampMap;
sampMap1 = tempMap[1];
sampMap1[3] = "Thomson";
tempMap[1] = sampMap1;
}


Will this program cause any memory leak?
As we are assigning( over writing ) sampMap1 to tempMap[1], will the tempMap loose the reference of sampMap and produce any memory leak?

laitinen
April 17th, 2007, 04:38 AM
Will this program cause any memory leak?

No.


Regards,

Laitinen

GNiewerth
April 17th, 2007, 04:49 AM
Memory leaks can only be created when allocating memory from the heap. Since you donīt do that and map is assumed not to create any leaks, there will be no memory leaks.

Regards,
Guido

Zaccheus
April 17th, 2007, 06:00 AM
The map class and the string class automatically free the memory they use.

adapanaidu
April 18th, 2007, 07:04 AM
Thank U Guys. :)