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