Home › C++ Tutorial › C++ memory management
C++ memory management
new and delete.
new allocates on the heap and delete frees it (delete[] for arrays). Prefer smart pointers such as unique_ptr in modern code.
Example code
#include <iostream>
#include <memory>
using namespace std;
int main() {
int *p = new int(42);
cout << *p << endl;
delete p;
auto u = make_unique<int>(7);
cout << *u << endl;
return 0;
}