C++ new Keyword
Example
Reserve memory for an int with new:
int* ptr = new int;
*ptr = 35;
cout << *ptr;
delete ptr;
Definition and Usage
The new keyword reserves dynamic memory. Dynamic memory is memory that you create while the program runs, and free yourself when you are done with it.
new returns a pointer to the new memory. Use new[] to create an array, like new int[3].
Always free the memory with delete (or delete[] for arrays) when you do not need it anymore. If you forget it, the program uses more and more memory. This is called a memory leak.
Related Pages
Read more about dynamic memory in our C++ new and delete Tutorial.