|
1
2
3
|
int*p;p=new int;//返回类型为int*类型(整数型指针),分配大小为sizeof(int); |
|
1
2
3
|
int*parr;parr=new int[100];//返回类型为int*类型(整数型指针),分配大小为sizeof(int)*100; |
|
1
2
3
4
5
6
7
|
int*p;p=(int*)malloc(sizeof(int)*128);//分配128个(可根据实际需要替换该数值)整型存储单元,//并将这128个连续的整型存储单元的首地址存储到指针变量p中double*pd=(double*)malloc(sizeof(double)*12);//分配12个double型存储单元,//并将首地址存储到指针变量pd中 |
|
1
2
3
4
5
6
7
8
9
10
|
type*p;if(NULL==(p=(type*)malloc(sizeof(type))))/*请使用if来判断,这是有必要的*/{perror("error...");exit(1);}.../*其它代码*/free(p);p=NULL;/*请加上这句*/ |
|
1
2
|
int*p=(int*)malloc(sizeof(int)*100);//分配可以放得下100个整数的内存空间。 |
|
1
2
3
4
5
|
char*ptr;if((ptr=(char*)malloc(0))==NULL)puts("Gotanullpointer");elseputs("Gotavalidpointer"); |
原文:http://www.cnblogs.com/arxive/p/5072784.html