美文网首页
Some pitfall of c/cpp pointer

Some pitfall of c/cpp pointer

作者: JoeJiang | 来源:发表于2017-09-16 21:22 被阅读51次

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 (江东敏)

相关文章

网友评论

      本文标题:Some pitfall of c/cpp pointer

      本文链接:https://www.haomeiwen.com/subject/mzuusxtx.html