u/Apprehensive_Poet304

Is there a modern C++ alternative to flexible array members for variable-length struct tails?

Building a cache-optimized skip list where nodes have a variable number of forward pointers depending on their level (determined probabilistically at insertion). My naive approach was `std::array<Node*, MAX_LEVEL>` but that wastes memory since a level-1 node still allocates 16 pointer slots, most of which are nullptr. At scale this killed cache density significantly.

Currently using a C flexible array member:

struct Node {
    K key;
    V value;
    int level;
    Node* forward[];  // flexible array member
};

// allocate with extra space for forward pointers
void* mem = pool.allocate(sizeof(Node) + lvl * sizeof(Node*));
Node* n = new (mem) Node(k, v, lvl);

This works great. one contiguous allocation, forward pointers live immediately after the struct, no extra cache misses during traversal. my benchmarks show meaningful wins over std::map at 250K+ elements largely due to this cache density improvement by around 30%-40% or so.

This seems sort of messy though, and I was looking for a modern C++ alternative

I looked at std::span but it still needs a separate allocation for the pointer slots and adds 16 bytes of metadata per node. std::array requires compile-time size. std::inplace_vector (C++23) also needs a compile-time max.

Is the flexible array member still the only real tool for this pattern in modern C++? Is there something cleaner I'm missing, or is this just a gap in the language?

Using C++23, GCC, single-threaded for now.

reddit.com

How and when to cite CS Research papers

Currently I'm reading a research paper on FPGA parallelism for Limit Orderbooks. I'm planning on using it as inspiration to implement a (somewhat?) similar algorithm using CUDA, but it will of course look very different (streams, concurrency, integration with my TCP server, etc). I was wondering how should I cite this work (or if reading it as inspiration for my implementation should have a citation in the first place). I am really grateful for their work and all, I'm just a bit nervous because I have no clue how this works at all. Do I just have an MLA citation and say "hey I used their stuff as inspiration for this small part of my stuff and thus it looks a bit similar"--or would that get me into hot water. I want to do this the right way because I really respect them and I also don't want to get in trouble in the future. Any tips?

reddit.com
u/Apprehensive_Poet304 — 2 months ago