Some pitfall of c/cpp pointer

Copyright @ Joel Jiang(江东敏) at 2017.09.16 21:00 p.m
in Shenzhen.China. LIST- TE- E11 -05
And some picture refer from the internet.

1.Avoid memory leak.
Make a attention of the "malloc/free" and "new/delete" usage.
1> Normal usage, leak one of them:

Paste_Image.png

2> In the abnormal condition, for the "try../ catch..." :


2.Avoid the wild pointer.
"Wild" pointer is not aqual to the "NULL" pointer, means that "if(NULL == P)" can't be a situation for before the process in a function. There is some case as follow:
Note: P is pointer.
1> When the P is free or delete and don't be set as "NULL" .Actually the P will be a wild P, because when we delete P in this operation, deleting operation is delete the memory that the pointer point to ,rather than the P. eventually the P will be the wild pointer if you set it to "NULL".

Paste_Image.png

2> Forget init the point variable when it be structured.

Paste_Image.png

3.Don't return the local variable.


Paste_Image.png

4.More use the "if(NULL == P)" when apply memory and the function parameter.

Paste_Image.png

The sharing of knowledge, the spirit of encouragement.
By Joel Jiang (江东敏)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 阅读进度:191/266 今天读的内容: ⊙ 剖面的视野—— 滕头案例馆 ⊙ 为了一种曾经被贬抑的世界的呈现 ⊙ ...
    冰洛洛阅读 146评论 0 0
  • 这张照片,有点意思。多少人的关系变了,是因为别人的嘴。 不知我曾经死过谁的嘴下,你永远不知道在别人嘴里有多少个版本...
    淡淡忘阅读 242评论 0 0
  • 人生中第一个目标清单,从给自己一个好的习惯开始。 100天目标清单: 1,每天早上6:30-7:30(视娃的起床时...
    13号Nicole阅读 1,517评论 1 35