char a[] = “hello”;
a[0] = ‘X’;
cout << a << endl;
char *p = “world”; // 注重p指向常量字符串
p[0] = ‘X’; // 編譯器不能發現該錯誤
cout << p << endl;
示例3.1 修改數組和指針的內容// 數組…
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 數組和指針的內容復制與比較char a[] = "hello world";
char *p = a;
cout<< sizeof(a) << endl; // 12字節
cout<< sizeof(p) << endl; // 4字節
示例3.3(a) 計算數組和指針的內存容量void Func(char a[100])
{
cout<< sizeof(a) << endl; // 4字節而不是100字節
}
示例3.3(b) 數組退化為指針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 試圖用指針參數申請動態內存void GetMemory2(char **p, int num)
{
*p = (char *)malloc(sizeof(char) * num);
}
void Test2(void)
{
char *str = NULL;
GetMemory2(&str, 100); // 注重參數是 &str,而不是str
strcpy(str, "hello");
cout<< str << endl;
free(str);
}
示例4.2用指向指針的指針申請動態內存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 用函數返回值來傳遞動態內存char *GetString(void)
{
char p[] = "hello world";
return p; // 編譯器將提出警告
}
void Test4(void)
{
char *str = NULL;
str = GetString(); // str 的內容是垃圾
cout<< str << endl;
}
示例4.4 return語句返回指向“棧內存”的指針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)); // 申請動態內存
a->Initialize(); // 初始化
//…
a->Destroy(); // 清除工作
free(a); // 釋放內存
}
void UseNewDelete(void)
{
Obj *a = new Obj; // 申請動態內存并且初始化
//…
delete a; // 清除并且釋放內存
}
示例6 用malloc/free和new/delete如何實現對象的動態內存治理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); // 無參數的構造函數
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; // 錯誤的用法
新聞熱點
疑難解答