Mac OSX上的Qt App中的完全随机malloc错误
Totally random malloc errors in Qt App on Mac OSX
#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();
}
当我运行上面的代码时,大约 1 次中有 5 次出现以下错误:
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 new
与operator delete
结合使用 - 您不能混合搭配不同的组合。
你可能想说
char* aa = new char[25];
创建包含 25 个字符的数组。
相关文章:
- 如果没有malloc,链表实现将失败
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 为什么 Serial.println(<char[]>);返回随机字符?
- 字符串-C++后显示的随机字符
- 循环中的随机函数
- 在c++构造函数中使用随机字符串生成器
- malloc() 可能出现内存泄漏
- Cuda C++:设备上的Malloc类,并用来自主机的数据填充它
- 使用std::mt19937从字符串中返回一个随机单词
- 为什么std::condition_variable notify_all的工作速度比notify_one快(对于随机请
- 如何在C++中高效地构造随机骰子
- 在类中使用随机生成器时出现性能问题
- 在将数字随机生成为数组期间从内存输出随机数的数组
- 将字符随机转换为大写的函数
- 当我尝试加载内核模块时,如何修复C++中的这个 malloc() 错误?
- 为什么 vector 的随机访问迭代器给出与指针不同的内存地址?
- 在C++中创建队列 - 什么是 malloc 错误?
- 如何在 malloc 内存中初始化非 POD 数据
- 如何生成一个随机的 n 位数,其中 n 是任意的
- Mac OSX上的Qt App中的完全随机malloc错误