Ask HN: C with Array.size?

1 points by hnaccountme ↗ HN
What limitations or issues will vanish if C had inbuilt array size?

4 comments

[ 0.26 ms ] story [ 15.6 ms ] thread
Where to store the value?

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?

store it in the bytes before the start of the array. Maybe use a bit flag to indicate additional bytes if length is too much to store in one byte. Like unicode

I was thinking for statically defined arrays, not for file streams or dynamic memory allocation

Do you know that, for statically defined arrays, you can get the size like this:

   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);
> store it in the bytes before the start of the array

Ah, Pascal strings. There is a reason why they have lost the battle.