gpt4 book ai didi

c++ - 将任意元素存储在连续内存中

转载 作者:搜寻专家 更新时间:2023-10-31 00:29:43 26 4
gpt4 key购买 nike

我正在尝试创建一个数据结构,它将在连续内存中保存 N 种不同类型。所以在编译时我可以说我想存储 3 种不同类型的 4 个元素,在内存中它看起来像 111122223333。

我一直在使用可变模板方法,我认为它会做我想做的,但是我不确定如何在 add 方法中将元素添加到每个数组。

template<std::size_t N, typename... Args>
class Batch
{
private:
std::tuple<std::array<Args, N>...> data_;
size_t currentPos_;

public:
template<typename T>
void addToArray(std::array<T, N>& array, const T& value)
{
array[currentPos_] = value;
}

void add(const Args&... values)
{
//????
addToArray(/*array, value*/);

currentPos_++;
}

const void* data()
{
&return data_;
}
};


int main()
{
Batched<3, float, double, int> b;

b.add(1.0f, 1.0, 1);
b.add(2.0f, 2.0, 2);
b.add(3.0f, 3.0, 3);
b.add(4.0f, 4.0, 4);
return 0;
}

即使我让它工作,内存布局是否正确?有没有更好的方法?

最佳答案

我不认为这是个好主意,但是......我只是为了好玩而展示它

使用 std::vector<char> (以及 C++11 添加的方法 data() 授予的对以下内存的访问权限)和古老的 memcpy() ,我想你可以简单地按照下面的方式做

#include <vector>
#include <cstring>
#include <iostream>

template <typename... Args>
class Batch
{
private:
std::vector<char> buffer;

public:

void addHelper ()
{ }

template <typename T, typename ... Ts>
void addHelper (T const & v0, Ts ... vs)
{
auto pos = buffer.size();

buffer.resize(pos + sizeof(T));

std::memcpy(buffer.data() + pos, & v0, sizeof(T));

addHelper(vs...);
}

void add (const Args&... values)
{ addHelper(values...); }

const void * data()
{ return buffer.data(); }

void toCout ()
{ toCoutHelper<Args...>(0U, buffer.size()); }

template <typename T, typename ... Ts>
typename std::enable_if<(0U < sizeof...(Ts)), void>::type
toCoutHelper (std::size_t pos, std::size_t size)
{
if ( pos < size )
{
T val;

std::memcpy( & val, buffer.data() + pos, sizeof(T) );

std::cout << " - " << val << std::endl;

toCoutHelper<Ts...>(pos+sizeof(T), size);
}
}

template <typename T, typename ... Ts>
typename std::enable_if<0U == sizeof...(Ts), void>::type
toCoutHelper (std::size_t pos, std::size_t size)
{
if ( pos < size )
{
T val;

std::memcpy( & val, buffer.data() + pos, sizeof(T) );

std::cout << " - " << val << std::endl;

toCoutHelper<Args...>(pos+sizeof(T), size);
}
}

};


int main()
{
Batch<float, double, int> b;

b.add(1.0f, 1.0, 1);
b.add(2.0f, 2.0, 2);
b.add(3.0f, 3.0, 3);
b.add(4.0f, 4.0, 4);

b.toCout();

return 0;
}

--- 编辑 ---:添加了一个方法,toCout()打印(到 std::cout )所有存储的值;只是建议如何使用这些值。

--- 编辑 2 ---:正如 ildjarn 所指出的(谢谢!)如果在 Args... 中,这个解决方案是非常危险的types 是一些非 POD(Plain Old Data)类型。

this page中解释得很好.

我转录相关部分

An example of a type that cannot be safely copied using memcpy is std::string. This is typically implemented using a reference-counted shared pointer, in which case it will have a copy constructor that causes the counter to be incremented. If a copy were made using memcpy then the copy constructor would not be called and the counter would be left with a value one lower than it should be. This would be likely to result in premature deallocation of the memory block that contains the character data.

--- 编辑 3 ---

正如 ildjarn 所指出的(再次感谢!)使用此解决方案离开 data() 是非常危险的成员(member)。

如果有人使用这种方式返回的指针

   char const * pv = (char const *)b.data();

size_t pos = { /* some value here */ };

float f { *(float*)(pv+pos) }; // <-- risk of unaligned access

在某些体系结构中,可能会导致访问 float *在一个可以终止程序的未对齐地址中

data() 返回的指针中恢复值的正确(和安全)方法是toCoutHelper()中使用的那个, 使用 `std::memcpy()

   char const * pv = (char const *)b.data();

size_t pos = { /* some value here */ };

float f;

std::memcpy( & f, pv + pos, sizeof(f) );

关于c++ - 将任意元素存储在连续内存中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39821244/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com