Hacker News new | ask | show | jobs
by mauvia 871 days ago
I don't know if the author is looking at this comment section but small issue:

> Array: Implements a dynamic array similar to std::array in C++.

should read "static array". std::array requires you to state the size.

C++ std::arrays are pretty much just uniformly typed tuple structures, not dynamic arrays.

In fact you can use them directly on the stack with no memory allocations whatsoever.

2 comments

Erm, actually, all STL implementations I've seen use a simple C-style array under the hood to store `std::array` data, not a much more complex tuple. But it doesn't matter much in the context of the question: the whole point of `std::array` is to provide fixed inplace storage for the data, basically just a safe version of a C-array. Implementing it on top of dynamically sized vector, like the author did, is... just missing the whole idea. This thing was the first I noticed while looking at the sources... And I don't want to look any more further now.
I think that's what parent comment meant by "tuple": a list-like structure that can't be resized.
Dynamically allocated static array might be closer to truth for what they implemented, I think.
The real std::array is static. But c doesn't really have any way to do static arrays, so c_std has decided to implement a dynamic array with an api similar to the std::array.

The documentation is correct regarding what c_std has actually implemented. And it's still useful, unlike vector the size is (dynamically) set once at construction and then enforced.

Of course C can do static array:

    int array[N]; 
is one.
But not an abstract data structure that can be allocated on the stack.

(Your example can go on the stack. A struct with a final array member of unspecified size can work as an abstract data structure. But you can't have both at once.)

Well, std::array is meant to be a direct replacement for what jenadine mentioned, the C style of doing static arrays. I think under the hood it takes over the array initialization syntax as well (or array initialization of objects got added to the standard to support std::array?) and IIRC there are some functions that are simply unavailable to std::array because of its array based construction.

I don't remember exactly what but I have run into it before, I think the iterator implementation is a raw pointer and that breaks the interface expectations a bit?