Introduction to C++ | free vs delete

C++ free vs delete

In C++, both free and delete are used to deallocate dynamically allocated memory, but they are used in different contexts. Understanding the difference between free and delete is crucial for proper memory management and avoiding issues such as memory leaks or undefined behavior.

What is free?

free is a C library function used to deallocate memory that was previously allocated by malloc, calloc, or realloc. It is not specific to C++ but works in C and C++ programs when working with raw memory allocations.

free only works with memory allocated using malloc, calloc, or realloc. It does not call the destructor for objects, and it cannot be used for memory allocated with new.

Syntax of free:

Syntax of free


        free(pointer);
                    

What is delete?

delete is a C++ keyword used to deallocate memory that was previously allocated with new. Unlike free, delete automatically calls the destructor of the object before releasing the memory. For arrays allocated with new[], delete[] should be used to properly deallocate the memory.

Using delete on a pointer that was allocated with malloc (or vice versa) can lead to undefined behavior, so it's important to match the correct deallocation method with the allocation method.

Syntax of delete:

Syntax of delete


        delete pointer;   // Deallocates a single object
        delete[] pointer; // Deallocates an array of objects
                    

Differences between free and delete

Feature free delete
Library C Standard Library (stdlib.h) C++ Keyword
Works with Memory allocated using malloc, calloc, or realloc Memory allocated using new or new[]
Destructor Call Does not call destructor Calls the destructor for objects before deallocating memory
Array Deallocation Cannot be used for arrays delete[] should be used for arrays allocated with new[]

Example of free

The following example demonstrates using free to deallocate memory allocated using malloc:

Code Example: free


        #include <iostream>
        #include <cstdlib> // for malloc and free
        using namespace std;
        
        int main() {
            // Dynamically allocate memory for an integer using malloc
            int* ptr = (int*) malloc(sizeof(int));
        
            if (ptr == NULL) {
                cout << "Memory allocation failed" << endl;
                return -1;
            }
        
            // Assign value to allocated memory
            *ptr = 10;
            cout << "Value: " << *ptr << endl;
        
            // Free the allocated memory
            free(ptr);
        
            return 0;
        }
                    

Output

Value: 10

Example of delete

Here’s an example of how to use delete to deallocate memory allocated using new:

Code Example: delete


        #include <iostream>
        using namespace std;
        
        int main() {
            // Dynamically allocate memory for an integer using new
            int* ptr = new int;
        
            // Assign value to allocated memory
            *ptr = 20;
            cout << "Value: " << *ptr << endl;
        
            // Delete the allocated memory
            delete ptr;
        
            return 0;
        }
                    

Output

Value: 20

Pro Tip:

💡 Pro Tip

Always ensure that the deallocation method matches the memory allocation method. If you use malloc or calloc for memory allocation, always use free to deallocate. If you use new or new[] for allocation, use delete or delete[], respectively. Mixing them (e.g., using delete for memory allocated with malloc) can cause undefined behavior.