C++ string 使用引起的内存泄露

今天在CI的时候看到一位程序员的代码,如下,大概就是返回一个给定长度的,用空格填充的字符串。at the first glance,这段代码会引起内存泄露。

因为在函数中new了一块内存作为临时指针,指针作为返回结果临时变量的构造函数的参数, 之后再也没有引用到这块地址的指针。这段代码也在

程序中完全失去控制。造成了内存的泄露。

 

造成内存泄露的原因是,对于string, 包含一个内部成员的char*, 每次构造一个新的string 对象,都是把参数的内容copy一份,而不是直接向参数的指针。

如果此函数多次调用,会造成内存的不断流失。生气

 

 static string getSpacesString(int spacesNum, int times = 1)
{
  char* s = new char[spacesNum * times + 1];
  memset(s, ' ', spacesNum * times);
  s[spacesNum * times] = '\0';
  return s;

}

 

为了看的清楚,用GDB调试结果。

int main (int argc, char** argv)
{

  string test = getSpacesString(10,2);     //同样返回的结果会作为临时变量,作为test拷贝构造函数的参数。

  char * sst = const_cast<char*>(test.c_str());

  printf("address of sst is %x \n",sst);
}

 

可以看到两次的指针是不同的。

 

一种解决办法是利用栈空间作为临时字符串。

#define MAX_STR_LENTH 100

static string getSpacesString(int spacesNum, int times = 1)
{

 //char* s = new char[spacesNum * times + 1];
  char s [MAX_STR_LENTH] ;
 
  printf("address is %x  \n", s);
  memset(s, 'a', spacesNum * times);
  s[spacesNum * times] = '\0';
  return s;
}

 

 

 

 


[ansen@localhost final]$ gdb ./a.out
GNU gdb (GDB) Fedora 7.6.50.20130731-16.fc20
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-redhat-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word".
..
Reading symbols from /home/ansen/training/network/final/a.out...done.
(gdb) b main
Breakpoint 1 at 0x8048823: file memleakstr.cxx, line 22.
(gdb) s
The program is not being run.
(gdb) r
Starting program: /home/ansen/training/network/final/a.out 

Breakpoint 1, main (argc=1, argv=0xbffff2d4) at memleakstr.cxx:22
22	  string test = getSpacesString(10,2);
Missing separate debuginfos, use: debuginfo-install glibc-2.18-11.fc20.i686 libgcc-4.8.2-1.fc20.i686 libstdc++-4.8.2-1.fc20.i686
(gdb) s
getSpacesString (spacesNum=10, times=2) at memleakstr.cxx:10
10	  char* s = new char[spacesNum * times + 1];
(gdb) p s
$1 = 0xb7ced0d3 <__new_exitfn+19> "\201\303-\177\030"
(gdb) n
12	  printf("address is %x  \n", s);
(gdb) p s
$2 = 0x804b008 ""
(gdb) n
address is 804b008  
13	  memset(s, 'a', spacesNum * times);
(gdb) n
14	  s[spacesNum * times] = '\0';
(gdb) n
15	  return s;
(gdb) n
16	}
(gdb) n
main (argc=1, argv=0xbffff2d4) at memleakstr.cxx:24
24	  char * sst = const_cast<char*>(test.c_str());
(gdb) n
26	  printf("address of sst is %x \n",sst);
(gdb) p sst
$3 = 0x804b034 'a' <repeats 20 times>
(gdb) n
address of sst is 804b034 
29	}
(gdb) n
0xb7cd4b83 in __libc_start_main () from /lib/libc.so.6
(gdb) 


 

 

 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值