I noticed something here in this code . even if when push back the elements in vector (see function passtoit). Destructor of struct test is being called . can anyone please explain this to me .. why this behavior is there ...
when i declare the std::vector
of static object and let it run it gives me that heap corruption problem and when i declare the test as std::vector
of test pointer(*) and delete that pointer as shown in the commented code , it works fine. please explain . it will help me alot . i don't know what else to write here in order to make the stackworkflow understand that it is valid question
#include "stdafx.h"
#include <vector>
#include <iostream>
#include <vld.h>
using namespace std;
class declaration
class testHelper
{
public:
testHelper(int passed):m(passed){}
int m;
~testHelper(){cout<<"deleting as thought";}
};
declaration of structure
struct test
{
// constructor
test(string name,testHelper* help):name(name),dummy(help){}
string name;
testHelper *dummy;
// destructor
~test()
{
cout<<"deleting dummy";
if(dummy!=NULL)
{
delete dummy;
dummy =NULL;
}
}
};
function to pass
int passtoit()
{
std::vector<test> x;
// push back on the vector
x.push_back(test("prakash",(new testHelper(10))));
//for(std::vector<test>::iterator i =x.begin();i!=x.end();++i)
//{
// delete *i;
//}
return 0;
}
main function
int _tmain()
{
// calling the function
passtoit();
return 0;
}