Ask HN: C with Array.size?
What limitations or issues will vanish if C had inbuilt array size?
How often to assure it is still correct?
What about dynamic streams?
Do you dream it will prevent stack overflow or out of bounds access?
I was thinking for statically defined arrays, not for file streams or dynamic memory allocation
int foo[10];
int ten = sizeof(foo)/sizeof(foo[0]);
Of course, that doesn’t work if you do that with a function argument because you can’t pass arrays as arguments, only pointers, so a function can’t know whether a passed value is a statically defined array or dynamically allocated. int f(int * foo) {
return sizeof(foo)/sizeof(foo[0]); //
}
int notTen = f(foo);
That even is the case when you do things like these (https://stackoverflow.com/a/2375038): int f(int foo[10]) {
return sizeof(foo)/sizeof(foo[0]);
}
int notTen = f(foo);
or int f(int foo[static 10]) {
return sizeof(foo)/sizeof(foo[0]);
}
int notTen = f(foo);Ah, Pascal strings. There is a reason why they have lost the battle.