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:

Some pitfall of c/cpp pointer_第1张图片
Paste_Image.png

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

Some pitfall of c/cpp pointer_第2张图片

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".

Some pitfall of c/cpp pointer_第3张图片
Paste_Image.png

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

Some pitfall of c/cpp pointer_第4张图片
Paste_Image.png

3.Don't return the local variable.


Some pitfall of c/cpp pointer_第5张图片
Paste_Image.png

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

Some pitfall of c/cpp pointer_第6张图片
Paste_Image.png

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

你可能感兴趣的:(Some pitfall of c/cpp pointer)