How not to check array size in C++
How not to check array size in C++
How often do you see the sizeof(array)/sizeof(array[0]) statement used to get the size of an array? I really hope it′s not too often, because it′s 2024 already. In this note, we′ll talk about the...
You're viewing a single thread.
Tbh if you aren’t already using someone else’s array implementation that includes length information, just write your own simple wrapper e.g.
struct MyArrayWrapper { int *data; int length; }
4 1 ReplyAt that point I would just use std::span if you can, then you also get the standard container/iterator interfaces for free.
2 0 ReplyThe article did mention that that's what you'd probably have to do.
I have only one pointer (for example, if you created an array using new)
In most cases, it's necessary to rewrite the program a bit and add an array size passing. Sadly, that's how it works.
1 0 Reply