C ++ new and delete usage

Source: Internet
Author: User

New and delete can effectively and directly allocate and release dynamic memory. The new operator returns a pointer of the specified type. If the allocation fails (if there is not enough memory space), 0 is returned. For example, double * p; p = new double; * p = 30.4; // store the value in the opened unit. The system automatically opens up the memory unit according to the space size of the double type, and put the address in Pointer p. Of course, you can also initialize the value in the unit when opening up the memory unit. The preceding code is equivalent to: double * p; p = new double (30.4); the delete operator releases the memory of the new request channel. Delete p releases the memory unit of the p pointer. the pointer Variable p is still valid and can be directed to another memory unit. New can also allocate memory to the array. When released, it can also tell the number of delete arrays. For example: int * p; p = new int [10]; delete [10] p; // tell the delete array how many elements are there, or delete [] p;

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.