Delete, delete [], delete void *

Source: Internet
Author: User

The renren6250 statement is comprehensive.
The use of delete or delete [] for arrays of simple data types such as int and char is exactly the same, and delete [] must be used for arrays of class instances;

 

C ++ casual Delete void *

After using C ++ for so long, I found that C ++ is the most complex language in all languages. The path to C ++ is hard .....


Delete void *

This operation will not be problematic for some simple built-in types of the system. However, if the object pointed to by void * is to be released in the destructor, memory will be lost during this creation,Because it does not execute destructorFor example:

Class centity
{
Public:
Centity (char flag );
~ Centity ();
PRIVATE:
Void * m_data;
Char m_flag;
};

Centity: centity (char flag)
{
M_flag = flag;
Cout <"constructing entity" <m_flag <Endl;
M_data = new char [100];
}

Centity ::~ Centity ()
{
Cout <"destructing entity" <m_flag <Endl;
Delete [] m_data;
}

There is no problem with the above class, but refer to the following calls:
Int main (INT argc, char * argv [])
{
Centity * A = new centity ('A ');
Delete;

Void * B = new centity ('B ');
Delete B;
 
Return 0;
}

The output is constructing Entity.
Destructing entity
Constructing entity B
It can be seen that the memory pointed to by m_data is not released and the Destructor is not executed during Delete B.

I don't know if I have collected it online, right?

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.