gpt4 book ai didi

c++ - Mac OSX 上 Qt App 中完全随机的 malloc 错误

转载 作者:搜寻专家 更新时间:2023-10-31 00:01:49 25 4
gpt4 key购买 nike

#include <QtCore/QCoreApplication>

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
a.setApplicationName("xxx");
char bb[25] = {10, 1, 64, 18, 20, 116, 97, 114, 97, 110, 103, 105, 108, 108, 51, 64, 103, 109, 97, 105, 108, 46, 99, 111, 109};
char* aa = new char(25);
memcpy(aa, bb, 25);
delete aa;
return a.exec();
}

当我运行上面的代码时,5 次中大约有 1 次出现以下错误:

tftest(28702,0x7fff70de3cc0) malloc: *** error for object 0x10160ee28: incorrect checksum for freed object - object was probably modified after being freed.
*** set a breakpoint in malloc_error_break to debug
Press <RETURN> to close this window...

这让我抓狂,因为错误完全是随机出现的。

整个崩溃日志位于 http://pastebin.com/Qtp9T2gW

最佳答案

行:

char *aa = new char(25); // dynamically allocate a single char = 25

完全不同于:

char *aa = new char[25]; // dynamically allocate an array [0..24] of char

您还需要将 operator new[]operator delete[] 结合,将 operator newoperator delete - 你不能混合搭配不同的组合。

关于c++ - Mac OSX 上 Qt App 中完全随机的 malloc 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9204046/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com