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

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)

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

c语言 ++ --运算符

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值