![]() |
In C++, releasing memory means deallocating the memory that was previously allocated by the user. It is very important to avoid memory leaks. Other programming languages like Java support automatic garbage collection to release the dynamically allocated memory, but in C++ we have to release the allocated memory manually. In this article, we will learn how to release memory in C++. Free the Allocated Memory in C++In C++, if the dynamic memory allocation is done using the Syntax to Release Memory in C++//For single elements delete object_name //For arrays Here, object_name can be a pointer or any other data object in C++.
It is also important to note that we can also allocate memory in C++ using malloc() and calloc() (inherited from C language). This allocated memory should be freed by the function free(). C++ Program to Release Memory in C++The following program illustrates how we can release memory for dynamically allocated array in C++.
Output 0 1 2 3 4 Memory for array released Time Complexity: O(1) where N is the size of the array. We can also use smart pointers (like |
Reffered: https://www.geeksforgeeks.org
C++ |
Related |
---|
![]() |
![]() |
![]() |
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 12 |