C ++ learning cheat sheet (2)

Source: Internet
Author: User

1. Differences between malloc, free, new, and delete

First, malloc and free belong to a group of library functions in C. New Delete belongs to a group of operators in C ++. All functions are used to open up and release memory space. But the difference is. The new and delete operators call the class constructor and destructor when opening up the space. Malloc free does not have this function. (C does not have the concept of class either)

2. Which class member functions are generated by C ++ empty classes by default?

The compiler automatically generates four functions: default constructor, destructor, copy constructor, and assign value function.

3. The destructor in C ++ can be inline functions.

 

4. Differences between Delete and delete [] in C ++.

1. for basic types (INT, Char, float, etc.), the memory space opened up can be released using both operators.

Char * A = new char [300];

Delete A; // or delete [];

2. For the custom class type, after opening up the memory space of the class array, use Delete [] to release the memory. If delete is used, the memory can also be released, but it does not call the class

Destructor, resulting in incomplete release operations in the class.

 

Class T;

 

T* T = new T [20];

Delete t; // only the destructor of T [0] is called. Other objects are released without calling the destructor.

Delete [] T; // release the memory and call the corresponding destructor.

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.