I want to create a static array of instances of a struct, which has a member -- that points to a static stack-based variable-sized byte array. (can be represented as an array)
Due to the large size of the array and the fact that it holds static data -- I want to avoid heap allocations and keep the data stack-based.
A concrete example:
struct DataBlock
{
size_t id;
const unsigned char* data;
};
void test()
{
// creating an array of DataBlock, with static data of different size
DataBlock dataArray [] = {
{
.id = 1;
.data = {0xFF, 0xCC, 0XDD};
},
{
.id = 1;
.data = {0xFF};
}
};
}
If the data was of the same size, I could've just defined data member like that –
const unsigned char data[STATIC_SIZE];
But due to the fact that the length can be different for each member, this approach isn't relevant.
An obvious solution would be to replace data with an std::vector -- but again, I want to avoid heap allocations, due to the fact that the data is 100% static and known in compile-time.
I've explored some template-based solutions -- which eventually produce a separate function for every array size, which is also not a desired results.
I'm sure C++ has a solution for that sort of scenario.