分配资源的函数或对象,同时应该负责释放资源。不要推迟到另外一个函数或对象去释放。这样容易造成潜在隐患。
比如在一个函数中open file 或者new 空间,也应该在该函数中close file 或
delete
空间。
whoever allocates a resource should be responsible for
deallocating it。
Some C and C++
developers make a point of setting a pointer to NULL after they deallocate the
memory it references. This is a good idea.
Because this will prevent refer(deference) to this
pointer later, which will cause runtime error.
No comments:
Post a Comment