c语言 ++ --运算符

Hello, folks! In this article, we will be understanding the necessity and usage of the delete operator in C++.

大家好! 在本文中,我们将了解C ++中delete运算符必要性和用法



C ++ delete()函数入门 (Getting started with C++ delete() function)

C++ language, being an object-oriented language, has the capability to create objects and manipulate the data variables at run-time. That is, they create and deletion of objects can happen dynamically and with ease.

C ++语言是一种面向对象的语言,具有在运行时创建对象和操作数据变量的功能。 也就是说,它们可以动态轻松地创建和删除对象。

The delete operator in C++ serves the purpose of deletion of arrays, objects, pointers, etc. The new operator assigns dynamic memory to the object or data variable. In order to delete the memory assigned, we use delete operator.

C ++中的delete运算符用于删除数组,对象,指针等。new运算符将动态内存分配给对象或数据变量。 为了删除分配的内存,我们使用delete运算符。

Syntax:

句法:


delete object/array/pointer;

The delete operator deletes and thus deallocates the assigned memory from the heap structure.

delete运算符从堆结构中删除并因此释放了分配的内存。

Now that we have understood the working and need of delete operator, let us now understand the applications of delete operator in actual programming scenarios.

现在我们已经了解了删除运算符的工作原理和需求,现在让我们了解删除运算符在实际编程场景中的应用。



变体1:使用删除运算符删除空指针 (Variant 1: Deleting a void pointer using delete operator)

The delete operator can be used to free the memory space occupied by the void pointer variable efficiently.

delete运算符可用于有效释放void pointer变量占用的存储空间。

Example:

例:

#include <bits/stdc++.h>
using namespace std; int main()
{ void* str;   cout<<"Deleting void pointer.."<<endl;delete str;  return 0;
}

Output:

输出:


Deleting void pointer..


变体2:通过delete()函数删除动态分配的内存 (Variant 2: Deleting dynamically allocated memory through delete() function)

The delete operator can be used to delete the memory that has been dynamically allocated by using the malloc() function to the pointer variable.

可以使用delete运算符删除通过使用malloc()函数对指针变量动态分配的内存

Example:

例:

#include <bits/stdc++.h>
using namespace std; int main()
{ char* str = (char*)malloc(sizeof(char));cout<<"Deleting pointer memory allocated dynamically.."<<endl;delete str; return 0;
} 

Output:

输出:


Deleting pointer memory allocated dynamically..


变体3:使用delete()函数删除指针 (Variant 3: Deleting pointers with delete() function)

During the delete operation on pointers, the pointer to the object or the variable does not get deleted. So, the delete operator deletes the memory space that the pointer points in the RAM.

在对指针的删除操作过程中, 指向对象或变量的指针不会被删除。 因此,删除操作符将删除指针在RAM中指向的存储空间。

Example 1: Deleting NULL pointers with delete operator

示例1: 使用delete运算符删除NULL指针

#include <bits/stdc++.h>
using namespace std; int main()
{ int* str = NULL; cout<<"Deleting a NULL pointer.."<<endl;delete str; return 0;
} 

In the above example, we have destroyed the memory occupied by the NULL pointer variable.

在上面的示例中,我们销毁了NULL指针变量占用的内存。

Output:

输出:


Deleting a NULL pointer..

Example 2: Deleting a pointer variable point to a particular value

示例2:删除指向特定值的指针变量点

#include <bits/stdc++.h>
using namespace std; int main()
{ int* str = new int(100); cout<<"Value before deletion of pointer:"<<*str<<endl;cout<<"Deleting pointer.."<<endl;delete str; return 0;
} 

In the above snippet of code, we have used delete operator to deallocate the memory space occupied by the pointer variable holding a data value. Here, the data value is cleared from the space occupied by the pointer variable.

在上面的代码片段中,我们使用了delete运算符来释放由保存数据值的指针变量占用的内存空间。 在此,从指针变量占用的空间中清除数据值。

Output:

输出:


Value before deletion of pointer:100
Deleting pointer..


变体4:通过delete()函数删除数组对象 (Variant 4: Deleting an array object by delete() function)

Array objects in C++ can be deleted using the delele[] operator effectively.

可以使用delele[] operator有效删除C ++中的数组对象 。

Example:

例:

#include <bits/stdc++.h>
using namespace std; int main()
{ int* str = new int[100]; cout<<"Deleting array.."<<endl;delete[] str; return 0;
} 

Output:

输出:


Deleting array..


摘要 (Summary)

  • The delete operators deallocates the memory space occupied by the data variables, arrays or pointers.删除运算符将分配数据变量,数组或指针占用的内存空间
  • Moreover, it does not destroy the pointer, rather the memory space used by the pointer gets deleted.而且,它不会破坏指针 ,而是会删除指针使用的内存空间。
  • The delete[] operator deletes the space occupied by the array objects.delete[] operator删除数组对象占用的空间。


结论 (Conclusion)

Thus, in this article, we have studied and understood the the working of delete operator in C++.

因此,在本文中,我们研究并了解了C ++中delete运算符的工作方式。



