Register
handmade.network»Forums»Fixed size arrays in C++
103 posts
Fixed size arrays in C++
2 years ago Edited by pragmatic_hero 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?



Mārtiņš Možeiko
2000 posts / 1 project
Fixed size arrays in C++
2 years 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.
103 posts
Fixed size arrays in C++
2 years ago Edited by pragmatic_hero 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?
Mārtiņš Možeiko
2000 posts / 1 project
Fixed size arrays in C++
2 years 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... :(
477 posts
Fixed size arrays in C++
2 years 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
103 posts
Fixed size arrays in C++
2 years ago Edited by pragmatic_hero 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.