Live data from Hacker News

C array types are weird

anselmschueler.com

41–50 of 148 posts

Re: C array types are weird

#41
post #33

The real lack is that C doesn't have slices. Slices can do most of what pointers into arrays can do, with sane semantics. Slices were invented surprisingly late. They were implementable in the 1970s, but didn't really show up until the 1990s. Now that we have slices, the demand for pointers into the middle of an array has much decreased. I had a go at retrofitting C with slices over a decade ago.[1] Too much politica…

Meaning it died at committee?

From what I can see in the WG14 document log [1], it never made it to the committee in the first place.

[1] https://www.open-std.org/jtc1/sc22/wg14/www/wg14_document_lo...

Re: C array types are weird

#43
post #26

C array types are weird because C doesn't really need arrays. It's not what C was about. But if you designed a language in the era where Fortran, THE array language, reigned supreme, nobody would use your language. The mindshare Fortran had is difficult to convey now, half a century later. Think of it like making a chatbot today and not mentioning AI or LLMs, that's what making a language without arrays would have fe…

i learned FORTRAN in an accelerated tech program in 1996-ish in high school.

i used fortran recently to see how "slow" python is, i did matrix multiplies by hand in .c, and .py. Now i didn't write the fortran, the AI did, but i remember enough that i verified what it did was sane, also the other two i wrote did agree with results.

  fortran 1   unit of time
  C       1.7 unit of time
  python  2.2 unit of time
for the same matmuls.

anyhow, 1996-ish. crazy.

Re: C array types are weird

#44

there's no array type in c

Yes it does. It just decays to a pointer at the slightest touch.

There are differences. E.g. va_xxx functionality may be implemented either with a pointer or an array. The difference becomes visible if you try to pass a va_list to another variadic function and then extract it later with va_arg. About half of compilers will happily do that, and another half will refuse to compile the naive version. (There's a more sophisticated proper way.)

https://stackoverflow.com/questions/79897621

Re: C array types are weird

#45

Earlier quoted context omitted.

Meaning it died at committee?

From what I can see in the WG14 document log [1], it never made it to the committee in the first place. [1] https://www.open-std.org/jtc1/sc22/wg14/www/wg14_document_lo...

Never pushed it that far. Mentioning safety in a C or C++ context was viewed very negatively back then.

Re: C array types are weird

#46

Interestingly the article doesn't mention two-dimensional arrays and they're curios because they bring a certain asymmetry with them. It always tripped me over the most in C because I otherwise find the language very "symmetrical". It often feels like in design of this language the beauty of expressing certain things took priority over readability or safety which I admire in a way. But somehow not in the case of the…

I mean, just like with 1 dimensional arrays, it depends on the context.

Array memory is on the stack. The size of that array is actually not known at run time, its only known at compile time, where any reference to that length gets resolved by the compiled.

If your 2d array sits on the stack, then inferring memory layout is pretty easy. If you are dealing with pointer that was passed to a function, then you can't assume anything about data size or limits, which is why many functions that take pointers take a size parameter as well.

Re: C array types are weird

#47
post #45

Earlier quoted context omitted.

From what I can see in the WG14 document log [1], it never made it to the committee in the first place. [1] https://www.open-std.org/jtc1/sc22/wg14/www/wg14_document_lo...

Never pushed it that far. Mentioning safety in a C or C++ context was viewed very negatively back then.

We are working towards this though and a lot of this already works:

https://godbolt.org/z/EP3cP3qGs

Re: C array types are weird

#48

Interestingly the article doesn't mention two-dimensional arrays and they're curios because they bring a certain asymmetry with them. It always tripped me over the most in C because I otherwise find the language very "symmetrical". It often feels like in design of this language the beauty of expressing certain things took priority over readability or safety which I admire in a way. But somehow not in the case of the…

"breaks completely"

I rather would say it works nicely in auto-generating the complex indexing operation for n-dimensional arrays which makes it a lot more convenient and less error-prone to write such code. The compiler may also flatten a loop.

The array of pointer hack used previously to similate 2d arrays using an array to pointers to arrays should not be used outside of special algorithms, as it is error prone and slow.

Re: C array types are weird

#49
post #15
post #2

In practice, the [static n] notation can give you useful warnings and bounds checking. https://godbolt.org/z/PzcjW4zKK And while the (*array_ptr)[3] notation take a moment to get used to, it is very logical. If you have a pointer to an array, you dereference it first and then indx into it. Again, useful for bounds checking: https://godbolt.org/z/ao1so9KP7

I know of this notations but I don't see many people using [static n]. Not sure why, maybe it doesn't feel like C anymore, maybe it feels hacky? typically if you're passed an array you'd want to get more anyway, so you'd get passed a struct. Not sure.

I don't know. I see people increasingly make use of it. The problem was that in the past compilers ignored this completely, so there was simply no point. Nowadays GCC uses it for warning (the length for bounds and "static" for nonnull), so it starts to become useful.

Re: C array types are weird

#50
post #47
post #45

Earlier quoted context omitted.

Never pushed it that far. Mentioning safety in a C or C++ context was viewed very negatively back then.

We are working towards this though and a lot of this already works: https://godbolt.org/z/EP3cP3qGs

It is a struggle though to get the improvements through the committee. Especially the C++ folks from the Clang side fight very hard against it, this is - for example - why we not have forward declarations where I already had weak consensus, but the clang area team made it clear they will never implement it.
Post reply on HN