|
void Func(void)
{ A *a = new A; if(a == NULL) { return; } … } |
|
void Func(void)
{ A *a = new A; if(a == NULL) { cout << “Memory Exhausted” << endl; exit(1); } … } |
|
void main(void)
{ float *p = NULL; while(TRUE) { p = new float[1000000]; cout << “eat memory” << endl; if(p==NULL) exit(1); } } 示例7试图耗尽操作系统的内存 |
|
void * malloc(size_t size);
|
|
int *p = (int *) malloc(sizeof(int) * length);
|
|
cout << sizeof(char) << endl;
cout << sizeof(int) << endl; cout << sizeof(unsigned int) << endl; cout << sizeof(long) << endl; cout << sizeof(unsigned long) << endl; cout << sizeof(float) << endl; cout << sizeof(double) << endl; cout << sizeof(void *) << endl; |
|
void free( void * memblock );
|
|
int *p1 = (int *)malloc(sizeof(int) * length);
int *p2 = new int[length]; |
|
class Obj
{ public : Obj(void); // 无参数的构造函数 Obj(int x); // 带一个参数的构造函数 … } void Test(void) { Obj *a = new Obj; Obj *b = new Obj(1); // 初值为1 … delete a; delete b; } |
|
Obj *objects = new Obj[100]; // 创建100个动态对象
不能写成
Obj *objects = new Obj[100](1);// 创建100个动态对象的同时赋初值1 |
|
delete []objects; // 正确的用法
delete objects; // 错误的用法 |
发表于 @ 2008年02月20日 19:49:00 | 评论( loading... ) | 举报| 收藏