handmade.network » Forums » Fixed size arrays in C++
pragmatic_hero
86 posts
#14115 Fixed size arrays in C++
3 months ago Edited by on Jan. 24, 2018, 6:16 a.m. Reason: Initial post

So there's no way to have a fixed-size bounds-checked arrays in C as far as I know.

The *standard way* to do this in C++ is std::array which is templatized std::array<type, max_size> as in std::array<int, 6>.

With overloaded operator[] which does bounds-checks in debug (at least in MS implementation, GCC's impl doesn't do it by default I think).

DOOM-3-BFG has a idArray which is essentially the same thing with less bullshit.

The killer question here is simple, how hard does this tank the compile times if used in the *large*?

Because for the most part, all that's ever needed is whole bunch of fixed sized arrays.
And everything else is built on top of it (open-addressed hashmaps, packedarrays, freelists).

Every new std::array<Tulip, 100> will instantiate a new template and shit out a ton of code, and in a code-base of reasonable size this will take forever to compile, right?



mmozeiko
Mārtiņš Možeiko
1638 posts
1 project
#14118 Fixed size arrays in C++
3 months ago Edited by Mārtiņš Možeiko on Jan. 24, 2018, 8:32 a.m.

Macros?
Or C99 functions with static size arguments if arrays are fixed size.
pragmatic_hero
86 posts
#14119 Fixed size arrays in C++
3 months ago Edited by on Jan. 24, 2018, 8:52 a.m.

mmozeiko
Macros?
Or C99 functions with static size arguments if arrays are fixed size.

What do you mean?
mmozeiko
Mārtiņš Možeiko
1638 posts
1 project
#14121 Fixed size arrays in C++
3 months ago

Something like this:
1
#define GET(arr, i) (i < get_array_size(arr) ? fatal_assert() : arr[i])


As for C99, it actually seems static array sizes in arguments work only for optimization, no warnings for bounds checking... :(
ratchetfreak
351 posts
#14122 Fixed size arrays in C++
3 months ago

Simple use of templates like you would have with std::array barely affect compile times in any measurable way.

It's the overuse where everything is a template instantiation that tanks compile times
pragmatic_hero
86 posts
#14124 Fixed size arrays in C++
3 months ago Edited by on Jan. 24, 2018, 5:33 p.m.

ratchetfreak
Simple use of templates like you would have with std::array barely affect compile times in any measurable way.

It's the overuse where everything is a template instantiation that tanks compile times

But std::array is exactly that. Every array is a template instantiation. EVERY ARRAY.

It's not like a container where the types might overlap. Since the signature is Type x Size, its unlikely to ever overlap and reuse anything.

C++ tanks compile times by design. Not necessary from over-use of anything.
It's hard to predict over lifetime of a project how something seemingly innocuous might tank it.

The whole C++ standard library tanks compile times.
Using C++ as per Bjarnie and "Modern C++" would be std::vector (20k) + std::string (20k) + std::map(20k). There 60k lines out of nothing. Multiplied by meagre 10 compilation units, half a mil LoC.

Not that far fetched to assume that every new std::array instance would be like shitting out 300 loc compile time wise. 10 fixed size arrays? 3000 LoC! Hyperbole? I hope so.