Does D actually support flexible array members?

LinguisticMystic Egor.Sozonov.09 at gmail.com
Thu Aug 18 08:41:02 UTC 2022


I'm porting some C code for arena allocator to D, and somehow the 
flexible array members (a feature of C99 for dynamically-sized 
structs) work in D without significant changes in the code. 
Here's my arena definition:

```
struct ArenaChunk {
     size_t size;
     ArenaChunk* next;
     char[] memory; // flexible array member
}

struct Arena {
     ArenaChunk* firstChunk;
     ArenaChunk* currChunk;
     int currInd;
}
```

And here's how I use the FAM's memory for allocating stuff:

```
     void* result = cast(void*)(&ar.currChunk.memory + ar.currInd);
```

This seems to work, but I'm a little doubtful, does D really 
support FAMs in the same way as C, or am I misusing some other D 
feature here? I mean, FAM's aren't even supported by C++, and 
aren't listed on [the D 
reference](https://tour.dlang.org/tour/en/basics/arrays)  yet 
somehow the code works.



More information about the Digitalmars-d-learn mailing list