C++ "pointer being freed was not allocated"
C++ "pointer being freed was not allocated"
代码很简单,如下所示:
#include <bitset>
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<void*> v;
int a1 = 4;
string a2 = "123";
vector<int> a3 = {1, 2, 3, 4};
int s1 = sizeof a1;
int s2 = sizeof a2;
int s3 = sizeof a3;
v.push_back((void*)(&a1));
v.push_back((void*)(&a2));
v.push_back((void*)(&a3));
fstream _f("test.bin", ios::in | ios::out | ios::binary);
_f.seekp(0);
_f.write((char*)(v[0]), s1);
_f.write((char*)(v[1]), s2);
_f.write((char*)(v[2]), s3);
int r1;
string r2;
vector<int> r3;
_f.seekg(0);
_f.read((char*)(&r1), s1);
_f.read((char*)(&r2), s2);
_f.read((char*)(&r3), s3);
cout << r1 << endl
<< r2 << endl
<< r3[0] << " " << r3[1] << " " << r3[2] << " " << r3[3] << endl;
_f.close();
}
它将具有以下输出:
4
123
1 2 3 4
test(51104,0x7fff9c57d3c0) malloc: *** error for object 0x7fe76fd00000:pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Command terminated
输出很好,但它说"未分配正在释放的指针",但是,我的代码中没有free()
操作,我想知道为什么会发生这种情况?
但是,r2 与 r3 没有同样的问题,下面的代码运行良好。
#include <bitset>
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<void*> v;
int a1 = 4;
string a2 = "123";
vector<int> a3 = {1, 2, 3, 4};
int s1 = sizeof a1;
int s2 = sizeof a2;
int s3 = sizeof(int)*a3.size();
v.push_back((void*)(&a1));
v.push_back((void*)(&a2));
v.push_back((void*)(&a3));
fstream _f("test.bin", ios::in | ios::out | ios::binary);
_f.seekp(0);
_f.write((char*)(v[0]), s1);
_f.write((char*)(v[1]), s2);
int r1;
string r2;
vector<int> r3;
_f.seekg(0);
_f.read((char*)(&r1), s1);
_f.read((char*)(&r2), s2);
cout << r1 << endl
<< r2 << endl;
_f.close();
v.clear();
}
输出:
4
123
当你用
以下命令覆盖向量时,会导致未定义的行为:
_f.read((char*)(&r3), s3);
如果你想要一个指向保存矢量数据的数组的指针,你需要使用 r3.data()
.而且你不能使用 sizeof r3
来获取它的大小,这是vector
对象的大小,而不是底层数组的大小。
int s3 = r3.size() * sizeof(int);
_f.read((char*)r3.data(), s3);
请参阅如何获取指向原始数据的 std::vector 指针?
相关文章:
- OpenMP卸载说'fatal error: could not find accel/nvptx-none/mkoffload'
- 使用JsonCpp将数据返回到带有pybind11的python会在python调用中产生Symbol not foun
- OpenCV Android C++ imwrite not found
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- 使用单词"not"作为C ++类的名称会导致VS2019错误
- 错误"Could not find Boost"(缺少:上下文标头)
- 如何修复"error: ‘_1’ was not declared in this scope"?
- 套接字连接"Operation not permitted"错误,甚至使用升压/平发器根.cpp
- Is !NaN not a NaN?
- 删除分配的 (?) 指针时"Pointer being freed was not allocated"
- C++编译错误,"malloc: *** error for object: pointer being freed was not allocated"
- 创建一个移动分配函数,不断得到"pointer being freed was not allocated"
- 解析 csv 文件,"malloc: *** error for object 0x7ffeeb4f4b80: pointer being freed was not allocated"出现此错误
- C++ "pointer being freed was not allocated"
- 构造函数未被调用并"pointer being freed was not allocated"错误
- 结构"pointer being freed was not allocated"
- 当我弹出时,我收到"pointer being freed was not allocated"错误
- 调试"malloc error: pointer being freed was not allocated" ,使用 gdb
- 又是"pointer being freed was not allocated"
- 仅在模拟器中" malloc error pointer being freed was not allocated "错误: