[英]C++ way of creating a structure with variable number of items
我需要在末尾創建一個包含可變數量項的內存區域。 我可以這樣寫的東西:
#pragma pack(push,0)
struct MyData
{
int noOfItems;
int buffer[0];
};
#pragma pack(pop)
MyData * getData(int size)
{
bufferSize = size* sizeof(int) + sizeof(MyData ::noOfItems);
MyData * myData= (MyData *)new char[m_bufferSize];
return myData;
}
此代碼適用於VS 2015,警告大小為零的數組不是標准的
一些搜索向我顯示這是一個C hack並且在C ++上不受支持。
我怎么能用C ++做到這一點
這樣的事情怎么樣? 它適用於簡單的可復制類型:
template <typename T, typename INT = size_t>
class packed_array {
public:
packed_array(INT size) {
buf_ = new char[sizeof(INT) + size * sizeof(T)];
memcpy(buf_, &size, sizeof(INT));
}
~packed_array() { delete[] buf_; }
void set(INT index, T val) {
memcpy(buf_ + sizeof(INT) + index * sizeof(T), &val, sizeof(T));
}
T get(INT index) const {
T temp;
memcpy(&temp, buf_ + sizeof(INT) + index * sizeof(T), sizeof(T));
return temp;
}
const char* data() const { return buf_; }
private:
char* buf_;
static_assert(std::is_trivially_copyable<T>::value);
};
int main() {
int n;
std::cin >> n;
packed_array<double, int> a(n);
for (int i = 0; i < n; i++)
a.set(i, pow(2.0, i));
for (int i = 0; i < n; i++)
std::cout << a.get(i) << std::endl;
}
template<class T, class D>
struct var_array_at_end {
var_array_at_end( std::size_t N ) {
::new( (void*)data() ) std::aligned_storage_t<sizeof(T)*N, alignof(T)>;
for (std::size_t i = 0; i < N; ++i) {
::new( (void*)( data()+sizeof(T)*i) ) ) T();
}
}
char* data() { return reinterpret_cast<char*>(this)+sizeof(D); }
char const* data() const { return reinterpret_cast<char*>(this)+sizeof(D); }
T* ptr(std::size_t i = 0) { return reinterpret_cast<T*>( data()+sizeof(T)*i ); }
T const* ptr(std::size_t i = 0) const { return reinterpret_cast<T*>( data()+sizeof(T)*i ); }
T& operator[](std::size_t n) {
return *ptr(n);
}
T const& operator[](std::size_t n) const {
return *ptr(n);
}
};
struct MyData:
var_array_at_end<int, MyData>
{
private:
explicit MyData( int count ):
var_array_at_end<int, MyData>( count>0?(unsigned)count:0 ),
noOfItems(count)
{}
struct cleanup {
void operator()(MyData* ptr) {
char* buff = reinterpret_cast<char*>(ptr);
ptr->~MyData();
delete[] buff;
}
};
public:
using up = std::unique_ptr<MyData*, cleanup>;
static up create( int count ) {
if (count < 0) count = 0;
std::unique_ptr<char> buff = std::make_unique<char[]>( sizeof(MyData)+sizeof(int)*count );
auto* ptr = ::new( (void*)buff.get() ) MyData( count );
(void)buff.release();
return up( ptr, {} );
}
int noOfItems;
};
MyData * getData(int size)
{
return MyData::create(size).release(); // dangerous, unmanaged memory
}
我相信這是標准兼容的,假設你的實現沒有在普通類型的數組(如char)上添加填充。 我不知道任何實現。
我沒想到MyData
只包含普通的舊數據; 你可以用上面的東西把std::vector
填充到它里面。 我可以用這個假設簡化幾行。
這不僅僅是一種痛苦。
auto foo = MyData::create(100)
為MyData
創建一個唯一的ptr,它后面有一個100 int
的緩沖區。 (*foo)[77]
訪問緩沖區的第77個元素。
由於標准中存在缺陷,因此在MyData
之后沒有數組,而是在相鄰內存位置包含100個不同int
對象的緩沖區。 這兩件事之間存在着令人煩惱的差異; naive指針arithimetic保證在數組內工作,但不能在緩沖區中的壓縮鄰居int
之間工作。 我不知道強制執行這種差異的編譯器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.