delete
to manually de-allocate the moemory space.int *n;
n = new int(17); //Initializes *n to 17
delete
int *p1;
p1 = new int;
//without initalization - not recommended.
//if cout << *p1; will get whatever was previously left there in the 4 bytes space. some unknown number.
MyClass *mcPtr;
mcPtr = new MyClass(32.0, 17);
delete p;
new
operators will be able to allocaate the memory spacedelete p;
p = NULL; //0
#include <iostream>
using namespace std;
int* testFunc3 (int a){ //return a pointer
int *p1; //p1 is a local variable
p1 = new int(a); //the address returned by new will not be automatically collected after the end of this function scope
//here, new operator allocates the memory dynamically/manually
cout << "Inside testFunc3, int value: "<< *p1 << " is allocated memory at address: " << p1 << endl;
return p1; //here return the pointer, i.e., return the memory address value
}
//after the end of this scope, local variables are automatically deleted,
int main() { //main function also has its scope
int *p;
p = testFunc3(10);
cout << "1 In main, p is pointing to int value: " << *p << " at address: " << p << endl << endl;
p = testFunc3(20); //here, we lost track of the previous memory address. this is a bad practice.
// Memory leak! //what is a better practice?
cout << "2 In main, p is pointing to int value: " << *p << " at address: " << p << endl << endl;
delete p; //release/de-allocate the memory space pointed to by pointer p back to the pool (freespace).
//p still stores the address at this time.
cout << "3 In main, p is pointing to int value: " << *p << " at address: " << p << endl << endl;
//undefined behaviour. the memory may be used by others.
p = NULL; // essentially change the memory address to zero. should do this immediately after delete p.
cout << "4 Pointer p is set to " << p << endl;
return 0;
}
//after the end of this scope, all variables are automatically removed.
//all memory addresses are released back to the operating system.
Inside testFunc3, int value: 10 is allocated memory at address: 0x7f8866c057d0
1 In main, p is pointing to int value: 10 at address: 0x7f8866c057d0
Inside testFunc3, int value: 20 is allocated memory at address: 0x7f8866c057e0
2 In main, p is pointing to int value: 20 at address: 0x7f8866c057e0
3 In main, p is pointing to int value: 20 at address: 0x7f8866c057e0 (still pointing to old address even when you deleted)
4 Pointer p is set to 0x0
Value is deallocated, but why does it still output?
delete p; //release/de-allocate the memory space pointed to by pointer p back to the pool (freespace).
//p still stores the address at this time.
cout << "3 In main, p is pointing to int value: " << *p << " at address: " << p << endl << endl;
//undefined behaviour. the memory may be used by others.
new
) is not properly de-allocated (using delete
). Even though the memory is no longer in use by the program, it is still "reserved"Without
delete
, nonew
operator can allocate the same address allocated by a previousnew
#include <iostream>
using namespace std;
int main() {
//Warning! Don't try this on yor own computer. :)
/*
do {
new double (10.0);
} while (true);
return 0;
*/
for(int i = 0; i < 200000000 ; i++){
new int (10);
}
return 0;
}
//At the end of the program, all memory addresses are de-allocated, including leaked memory, managed by the operating system.
delete
Server(); //Constructor
~Server(); //Destructor
#include <iostream>
class MyClass {
public:
MyClass() {std::cout <<"MyClass constructed\n";}
~MyClass() {std::cout <<"MyClass destroyed\n";}
};
class ClassWithPointer {
private:
int i;
int *ip;
MyClass *cp;
public:
ClassWithPointer();
~ClassWithPointer();
};
ClassWithPointer::ClassWithPointer() {
i = 10;
ip = new int(20);
cp = new MyClass;
}
ClassWithPointer::~ClassWithPointer() {
// without this code the destructor won't work?
delete ip;
ip = NULL; //not very necessary, because no code will use this pointer after this anyway
delete cp;
cp = NULL; //not very necessary, because no code will use this pointer after this anyway
}
int main () {
ClassWithPointer testObj;
return 0;
}