How to Delete a Pointer in C++? Last Updated : 19 Feb, 2024 Comments Improve Suggest changes Like Article Like Report In C++, memory management is essential to avoid memory leaks, crashes, and undefinable behavior. In this article, we will learn how to properly delete a pointer in C++. Deleting a Pointer in C++By deleting a pointer, we refer to deleting the memory allocated to the pointer. To delete a pointer in C++ we can use the delete keyword and for arrays allocated with new, use delete[] to deallocate the memory. After, deleting or deallocating the memory, we should assign the nullptr to the pointer. Syntax to Delete the Pointer in C++For deleting the pointer variable use: delete pointerName;For deleting the pointer array use: delete[] arrayPointer;Also, if you have used the malloc() or calloc() function to allocate the memory, then use the free() function for deallocation. C++ Program to Delete a PointerThe below example demonstrates how we can delete a pointer properly in C++. C++ // C++ program to show how to delete a pointer properly #include <iostream> using namespace std; int main() { // Allocate memory for a single integer int* dynamicInt = new int; // Check if the pointer is not null before deleting if (dynamicInt != nullptr) { // Use the allocated memory *dynamicInt = 42; // Display the value cout << "Value of dynamicInt: " << *dynamicInt << endl; // Properly delete the pointer delete dynamicInt; dynamicInt = nullptr; // Set to null after deletion to avoid dangling // pointer } return 0; } OutputValue of dynamicInt: 42 Note: Undefined behavior occurs when attempting to remove a single point more than one time. So, before deleting the pointer, always check that it is not null . Comment More infoAdvertise with us Next Article How to Delete a Pointer in C++? D dikshashu9v51 Follow Improve Article Tags : C++ Programs C++ cpp-pointer C++-new and delete CPP Examples +1 More Practice Tags : CPP Similar Reads How to Create a Smart Pointer in C++? A smart pointer in C++ simulates a pointer while also providing automatic garbage collection as it deallocates or frees associated memory when it goes out of scope, which helps prevent memory leaks and dangling pointers. In this article, we will learn how to create a smart pointer in C++. Creating a 4 min read How to Delete a File in C++? C++ file handling allows us to manipulate external files from our C++ program. We can create, remove, and update files using file handling. In this article, we will learn how to remove a file in C++. Delete a File in C++ To remove a file in C++, we can use the remove() function defined inside the 2 min read How to Create a shared_ptr in C++? A std::shared_pointer is a smart pointer introduced in C++11 that manages the lifetime of a dynamically allocated object through reference counting. In this article, we will learn how to create a shared_pointer. shared_ptr in C++A std::shared_pointer can be created by using the std::make_shared meth 2 min read How to Create a Pointer to a Function in C++? In C++, a function pointer is a variable that stores the address of a function that can later be called through that function pointer. It is useful for passing functions as parameters to other functions(callback functions) or storing them in data structures. In this article, we will learn how to use 2 min read Data type of a Pointer in C++ A pointer is a variable that stores the memory address of an object. The pointer then simply âpointsâ to the object. The type of the object must correspond with the type of the pointer. Pointers are used extensively in both C and C++ for three main purposes: To allocate new objects on the heap.To pa 5 min read How to Clear a Stack in C++? In C++, clearing a stack means removing all element from the stack container leaving it empty. In this article, we will learn how to clear a stack in C++.The most efficient method to clear a stack is by assigning the new empty stack to our original stack container. Let's take a look at the code exam 3 min read How to Delete an Element from a Priority Queue in C++ ? In C++, a priority queue is a queue in which elements are arranged based on their priority values as each element has a priority value associated with it. In this article, we will learn how to delete an element from a priority queue in C++. For Example, Input: myPriorityQueue = {8, 6, 7, 5, 4, 2, 3} 2 min read How to Delete an Element from the Set in C++? In C++, the set container stores unique elements in the sorted order and the deletion operation should not disrupt this sorted order.C++ provides a built-in function set erase(), which can be used to easily delete the given element by passing its value to the function. Let's take a look at the simpl 1 min read How to Delete an Element from a Set in C++? A set in C++ is a container that stores unique elements in a sorted order. In this article, we will learn how to delete a specific element from a set. Example: Input: mySet = {5, 2, 8, 1, 4} Element to delete: 2 Output: mySet = {5, 1, 8, 4}Delete an Element from a Set in C++To delete a specific elem 2 min read How to Delete a Key-Value Pair from a Map in C++? In C++, maps are used to store key-value pairs in which each key is unique. In this article, we will learn how to delete a key-value pair from a map in C++. Example Input: mp={ {1,"One"}, {2,"Two"},{3,"Three"}}Key= 2Output: Map after deleting key: 1: One3: ThreeRemove a Key-Value Pair from Map in C+ 2 min read Like