malloc vs new & free vs delete
#include <iostream>
#include <crtdbg.h>
class Test
{
public:
Test()
{
std::cout << "Constructor" << std::endl;
}
Test(int Init)
: mInt(Init)
{
std::cout << "Constructor" << std::endl;
}
~Test()
{
std::cout << "Destructor" << std::endl;
}
int mInt = 0;
};
int main()
{
Test* pMalloc;
pMalloc = (Test*)(malloc(sizeof(Test)));
pMalloc->mInt = 10;
Test* pMallocArr;
pMallocArr = (Test*)(malloc(sizeof(Test) * 4));
for (int i = 0; i < 4; i++)
{
pMallocArr[i].mInt = i;
std::cout << pMallocArr[i].mInt << std::endl;
}
free(pMalloc);
free(pMallocArr);
Test* pNew = new Test(10);
Test* pNewArr = new Test[4];
delete pNew;
delete[] pNewArr;
_CrtDumpMemoryLeaks();
return 0;
}