博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c语言 ++ --运算符_了解C ++中的删除运算符
阅读量:2532 次
发布时间:2019-05-11

本文共 4187 字,大约阅读时间需要 13 分钟。

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)

, 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.

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

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 
using namespace std; int main() { void* str; cout<<"Deleting void pointer.."<

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 
using namespace std; int main() { char* str = (char*)malloc(sizeof(char)); cout<<"Deleting pointer memory allocated dynamically.."<

Output:

输出:

Deleting pointer memory allocated dynamically..


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

During the delete operation on , 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 
using namespace std; int main() { int* str = NULL; cout<<"Deleting a NULL pointer.."<

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 
using namespace std; int main() { int* str = new int(100); cout<<"Value before deletion of pointer:"<<*str<

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:100Deleting pointer..


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

can be deleted using the delele[] operator effectively.

可以使用delele[] operator有效删除 。

Example:

例:

#include 
using namespace std; int main() { int* str = new int[100]; cout<<"Deleting array.."<

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语言 ++ --运算符

转载地址:http://smlzd.baihongyu.com/

你可能感兴趣的文章
HAOI2007 理想的正方形 单调队列
查看>>
(原创)c#学习笔记04--流程控制04--循环03--for循环
查看>>
从控制台输入一个五位数,计算各个数位之和
查看>>
为Sublime Text 3设置优雅的字体
查看>>
Eclipse使用Jetty(转)
查看>>
vim命令收集(持续中)
查看>>
Zynq -- 启动过程
查看>>
206. Reverse Linked List(LeetCode)
查看>>
day 04 Java并发多线程
查看>>
Java定时任务调度工具Timer Quartz
查看>>
WPF,Silverlight与XAML读书笔记第三十五 - 可视化效果之图像
查看>>
Nginx中location配置[转]
查看>>
编程重点
查看>>
00-A-springmvc分布式项目项目结构
查看>>
vs调试时报503错误
查看>>
SVN使用&CVS使用
查看>>
redis
查看>>
Oracle存储过程中如何使用游标
查看>>
揭开NodeJS的神秘面纱!
查看>>
Number Triangles
查看>>