参考资料 (References)

  • C++ delete operator — Official DocumentationC ++删除运算符—官方文档

翻译自: https://www.journaldev.com/38336/delete-operator-in-c-plus-plus

c语言 ++ --运算符

c语言 ++ --运算符_了解C ++中的删除运算符相关推荐

  1. python标识运算符_讲解Python中的标识运算符

    下表列出了所有Python语言支持的标识运算符. 示例: 试试下面的例子就明白了所有Python编程语言提供的标识运算符: #!/usr/bin/python a = 20 b = 20 if ( a ...

  2. python中的成员运算符_在Python中使用成员运算符的示例

    下表列出了所有python语言支持的成员运算符. 例如: 试试下面的例子就明白了所有的python编程语言提供会员运算符: #!/usr/bin/python a = 10 b = 20 list = ...

  3. c语言共有几种运算符_【填空题】C语言一共有 ()个关键字,()中控制语句,()种运算符...

    [填空题]C语言一共有 ()个关键字,()中控制语句,()种运算符 更多相关问题 [填空题] 对煤进行工业分析的目的,是为了判断煤的(). [填空题] 钛的比重是不锈钢的一半,抗腐蚀性是不锈钢的(). ...

  4. 3_1符合python语言变量_以下选项中符合Python语言变量命名规则的是

    [单选题]以下选项中,不是Python语言特点的是 [单选题]较小的尺寸应离轮廓线较近,较大的尺寸线离轮廓线较远.( ) [单选题]关于Python语言的变量,以下选项中说法正确的是 [单选题]182 ...

  5. java 运算符_详解Java表达式与运算符

    课程导言 [变量的赋值与计算都离不开表达式,表达式的运算依赖于变量.常量和运算符.本节课讨论Java的表达式的构成.常量的定义.运算符的分类及应用.通过本课的学习你将掌握运用表达式和运算符完成变量赋值 ...

  6. java中的比较运算符_Java基础---Java中的比较运算符(十三)

    Java中的比较运算符 比较运算符用于判断两个数据的大小,例如:大于.等于.不等于.比较的结果是一个布尔值( true 或 false ). Java 中常用的比较运算符如下表所示: 注意哦: 1. ...

  7. sql 运算符_了解SQL ANY和ALL运算符

    sql 运算符 Hello, folks! Hope you all are doing well. In this article, we will be discussing about SQL ...

  8. mysql 删除视图 命令_在MySQL中,删除视图使用(????)命令。

    在MySQL中,删除视图使用(????)命令. 答:DROP 设计的优点之一就在于其在命令执行上的刚性 答:职能式 4月15日作业 答:笔记 喘证的主要临床特征是 答:呼吸急促,甚至张口抬肩,鼻翼煽动 ...

  9. php mysql增删查改 主码不能修改_关系规范化中的删除操作异常是指什么

    关系规范化中的删除操作异常是指不该删除的数据被删除,插入异常是指应该插入的数据未被插入:而规范化就是为了解决数据库中数据的插入.删除.修改异常等问题的一组规则. 关系规范化中的删除操作异常是指不该删除 ...

最新文章

  1. 5秒手机猫片也能重建猫咪3D模型,Meta提出新算法为变形物体建模 | CVPR 2022
  2. 理论+实操: linux中firewalld防火墙基础讲解(转载)
  3. USACO 1.3... 虫洞 解题报告(搜索+强大剪枝+模拟)
  4. 2009年9月三级网络技术51CTO版考前压轴试题
  5. 【C语言简单说】八:分支结构之if...else...(2)
  6. 探寻安全管理平台(SOC)项目的关键成功因素
  7. 还在为xmind发愁,赶紧试试xmind移动版、绿色版
  8. 【李宏毅2020 ML/DL】P57 Unsupervised Learning - Linear Methods | PCA Matrix Factorization
  9. 【Computer Organization笔记12】流水线技术概述
  10. 结构化编程 —— 顺序、分支(选择)、循环
  11. idea项目(git)版本回退
  12. 下载并遍历 names.txt 文件,输出长度最长的回文人名。
  13. 20169217 2016-2017-2 《网络攻防实践》第四周学习总结
  14. Java 在线反编译反编译工具
  15. 黄山学院计算机作业管理系统,在线作业管理系统
  16. 宏观经济模型代码来源 :MMB库
  17. Python实现将mp3音频格式转换为wav格式
  18. 【网络工程】交换机各层作用,100路监控摄像是否_该采用核心交换机吗
  19. 39期1组,第一个项目感受---------文字与回忆
  20. AI之NLP:自然语言处理技术简介(是什么/学什么/怎么用)、常用算法、经典案例之详细攻略(建议收藏)daiding

热门文章

  1. android_Media
  2. Delphi调用外部程序详解
  3. 云服务器上安装Anaconda3 (亲测有效)
  4. hidden field implements session
  5. 学习项目管理PRINCE2有什么用??
  6. CentOS下安装实时检测网络带宽的小工具bmon
  7. Html 小插件4 百度搜索代码
  8. JAVA子类调用父类构造方法
  9. 程序执行系统VC++程序出现“试图执行系统不支持的操作”
  10. setTimeout和setInteval