Use of Delete and equality operators in C + + programming _c language

Source: Internet
Author: User

Delete delete operator
frees the memory block.
Grammar

[::] Delete cast-expression
[::] Delete [] cast-expression

Note
The cast-expression parameter must be a pointer to a memory block that was previously assigned to an object created using the new operator. The result type of the delete operator is void, so it does not return a value. For example:

cdialog* mydialog = new CDialog;
Use Mydialog
Delete mydialog;

Using the delete pointer to pointers that do not use the new assignment produces unpredictable results. However, you can use delete for pointers with a value of 0. This setting means that deleting the result of a failed new operation does not cause damage when new fails and returns 0 o'clock.
The new and delete operators are also available for built-in types, including arrays. If pointer refers to an array, place the empty parentheses before pointer:
int* set = new int[100];
Use set[]
Delete operator delete [] set;
Using the delete operator on an object frees its memory. A program that cancels a reference pointer after an object is deleted can produce unpredictable results or crashes.
When the delete is used to release the memory of a C + + class object, the destructor of the object is called before the object's memory is disposed (if the object has a destructor).
If the operand of the delete operator is a modifiable left value, the value is not defined after the object is deleted.
Using the Delete
There are two syntactic variants: one for a single object and another for an array of objects. The following code fragment demonstrates the differences between them:

Expre_using_delete.cpp
struct udtype 
{
};

int main ()
{
  //Allocate A User-defined object, Udobject, and an object
  //type double on the free store US ing the
  //new operator.
  Udtype *udobject = new Udtype;
  Double *dobject = new Double;
  Delete the two objects.
  Delete Udobject;
  Delete Dobject; 
  Allocate an array of user-defined objects in the
  //Free store using the new operator.
  Udtype (*udarr) [7] = new UDTYPE[5][7];
  Use the array syntax to delete the array of objects.
  delete [] Udarr;
}

The following two scenarios produce undefined results: Use the array form of delete in the object (delete []) and use the non-array form of delete in the array.

How the Delete works
The function operator delete is called.
For objects that are not of class type (class, struct, or union), the global delete operator is invoked. For objects of the class type, if the deletion of the expression is a unary range resolution operator (::) Begins, the name of the free function is resolved in the global scope. Otherwise, the delete operator invokes the destructor (if the pointer is not NULL) for the object before releasing memory. The delete operator can be defined for each class, and the global delete operator is invoked if no such definition exists for the given class. If a delete expression is used to release a class object with a virtual destructor for its static object, the release function is resolved through the virtual destructor of the dynamic type of the object.


equality operators: = = and!=

Grammar

   expression = = Expression
expression!= expression

Note
The binary equality operator will strictly compare the equality or inequality of its operands.
The equality operator (= = = = Not equal to (!=)) has a lower priority than the relational operator, but behaves similarly. The result type of these operators is bool.
If the operands have the same value, the equality operator (= =) returns True (1), otherwise it returns False (0). The inequality operator (!=) returns True if the operand does not have the same value, otherwise it returns false.
operator keywords for!=
The NOT_EQ operator is the text equivalent of a!=. There are two ways to access the NOT_EQ operator in a program: include the header file Iso646.h, or compile using the/za (Disable language extensions) compiler option.

Expre_equality_operators.cpp
//compile with:/EHsc
#include <iostream>

using namespace std;

int main () {
  cout << boolalpha
     << "true expression 3!= 2 yields:"
     << (3!= 2) <&L T Endl
     << "the false expression = = yields:"
     << (=) << Endl;
}

The equality operator compares pointers to members of the same type.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.