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/