动态分配的字符串数组无法释放 [英] dynamically allocated string arrays failed to be deallocated

查看:155
本文介绍了动态分配的字符串数组无法释放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

string * p = new string[8];
cout<<sizeof(p)<<endl;
free(p);

这似乎对我确定但失败:

which seems ok to me but failed with:

8
a.out(85837) malloc: *** error for object 0x7fb5b3403ae8: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Abort trap: 6

对整数数组的另一个测试工作。有什么特别的c ++字符串?

Another test on an integer array worked. Is there anything special with c++ string?

推荐答案

Yap。 new [] delete [] 配对。 free() ing会导致未定义的行为。

Yap. new [] pairs with delete []. free()ing causes undefined behavior.

这篇关于动态分配的字符串数组无法释放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