char a[] = “hello”;
a[0] = ‘X’;
cout << a << endl;
char *p = “world”; // 注重p指向常量字符串
p[0] = ‘X’; // 編譯器不能發(fā)現(xiàn)該錯誤
cout << p << endl; 示例3.1 修改數(shù)組和指針的內(nèi)容// 數(shù)組…
char a[] = "hello";
char b[10];
strcpy(b, a); // 不能用 b = a;
if(strcmp(b, a) == 0) // 不能用 if (b == a)
…
// 指針…
int len = strlen(a);
char *p = (char *)malloc(sizeof(char)*(len+1));
strcpy(p,a); // 不要用 p = a;
if(strcmp(p, a) == 0) // 不要用 if (p == a)
… 示例3.2 數(shù)組和指針的內(nèi)容復(fù)制與比較char a[] = "hello world";
char *p = a;
cout<< sizeof(a) << endl; // 12字節(jié)
cout<< sizeof(p) << endl; // 4字節(jié) 示例3.3(a) 計算數(shù)組和指針的內(nèi)存容量void Func(char a[100])
{
cout<< sizeof(a) << endl; // 4字節(jié)而不是100字節(jié)
} 示例3.3(b) 數(shù)組退化為指針void GetMemory(char *p, int num)
{
p = (char *)malloc(sizeof(char) * num);
}
void Test(void)
{
char *str = NULL;
GetMemory(str, 100); // str 仍然為 NULL
strcpy(str, "hello"); // 運行錯誤
} 示例4.1 試圖用指針參數(shù)申請動態(tài)內(nèi)存void GetMemory2(char **p, int num)
{
*p = (char *)malloc(sizeof(char) * num);
}
void Test2(void)
{
char *str = NULL;
GetMemory2(&str, 100); // 注重參數(shù)是 &str,而不是str
strcpy(str, "hello");
cout<< str << endl;
free(str);
} 示例4.2用指向指針的指針申請動態(tài)內(nèi)存char *GetMemory3(int num)
{
char *p = (char *)malloc(sizeof(char) * num);
return p;
}
void Test3(void)
{
char *str = NULL;
str = GetMemory3(100);
strcpy(str, "hello");
cout<< str << endl;
free(str);
} 示例4.3 用函數(shù)返回值來傳遞動態(tài)內(nèi)存char *GetString(void)
{
char p[] = "hello world";
return p; // 編譯器將提出警告
}
void Test4(void)
{
char *str = NULL;
str = GetString(); // str 的內(nèi)容是垃圾
cout<< str << endl;
} 示例4.4 return語句返回指向“棧內(nèi)存”的指針char *GetString2(void)
{
char *p = "hello world";
return p;
}
void Test5(void)
{
char *str = NULL;
str = GetString2();
cout<< str << endl;
} 示例4.5 return語句返回常量字符串char *p = NULL;
char *str = (char *) malloc(100);class A
{
public:
void Func(void){ cout << “Func of class A” << endl; }
};
void Test(void)
{
A *p;
{
A a;
p = &a; // 注重 a 的生命期
}
p->Func(); // p是“野指針”
}class Obj
{
public :
Obj(void){ cout << “Initialization” << endl; }
~Obj(void){ cout << “Destroy” << endl; }
void Initialize(void){ cout << “Initialization” << endl; }
void Destroy(void){ cout << “Destroy” << endl; }
};
void UseMallocFree(void)
{
Obj *a = (obj *)malloc(sizeof(obj)); // 申請動態(tài)內(nèi)存
a->Initialize(); // 初始化
//…
a->Destroy(); // 清除工作
free(a); // 釋放內(nèi)存
}
void UseNewDelete(void)
{
Obj *a = new Obj; // 申請動態(tài)內(nèi)存并且初始化
//…
delete a; // 清除并且釋放內(nèi)存
} 示例6 用malloc/free和new/delete如何實現(xiàn)對象的動態(tài)內(nèi)存治理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);
}
}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); // 無參數(shù)的構(gòu)造函數(shù)
Obj(int x); // 帶一個參數(shù)的構(gòu)造函數(shù)
…
}
void Test(void)
{
Obj *a = new Obj;
Obj *b = new Obj(1); // 初值為1
…
delete a;
delete b;
}Obj *objects = new Obj[100]; // 創(chuàng)建100個動態(tài)對象Obj *objects = new Obj[100](1);// 創(chuàng)建100個動態(tài)對象的同時賦初值1delete []objects; // 正確的用法
delete objects; // 錯誤的用法新聞熱點
疑難解答