📜  在C++中删除关键字

📅  最后修改于: 2021-05-30 04:03:38             🧑  作者: Mango

Delete是一个运算符,用于销毁由新表达式创建的数组非数组(指针)对象

  • 可以通过使用Delete运算符Delete []运算符来使用Delete
  • New运算符用于动态内存分配,这会将变量放在堆内存中。
  • 这意味着Delete运算符从堆中释放内存。
  • 指向对象的指针不会被破坏,指针所指向的值或存储块将被破坏。
  • 删除运算符具有void返回类型,不返回值。

在下面的示例中,我们可以应用delete运算符:
1.删除数组对象:我们使用[]括号删除数组。

CPP
// Program to illustrate deletion of array
#include 
using namespace std;
 
int main()
{
    // Allocate Heap memory
    int* array = new int[10];
     
    // Deallocate Heap memory
    delete[] array;
 
    return 0;
}


CPP
// C++ program to deleting
// NULLL pointer
#include 
using namespace std;
 
int main()
{
    // ptr is NULL pointer
    int* ptr = NULL;
 
    // deleting ptr
    delete ptr;
 
    return 0;
}


CPP
#include 
using namespace std;
 
int main()
{
    // Creating int pointer
    int* ptr1 = new int;
     
    // Initializing pointer with value 20
    int* ptr2 = new int(20);
 
    cout << "Value of ptr1 = " << *ptr1 << "\n";
    cout << "Value of ptr2 = " << *ptr2 << "\n";
 
    delete ptr1; // Destroying ptr1
    delete ptr2; // Detroying ptr2
 
    return 0;
}


CPP
#include 
using namespace std;
int main()
{
    void* ptr; // Creating void pointer
 
    delete ptr; // Destroying void pointer
 
    cout << "ptr deleted successfully";
    return 0;
}


CPP
#include 
using namespace std;
 
int main()
{
    // Dynamic memory allocated by using malloc
    int* ptr2 = (int*)malloc(sizeof(int));
 
    delete ptr2;
 
    cout << "ptr2 deleted successfully";
 
    return 0;
}


CPP
#include 
using namespace std;
 
struct P {
    static void operator delete(void* ptr, std::size_t sz)
    {
        cout << "custom delete for size " << sz <


CPP
#include 
using namespace std;
 
int main()
{
    int x;
 
    // Delete operator always
    // requires pointer as input
    delete x;
 
    return 0;
}


CPP
#include 
using namespace std;
 
int main()
{
    int x;
    int* ptr1 = &x;
 
    // x is present on stack frame as
    // local variable, only dynamically
    // allocated variables can be destroyed
    // using delete operator
    delete ptr1;
 
    return 0;
}


2.删除NULL指针:删除NULL不会导致任何更改,也不会出错。

CPP

// C++ program to deleting
// NULLL pointer
#include 
using namespace std;
 
int main()
{
    // ptr is NULL pointer
    int* ptr = NULL;
 
    // deleting ptr
    delete ptr;
 
    return 0;
}

3.删除带或不带值的指针

CPP

#include 
using namespace std;
 
int main()
{
    // Creating int pointer
    int* ptr1 = new int;
     
    // Initializing pointer with value 20
    int* ptr2 = new int(20);
 
    cout << "Value of ptr1 = " << *ptr1 << "\n";
    cout << "Value of ptr2 = " << *ptr2 << "\n";
 
    delete ptr1; // Destroying ptr1
    delete ptr2; // Detroying ptr2
 
    return 0;
}

输出:

Value of ptr1 = 0
Value of ptr2 = 20

4.删除一个指针

CPP

#include 
using namespace std;
int main()
{
    void* ptr; // Creating void pointer
 
    delete ptr; // Destroying void pointer
 
    cout << "ptr deleted successfully";
    return 0;
}

输出

ptr deleted successfully

5.删除由malloc动态分配的内存

CPP

#include 
using namespace std;
 
int main()
{
    // Dynamic memory allocated by using malloc
    int* ptr2 = (int*)malloc(sizeof(int));
 
    delete ptr2;
 
    cout << "ptr2 deleted successfully";
 
    return 0;
}

输出

ptr2 deleted successfully

尽管以上程序在GCC上运行良好。不建议将delete与malloc()一起使用。
6.删除用户定义数据类型的变量

CPP

#include 
using namespace std;
 
struct P {
    static void operator delete(void* ptr, std::size_t sz)
    {
        cout << "custom delete for size " << sz <

输出

custom delete for size 1
custom delete for size 18

例外情况
1.尝试删除非指针对象

CPP

#include 
using namespace std;
 
int main()
{
    int x;
 
    // Delete operator always
    // requires pointer as input
    delete x;
 
    return 0;
}

输出

error: type ‘int’ argument given to ‘delete’, expected pointer

2.尝试删除指向本地堆栈分配变量的指针。

CPP

#include 
using namespace std;
 
int main()
{
    int x;
    int* ptr1 = &x;
 
    // x is present on stack frame as
    // local variable, only dynamically
    // allocated variables can be destroyed
    // using delete operator
    delete ptr1;
 
    return 0;
}

输出

Runtime error

想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”