Standard expression of Dynamic Allocation (new, delete; malloc, free)

Source: Internet
Author: User

1. Some people may only use the following sentence for Dynamic Allocation:

#include <iostream>using namespace std;int main(void){int *p;p = new int(1);//1cout << *p << endl;int *q;q = (int *)malloc(sizeof(int));*q = 1;cout << *q << endl;return 0;}

I don't want to say much about it.

(In addition, the above uses the new plain new usage)

2. I have summarized the Dynamic Allocation Statement (of course there are other statements), so I will write it later:

#include <iostream>using namespace std;int main(void){int *p = NULL;p = new(nothrow) int(1);//1if (p == NULL) {//2cerr << "Allocate failed!" << endl;exit(OVERFLOW);}cout << *p << endl;delete p;//3p = NULL;//4/*----------------------------------------------------*/int *q = NULL;q = (int *)malloc(sizeof(int));//1if (q == NULL) {//2cerr << "Allocate failed!" << endl;exit(OVERFLOW);}*q = 1;cout << *q << endl;free(q);//3q = NULL;//4return 0;}

Additional reading: Lin Rui

Http://blog.sina.com.cn/s/blog_446b43c10100d7ci.html

 

p= new (nothrow) int[i];if (p == 0)    cout << "Error: memory could not be allocated";

Http://www.cplusplus.com/doc/tutorial/dynamic/

 

 

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.