1.15.2.  Operators new and delete

[ fromfile: pointers.xml id: newdelete ]

Example 1.28. src/pointers/newdelete/ndsyntax.cpp

#include <iostream>
using namespace std;

int main() {
 int* ip = 0;               1
 delete ip;                 2
 if(ip) cout << "non-null" << endl;
 else cout << "null" << endl;
 ip = new int;              3
 int* jp = new int(13);     4
 //[...]      
 delete ip;                 5
 delete jp;
}

1

null pointer

2

has no effect at all - ip is still null.

3

allocate space for an int

4

allocate and initialize

5

Without this, we have a memory leak.




[12] We discuss this situation in an article on our Website.