【发布时间】:2019-03-11 16:21:17
【问题描述】:
我正在尝试将“保存/加载”序列添加到动态浮点数组 (float*)。我有一个将数据保存为float4 *dst; 的数组。
我有这段用于保存数组的代码
int sz = properties.height * properties.width;
float * d_array = (float*)malloc(4 * sizeof(float) * sz);
for (size_t i = 0; i < sz; i++, dst++)
{
d_array[4 * i + 0] = dst->x;
d_array[4 * i + 1] = dst->y;
d_array[4 * i + 2] = dst->z;
d_array[4 * i + 3] = dst->w;
}
// Up to this point, everything works great
ofstream outS(backupdata, ios::out | ios::binary);
outS.write((char *)&d_array, 4 * sz * sizeof(float)); // <- This is where the code breaks
outS.close();
这是我得到的错误:
Unhandled exception at 0x00007FFDC83316EE (vcruntime140d.dll) in myproject.exe: 0xC0000005: Access violation reading location 0x000000FECA900000.
但是当我从指针错误行中删除 sz 时,它可以正常工作(尽管显然它没有到达整个数组)
我错过了什么?写入功能是否受内存限制?如果是这样,我该如何克服这个问题?
【问题讨论】:
-
&d_array是指针的地址。您不想将指针写出到文件中,无论如何它在您正在运行的进程之外没有任何意义。只需删除&。