C++ Tutorial

Class and Object

Reference

Inheritance and Derivation

Polymorphism and Virtual Functions

Operator Overloading

Template

Exception

Object Oriented Advanced

Input/Output Stream

File Operations

new And delete Operators in C++

In C++, memory management is essential to ensure efficient use of resources. Dynamic memory allocation allows programs to allocate memory during runtime. The new and delete operators in C++ are used for dynamic memory allocation and deallocation, respectively. In this tutorial, we'll learn how to use these operators for memory management.

  • new Operator:

The new operator is used to allocate memory for an object or an array of objects during runtime. The general syntax for the new operator is:

pointer_variable = new data_type;

Here's an example of using the new operator to allocate memory for an integer:

int* p = new int;

When you use the new operator to allocate memory for an array, you need to specify the size of the array:

pointer_variable = new data_type[array_size];

Here's an example of using the new operator to allocate memory for an array of integers:

int* arr = new int[5];
  • delete Operator:

The delete operator is used to deallocate memory that was previously allocated using the new operator. It's essential to release memory when it is no longer needed to prevent memory leaks. The general syntax for the delete operator is:

delete pointer_variable;

Here's an example of using the delete operator to deallocate memory for an integer:

int* p = new int;
// ... use p ...
delete p;

When you use the delete operator to deallocate memory for an array, you need to use the [] operator:

delete[] pointer_variable;

Here's an example of using the delete operator to deallocate memory for an array of integers:

int* arr = new int[5];
// ... use arr ...
delete[] arr;
  • Example:

Let's see an example that demonstrates the use of new and delete operators:

#include <iostream>

int main() {
    // Allocate memory for an integer
    int* p = new int;
    *p = 42;

    // Print the value and address of the dynamically allocated integer
    std::cout << "Value: " << *p << ", Address: " << p << std::endl;

    // Deallocate the memory
    delete p;

    // Allocate memory for an array of integers
    int* arr = new int[3];
    arr[0] = 10;
    arr[1] = 20;
    arr[2] = 30;

    // Print the values and addresses of the elements in the dynamically allocated array
    for (int i = 0; i < 3; ++i) {
        std::cout << "Value: " << arr[i] << ", Address: " << &arr[i] << std::endl;
    }

    // Deallocate the memory for the array
    delete[] arr;

    return 0;
}

In this example, we first allocate memory for an integer, print its value and address, and then deallocate the memory. After that, we allocate memory for an array of integers, print their values and addresses, and then deallocate the memory.

That's it for our tutorial on the new and delete operators in C++. By using these operators, you can manage memory allocation and deallocation in your programs, ensuring efficient use of resources and preventing memory leaks.

  1. Dynamic memory allocation in C++:

    • Description: Introduces the concept of dynamic memory allocation and its importance in managing memory at runtime.
    • Example Code:
      #include <iostream>
      
      int main() {
          // Dynamically allocate an integer
          int* dynamicInt = new int;
      
          // Assign a value to the dynamically allocated integer
          *dynamicInt = 42;
      
          // Use the dynamically allocated integer
          std::cout << "Dynamic Integer: " << *dynamicInt << std::endl;
      
          // Don't forget to free the allocated memory
          delete dynamicInt;
      
          return 0;
      }
      
  2. Array allocation with new and delete in C++:

    • Description: Illustrates how to dynamically allocate and deallocate arrays using new[] and delete[].
    • Example Code:
      #include <iostream>
      
      int main() {
          // Dynamically allocate an array of integers
          int* dynamicArray = new int[5];
      
          // Assign values to the dynamically allocated array
          for (int i = 0; i < 5; ++i) {
              dynamicArray[i] = i * 10;
          }
      
          // Use the dynamically allocated array
          for (int i = 0; i < 5; ++i) {
              std::cout << "Element " << i << ": " << dynamicArray[i] << std::endl;
          }
      
          // Don't forget to free the allocated memory for arrays
          delete[] dynamicArray;
      
          return 0;
      }