在一个特定的C++函数中,我碰巧有一个指向一个大的浮动缓冲区的指针,我想暂时使用它来存储一半的双值。是否有一种方法可以使用此缓冲区作为存储双数的划痕空间,而这也是标准允许的(即不允许未定义的行为)?
总括而言,我想这样做:
void f(float* buffer)
{
double* d = reinterpret_cast<double*>(buffer);
// make use of d
d[i] = 1.;
// done using d as scratch, start filling the buffer
buffer[j] = 1.;
}据我所知,没有一种简单的方法可以做到这一点:如果我正确理解,像这样的reinterpret_cast<double*>会由于类型混叠而导致未定义的行为,如果不复制数据和分配额外的空间,就不可能使用memcpy或float/ union,这违背了目的,而且在我的情况下,代价也很高(在C++中不允许使用联合进行类型双关)。
可以假定浮点缓冲区是正确对齐的,以便用于双倍。
发布于 2018-07-11 16:14:07
我认为下面的代码是一种有效的方法(实际上只是一个关于这个想法的小例子):
#include <memory>
void f(float* buffer, std::size_t buffer_size_in_bytes)
{
double* d = new (buffer)double[buffer_size_in_bytes / sizeof(double)];
// we have started the lifetime of the doubles.
// "d" is a new pointer pointing to the first double object in the array.
// now you can use "d" as a double buffer for your calculations
// you are not allowed to access any object through the "buffer" pointer anymore since the floats are "destroyed"
d[0] = 1.;
// do some work here on/with the doubles...
// conceptually we need to destory the doubles here... but they are trivially destructable
// now we need to start the lifetime of the floats again
new (buffer) float[10];
// here we are unsure about wether we need to update the "buffer" pointer to
// the one returned by the placement new of the floats
// if it is nessessary, we could return the new float pointer or take the input pointer
// by reference and update it directly in the function
}
int main()
{
float* floats = new float[10];
f(floats, sizeof(float) * 10);
return 0;
}重要的是,您只使用从新位置接收到的指针。重要的是安置新的后背浮标。即使它是一个非操作结构,你也需要重新启动浮标的生命周期。
忘记评论中的std::launder和reinterpret_cast。安置新人会为你做好这份工作。
编辑:在main中创建缓冲区时,请确保正确的对齐。
更新:
我只是想介绍一下评论中讨论过的事情的最新情况。
为此,我们可以:( a)通过引用传递浮点数指针并更新它,或者( b)从函数中返回新获得的浮点数指针:
a)
void f(float*& buffer, std::size_t buffer_size_in_bytes)
{
double* d = new (buffer)double[buffer_size_in_bytes / sizeof(double)];
// do some work here on/with the doubles...
buffer = new (buffer) float[10];
}b)
float* f(float* buffer, std::size_t buffer_size_in_bytes)
{
/* same as inital example... */
return new (buffer) float[10];
}
int main()
{
float* floats = new float[10];
floats = f(floats, sizeof(float) * 10);
return 0;
}https://timsong-cpp.github.io/cppwp/intro.object#1:
“当隐式更改联合的活动成员或创建临时对象时,由定义、新表达式创建对象。”
还有一个似乎令人感兴趣的部门:
https://timsong-cpp.github.io/cppwp/basic.life#1
“如果对象是类或聚合类型,并且它或它的一个子对象由一个普通的默认构造函数以外的构造函数初始化,则称为非空初始化。T类型的对象的生存期从以下时间开始:
那么,现在我们可能会说,由于双倍是琐碎的,我们是否需要采取一些行动来将琐碎的物体带到生活中去,改变实际的生命对象呢?我说是的,因为我们最初获得了浮点数的存储,通过双指针访问存储将违反严格的混叠。所以我们需要告诉编译器实际的类型已经改变了。最后一点讨论得很有争议。你可以形成你自己的观点。你现在有所有的信息了。
发布于 2018-07-11 18:15:25
你可以通过两种方式来实现这一点。
第一:
void set(float *buffer, size_t index, double value) {
memcpy(reinterpret_cast<char*>(buffer)+sizeof(double)*index, &value, sizeof(double));
}
double get(const float *buffer, size_t index) {
double v;
memcpy(&v, reinterpret_cast<const char*>(buffer)+sizeof(double)*index, sizeof(double));
return v;
}
void f(float *buffer) {
// here, use set and get functions
}第二:与float *不同,您需要分配一个“无类型”的char[]缓冲区,并使用新的放置位置将浮点数或双倍放入其中:
template <typename T>
void setType(char *buffer, size_t size) {
for (size_t i=0; i<size/sizeof(T); i++) {
new(buffer+i*sizeof(T)) T;
}
}
// use it like this: setType<float>(buffer, sizeOfBuffer);然后使用这个访问器:
template <typename T>
T &get(char *buffer, size_t index) {
return *std::launder(reinterpret_cast<T *>(buffer+index*sizeof(T)));
}
// use it like this: get<float>(buffer, index) = 33.3f;第三种方法可能类似于ph n的回答(参见我在该答案下的评论),不幸的是,由于this problem,我无法找到合适的解决方案。
发布于 2018-07-18 00:06:13
这是另一种不那么可怕的方法。
你说,
...a浮动/双联合是不可能的without...allocating额外空间,这违背了目的,而在我的情况下,它的代价却是昂贵的。
所以,让每个联合对象包含两个浮动,而不是一个。
static_assert(sizeof(double) == sizeof(float)*2, "Assuming exactly two floats fit in a double.");
union double_or_floats
{
double d;
float f[2];
};
void f(double_or_floats* buffer)
{
// Use buffer of doubles as scratch space.
buffer[0].d = 1.0;
// Done with the scratch space. Start filling the buffer with floats.
buffer[0].f[0] = 1.0f;
buffer[0].f[1] = 2.0f;
}当然,这使得索引更加复杂,调用代码将不得不进行修改。但是它没有开销,而且更明显是正确的。
https://stackoverflow.com/questions/51289394
复制相似问题