3 ms·
> C++ gives you more tools for avoiding them A direct link, for the curious: https://en.cppreference.com/w/cpp/container/array https://en.cppreference.com/w/cp
by MaxBarraclough 6y ago
> C++ gives you more tools for avoiding them
A direct link, for the curious: https://en.cppreference.com/w/cpp/container/array https://en.cppreference.com/w/cpp/container/array
As an aside, the C’s Biggest Mistake article cropped up in HN discussion 8 days ago, https://news.ycombinator.com/item?id=24373728 https://news.ycombinator.com/item?id=24373728
- saagarjha 6y agoC++ also lets you grab an array’s size via templates, FWIW.
- MaxBarraclough 6y agoDo you mean std::extent? [0] You can do the same in C if you define a macro that uses sizeof [1] This doesn't diminish the advantage of std::array, though, as it embeds the size of the array into the object, unlike when a raw array is passed and 'decays' to a pointer. [0] https://en.cppreference.com/w/cpp/types/extent https://en.cppreference.com/w/cpp/types/extent [1] https://stackoverflow.com/a/4415646/ https://stackoverflow.com/a/4415646/
- saagarjha 6y agoI mean like you can pass it through a function by using the “array syntax” when defining the parameter and making the size a template parameter. Like so: template <size_t N> void foo(int bar[N]) And this gives you the size without an additional size parameter as you’d usually need in C (of course with the limitation that the parameter now has to be a compile-time sized array).