[英]C++ Save and load float, addressed by a pointer, to a binary file
I'm trying to make a "Save / Load" sequence to a dynamic array of floats (float*). 我正在尝试对动态浮点数(float *)进行“保存/加载”序列。 I have an array that holds the data as a
float4 *dst;
我有一个数组,将数据保存为
float4 *dst;
. 。
I have this piece of code for saving the array 我有这段代码用于保存数组
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();
This is the error that I'm getting: 这是我得到的错误:
Unhandled exception at 0x00007FFDC83316EE (vcruntime140d.dll) in myproject.exe: 0xC0000005: Access violation reading location 0x000000FECA900000.
but when I remove the sz
from the pointer error line, it works fine (though obviously it doesn't get to the entire array) 但是当我从指针错误行中删除
sz
时,它可以正常工作(尽管显然不能到达整个数组)
What's am i missing? 我想念什么? is the write function limited by memory?
写入功能受内存限制吗? and if so, how can I overcome this issue?
如果是这样,我该如何克服这个问题?
just do 做就是了
outS.write((char *)d_array, 4 * sz * sizeof(float));
d_array
values the address of the array whose content has to be saved d_array
值必须保存其内容的数组的地址
using &d_array
you (try to) save the address of d_array
then the memory after it 使用
&d_array
您(尝试)保存d_array
的地址,然后保存其后的内存
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.