Live data from Hacker News

C array types are weird

anselmschueler.com

81–90 of 148 posts

Re: C array types are weird

#81

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…

As I recall, C# supports this in a completely sensible way by distinguishing a[i,j] and a[i][j]. If I understand right, in C, a[i][j] means what C# would spell a[i,j], which does seem rather surprising and inconsistent

In C, a[i][j] can mean either a[i,j] or a[i][j], depending on the type of a.

Re: C array types are weird

#82
post #69
post #42

Earlier quoted context omitted.

And yet so few other languages attempt to have a stable ABI (other than by...mimicking C).

C doesn't have a stable ABI, most folks mistake the ABI from OS written in C, with an imaginary C ABI.

OS ABIs are usually stable though ;)

Finally someone getting it right though. C doesn't have any ABI per se, compilers need to implement whatever ABI a specific operating system defines (otherwise the language would be pretty useless since it couldn't call into operating system functions).

Re: C array types are weird

#83

It's still weird to me that you can declare an array with the register keyword. Then it (understandably) becomes UB to attempt to get the pointer. (It also probably isn't stored in a register, since the keyword is just asking the compiler nicely.)

The meaning of the 'register' keyword has changed over time to just "it's illegal to take the address of this item": https://www.godbolt.org/z/TKq9rWzP1 Don't know what's the idea behind not allowing to take the address of a value though.

It was always only a suggestion to the compiler, to hold this variable in a register.

Compilers got so good at optimization that there is little point using it.

If a variable is held in a register you can't access it with a pointer. So if your intention is it should be in a register you can't take the address.

Re: C array types are weird

#84

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 ass…

> Array memory is on the stack.

Array memory can sit on either the stack or the heap.

> 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.

This is also a bit misleading, in two ways. First, it's not clear what you mean by "size" here - the size of the memory block(s), or the shape of the array?

Second, many people think that the C runtime doesn't know the amount of memory allocated to an array, but this is actually false. It's just the C abstract model that for some reason chose to not expose this information - but the size is actually always stored and accessible, and this is virtually mandated by the standard: otherwise, `free(arr)` couldn't realistically work, it would have to be `free(arr, size)`. This is one of the weirdest inefficiencies of C, in fact - it requires you to store the size of arrays twice - once in user code, and another time in the internal logic of the allocator.

Edit: and as a fun extra, C++ not only inherited this mistake from C, but reproduced it again, meaning that a C++ array allocated with new[] actually stores the size twice, at least with typical implementations - once in the C++ runtime and again in the allocator - and still requires the user-space code to store it a third time. This is because `delete[]` needs to call the destructors of all of the elements of the array, regardless of where and how the array was allocated, so the number of array elements needs to be stored alongside the object itself.

Re: C array types are weird

#85
post #50
post #47

Earlier quoted context omitted.

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.

But C and C++ do have forward declarations [1]... can you elaborate on what exactly you were trying to promote that was rejected?

[1] : https://en.wikipedia.org/wiki/Forward_declaration

Re: C array types are weird

#86
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…

this exactly. if you need arrays or sequences of objects/memory items u can trivially implement them :/. why does it need to be embedded in the language?? people want the language to do all the programming for them. I suppose this is why they like LLMs too..

they should pay programmers less. get rid of all these moneygrabs

Re: C array types are weird

#87
post #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 an…

> The compiler may also flatten a loop.

http://c2.com/cgi/wiki?SufficientlySmartCompiler

In practice, C compilers are still notoriously bad at loop optimizations.

Polyhedral optimizations provided some hope, but no compiler managed to adopt it in production.

Re: C array types are weird

#88

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…

And just in case you have not come across this, C++ allows you you overload all the relevant operators here: [], *, ->

So, you really can't tell what's going on behind the scenes.

I wanted to pull my hair out seeing some 'enterprise' code use

  state[i] = foo;
for some kind of logging where i was the severity level. There were even instances of state[i++], where the severity was incremental. I hope someone has rewritten that codebase with AI by now.

Re: C array types are weird

#89

Paging walter bright

At your service! D fixed it, and I'm sorry C users have suffered as the array-to-pointer decay blasted their kingdom. Fixing it in C is easy and should be the #1 priority.

At this point, C's #1 priority is not breaking things in existing implementations, though, Walter...

Re: C array types are weird

#90
int x[n] and int *x are very different things when it comes to defining memory layout tho. In one case you end up with n int sized slots of memory, in the second with one register sized slot. That makes all the difference when defining structs for example.
Post reply on HN