本文共 4187 字,大约阅读时间需要 13 分钟。
c语言 ++ --运算符
Hello, folks! In this article, we will be understanding the necessity and usage of the delete operator in C++.
大家好! 在本文中,我们将了解C ++中delete运算符的必要性和用法 。
, 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.
现在我们已经了解了删除运算符的工作原理和需求,现在让我们了解删除运算符在实际编程场景中的应用。
The delete operator can be used to free the memory space occupied by the void pointer
variable efficiently.
delete运算符可用于有效释放void pointer
变量占用的存储空间。
Example:
例:
#includeusing namespace std; int main() { void* str; cout<<"Deleting void pointer.."<
Output:
输出:
Deleting void pointer..
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:
例:
#includeusing namespace std; int main() { char* str = (char*)malloc(sizeof(char)); cout<<"Deleting pointer memory allocated dynamically.."<
Output:
输出:
Deleting pointer memory allocated dynamically..
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指针
#includeusing 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:删除指向特定值的指针变量点
#includeusing 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..
can be deleted using the delele[] operator
effectively.
可以使用delele[] operator
有效删除 。
Example:
例:
#includeusing namespace std; int main() { int* str = new int[100]; cout<<"Deleting array.."<
Output:
输出:
Deleting array..
delete[] operator
deletes the space occupied by the array objects. delete[] operator
删除数组对象占用的空间。 Thus, in this article, we have studied and understood the the working of delete operator in C++.
因此,在本文中,我们研究并了解了C ++中delete运算符的工作方式。
翻译自:
c语言 ++ --运算符
转载地址:http://smlzd.baihongyu.com/