内存操作错误
Incorrect memory operation?
请帮助查找此函数中的错误。
wchar_t* clean(wchar_t out[], const wchar_t in[])
{
int n = wcslen(in);
wchar_t *str = new wchar_t[n];
wcscpy(str, in);
out[0] = L' ';
wchar_t *state;
wchar_t *word = wcstok(str, L" ", &state);
while (NULL != word) {
if (wcslen(word) > 1) {
wcscat(out, word);
wcscat(out, L" ");
}
word = wcstok(NULL, L" ", &state);
}
delete state;
delete[] str;
return out;
}
该函数从原始字符串中获取单词并将其放入结果字符串中。除了函数忽略多个空格和单个字母中的单词。
不幸的是,程序落在这个函数的最后几行,出现了同样的错误(linux-3.7, gcc-4.7):
*** Error in `./a.out': free(): invalid next size (fast): 0x08610338 ***
请解释一下我错在哪里?
-
移除
delete state;
。state
不是指向动态内存的指针,您可以从没有为其分配任何动态分配中看出这一点。它只是一个指针,指向一个已经存在的字符串。 -
修复
new wchar_t[n]
缓冲区溢出;
相关文章:
- 给定一个整数数组,需要在Max_Heap上运行操作。得到错误"segmentation fault",有什么想法吗?(C++)
- C++ 随机数生成器:尝试将结果作为向量获取,但通过制作 void 函数来执行此操作而出现错误
- 如何修复无效的API密钥,IP或操作权限错误?
- 使用 OpenCV readNetFromTensorFlow 运行 Keras DNN 模型 (UNet):错误:操作
- Websocket 客户端在 C++ 中使用 boost::Beast - 在写入操作时抛出错误
- 在C++中使用友元函数操作模板类中的私有变量时会发生错误
- 如何修复重新平衡时AVL删除操作中的分段错误?
- 元数据操作失败LNK2022错误 (8013118D):重复类型中的布局信息不一致 (选择设备参数):(0x020002
- 我的移动分配操作程序重载的分段错误
- 打印矩阵后分割错误,但在打印额外行后修复(ostream <<操作器)
- glteximage3d抛出错误1282(无效操作)
- INC 操作码编译到错误的地址
- 当提供巨大的输入时,中止在堆栈操作程序中调用错误
- 加速 ASIO 系统错误 995:I/O 操作已中止
- 将迭代器放置为临时范围时,非销钉 - 操作和错误
- 二进制搜索树操作程序错误:分割故障(核心倾倒)
- 如何修复问题中的"teminated due to timeout"错误 数组操作 |黑客兰克?
- 尝试执行任何需要它的操作时,无法修复 WinAPI 中的"invalid handle"错误
- 错误:如果(object1 == object2),“操作器==”无匹配(操作数类型为“ const class”和“
- 为什么此内联程序集中的错误操作无法正常工作