this post was submitted on 13 May 2025
7 points (88.9% liked)

C++

2002 readers
1 users here now

The center for all discussion and news regarding C++.

Rules

founded 2 years ago
MODERATORS
top 2 comments
sorted by: hot top controversial new old
[โ€“] sus 2 points 4 days ago* (last edited 4 days ago) (1 children)

what kind of psychopath even came up with int a[ROWS][COLS] = { 0, 1, 2, 3, 4, 5, 6, 7 };

it's even obviously caught by -Wall (-Wmissing-braces) for both clang and gcc

(oh, actually, g++ fails to recognize it even though gcc and clang do recognize it)

sometimes the latter part is also caught by -fsanitize=undefined, though that goes away if you wrap the array access like so:
printf("%d\n", ((int*)a[0])[i]); (which I'm unsure if that's still undefined behavior, not that it's any more sane even if it isn't)

[โ€“] ulterno 0 points 3 days ago

I came from C, but sadly haven't needed using 2 dimensional arrays enough to be able to say something useful.

I tend to use std::array and std::vector anyway.

But definitely no linear initialisation. It's already too hard to remember which pointers the first [] vs second [] refer to.