C++ delete运算符

C++ delete运算符

delete运算符被用来释放在C++语言中程序的运行时使用new运算符、calloc和malloc()函数等动态创建的内存空间。换句话说,delete运算符用于从堆中释放数组和非数组(指针)对象,这些对象是new运算符动态分配到堆内存上的变量。我们可以在程序中使用delete运算符或delete [ ]运算符来删除已释放的空间。delete运算符的返回类型为void,因此它没有返回值。

C++ delete运算符

delete运算符的语法

我们可以使用delete运算符来删除特定的元素或变量,如下所示:

delete pointer_variable;
// delete ptr; It deallocates memory for one element

同样地,我们可以使用delete []运算符删除分配的内存空间块。

delete [ ] pointer_variable; 
// delete [] ptr; It deallocate for an array

使用delete运算符释放每个变量的内存位置的程序

让我们考虑一个示例,使用delete运算符从堆内存中删除每个变量的分配内存空间。

Program1.cpp

#include 
using namespace std;
int main ()
{   
    // declaration of variables
    int *ptr1, *ptr2, sum;

    // allocated memory space using new operator
    ptr1 = new int; 
    ptr2 = new int;

    cout << " Enter first number: ";
    cin >> *ptr1;
    cout << " Enter second number: ";
    cin >> *ptr2;
    sum = *ptr1 + *ptr2;
    cout << " Sum of pointer variables = " << sum;

    // delete pointer variables
    delete ptr1; 
    delete ptr2;
    return 0;
}

输出

Enter first number: 5
 Enter second number: 8
 Sum of pointer variables = 13 

使用delete []运算符删除数组对象的程序

让我们创建一个程序,使用C++中的delete []运算符删除动态创建的数组对象的内存空间。

Program2.cpp

#include 
using namespace std;
int main ()
{
    // declaration of variables
    int *arr, max_num, i;

    cout << " Enter total number of elements to be entered : ";
    cin >>max_num;

    // use new operator to declare array memory at run time
    arr = new int [max_num];

    cout << " Enter the numbers: \n";
    for (i = 0; i< max_num; i++)  // input array from user
    {
        cout << " Number " << i+1 << " is ";
        cin >> arr[i];
    }

    cout <<" Numbers are : ";
    for (i = 0; i < max_num; i++)
    {
        cout << arr[i] << "\t";
     } 

     // use delete operator to deallocate dynamic memory
     delete [ ] arr;
     return 0;
}

输出

Enter total number of elements to be entered : 7
 Enter the numbers:
 Number 1 is 45
 Number 2 is 600
 Number 3 is 78
 Number 4 is 93
 Number 5 is 29
 Number 6 is 128
 Number 7 is 32
 Numbers are : 45       600     78      93      29      128     32

使用delete运算符删除空指针的程序

考虑一个使用C++编程语言中的delete运算符删除空指针的程序。

Program3.cpp

#include 
using namespace std; 
int main ()
{
// initialize the integer pointer as NULL 
int *ptr = NULL;

// delete the ptr variable 
delete ptr;
cout << " The NULL pointer is deleted.";
return 0;
}

输出

The NULL pointer is deleted.

使用删除操作符删除有或没有值的指针

让我们考虑一个示例,使用C++中的删除操作符删除一个有或没有值的指针变量。

Program4.cpp

#include 
using namespace std;
int main ()
{
// Use new operator to create dynamic memory
int *ptr = new int;

// Use new operator to dynamic memory space for an array 
int *ptr2 = new int (10);
cout << " The value of ptr is: "<< *ptr << " \n ";
cout << " The value of ptr2 is: "<< *ptr2 << " \n ";

// use delete keyword to delete the value stored in *ptr and *ptr2
delete ptr;
delete ptr2;
return 0;
}

输出

The value of ptr is: 1415071048
  The value of ptr2 is: 10

使用malloc函数分配动态内存,然后使用delete运算符删除

让我们考虑一个使用malloc函数创建动态内存并使用delete运算符删除在C++编程语言中分配的内存的示例。

Program6.cpp

#include 
using namespace std;
int main ()
{
// create a dynamic memory using malloc() function
char *str = (char *) malloc (sizeof (char));
cout << " Dynamic memory is deleted using the delete operator. " << endl;
delete str; // use delete operator to delete the referencing pointer
return 0;
}

输出

Dynamic memory is deleted using the delete operator.

删除用户定义数据类型变量的程序

让我们写一个程序来演示使用delete操作符删除用户定义对象。

Program7.cpp

#include 
using namespace std;

struct Ptr
{
    static void operator delete (void *ptr1, std::size_t sz)
    {
cout << " Custom deletion of size " << sz <

输出

Custom deletion of size 1
 Custom deletion of size 24

使用delete操作符删除空指针的程序

让我们创建一个程序,使用C++中的delete操作符释放空指针的内存空间。

Program8.cpp

#include 
using namespace std;
int main ()
{
    // declare a void pointer 
    void *str;
cout << " The void pointer is deleted using the delete operator. " <

输出

The void pointer is deleted using the delete operator.

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程