返回的地址从第二配置覆盖第一分配地址。因此,第一分配成为一个内存泄漏!
空指针(第二部分)
空指针(指针设置为解决0)特别有用,在处理动态内存分配。一个空指针说“没有记忆已分配但”。这允许我们做的事情一样,有条件的分配内存:
|
1
2
3
|
//
If pnValue isn't already allocated, allocate itif
(!pnValue) pnValue
= new
int; |
|
1
2
|
int
*pnValue = new
int;int
*pnOtherValue = 0; //
will allocate later |
|
1
2
3
4
5
|
int
*pnValue = new
int;delete
pnValue; //
pnValue not set to 0if
(pnValue) *pnValue
= 5; //
will cause a crash |
|
1
2
3
4
5
6
7
|
int
*pnValue = new
int;*pnValue
= 7;delete
pnValue;pnValue
= 0;if
(pnValue) *pnValue
= 5; |
12万+

被折叠的 条评论
为什么被折叠?



