Please have a look at the following code
#include <iostream>
using namespace std;
class Memory
{
private:
int *memory;
public:
Memory()
{
memory = new int[3];
for(int i=0;i<3;i++)
{
memory[i] = i;
cout << memory[i] << endl;
}
}
~Memory()
{
delete[] memory;
}
};
int main()
{
cout << "Running" << endl;
Memory m;
// do I have to invoke ~Memory() ?
int *secondMemory = new int[5];
//How to clear the memory of 'secondMemory' ?
system("pause");
return 0;
}
In here, I have cleared the memory of the dynamically allocated memory
array in the class's destructor. But my questions are
- do I have to invoke ~Memory() ?
- How to clear the memory of 'secondMemory' ?
These questions are asked as comments in the appropriate places of the code. Please help!
Edit
The problem here is, if I delete the memory of 'secondMemory' in main(), then the memory is gone as soon as it is allocated!