Chinaunix首页 | 论坛 | 博客
  • 博客访问: 483297
  • 博文数量: 111
  • 博客积分: 2332
  • 博客等级: 大尉
  • 技术积分: 1187
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-29 11:22
文章分类

全部博文(111)

文章存档

2013年(9)

2012年(28)

2011年(17)

2010年(28)

2009年(29)

我的朋友

分类: C/C++

2012-11-19 10:55:42

When you call exit(), the destructors of automatic objects (local variables) do not get called.

In your specific example, the std::string destructor is not called, so the memory owned by thestd::string is never deallocated.

The reason there is no leak if you have fail() take a const char* is that there is no destructor forconst char*; nothing is deallocated when a pointer is destroyed. If the pointer points to dynamically allocated memory, then that memory must be deallocated (by you) before the program exits, otherwise you have a memory leak. If it points to a string literal, then there is no memory leak because string literals have static storage duration (that is, they exist for the entire lifetime of your program).

 

详见:

阅读(769) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~