newint* ptr = new int;
*ptr = 42;
cout << *ptr;This creates a variable on the heap and stores 42 there.
Always free heap memory to prevent leaks:
delete ptr;int* arr = new int[5];
arr[0] = 1;
arr[1] = 2;
// ...
delete[] arr; // use delete[] for arraysImportant: Use delete[] when freeing dynamic arrays.
If you allocate memory but never deallocate it, you create a memory leak:
int* leak = new int;
// forgot to call delete leak;This will stay in memory until the program exits.
nullptr)std::unique_ptr, std::shared_ptr) in modern C++nullptr is the recommended way to represent null pointers in C++11 and later:
int* p = nullptr;Ask the AI if you need help understanding or want to dive deeper in any topic