Live data from Hacker News

C array types are weird

anselmschueler.com

91–100 of 148 posts

Re: C array types are weird

#91

Earlier quoted context omitted.

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…

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

No, if we are using the definition of an array that is like int c[] = ..., that is always going to be on the stack. Heap continuous memory =/= array. You can use the [] operator to access it like an array, but fundamentally, as far as structures in C language are concerned, those 2 are different, because they get treated by compiler differently.

>but the size is actually always stored and accessible, and this is virtually mandated by the standard: otherwise, `free(arr)` couldn't realistically work,

That would only be true if each element in the array was a char.

The dynamic data structure stores total amount of memory allocated by address, it has no info about the size of the element, so it can't infer the actual number of items at runtime. You could write your own malloc that does this, but generally, that is left to the user for flexibility. For example, a really good practice in C coding that basically solves any double free is a mempool that allocates all the memory up front. That way, you never really even have to call free, and the memory you allocate can be partitioned any way you chose dynamically.

Re: C array types are weird

#92

Earlier quoted context omitted.

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.

It once told the compiler to hold the value in a register because the compiler wasn't very smart at all.

Re: C array types are weird

#94

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.

A register isn't in external memory, so isn't addressable as such. That part makes sense since if the compiler actually follows your suggestion it can't be addressed.

Thinking about it, storing arrays in registers would possibly make sense on systems like the 8051 where you actually have a bunch of general purpose register banks, but those don't exist in x86.

Re: C array types are weird

#95
post #9

This is one of the things that I feel is an inappropriate abstraction that is around for historical reasons. When I do FFI to call C from rust, I usually wrap the generated API (Which is pointer based) into rust's &[] array syntax. Arrays/lists/Vecs etc in most non-C languages feel like an abstraction over a collection of items; I feel like C's exposing the pointer directly is taking a low-level memory/MMIO operation…

This talk – "Programming without pointers" – by Andrew Kelley may be interesting to you. https://www.hytradboi.com/2025/05c72e39-c07e-41bc-ac40-85e83...

Interesting talk, thanks for sharing!

Re: C array types are weird

#96

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…

> If you see a[i][j] it could mean two completely different things:

> 1) ... a[i][j] == *((char*)a + i*M + j) // I added the char* cast to make it correct

> 2) ... a[i][j] == *(*(a + i) + j)

You may already understand this but: even in case (1), you still have

   a[i][j] = *(*(a + i) + j)
(It has to - that's what operator[] means in C.)

It's just that, in this case, `a + i` is applying pointer arithmetic to char[M]* so it adds M * i bytes to a's address.

This is similar to how `a + i`, if a is int32_t*, will give you an address 4 * i bytes bigger than a.

Really the confusing part of this is that *(a + i), which is an array value i.e. has type char[M], decays to char* when you add an integer to it (or dereference it). This is a pretty crazy hack really. Imagine if, in C++, you could do this

   std::vector v = {1, 2, 3};
   int* x = v + 1;   // equivalent to &v[1]
Yuck.

Re: C array types are weird

#99

Earlier quoted context omitted.

> Because C is an exceptionally unopinionated language. Compared to what? Anyway I find it hard to believe enabling suicide is a good thing

> Compared to what? ...for somewhat recent languages: Rust, Zig, Odin... (with differing intensity, but they all want to nudge the programmer into a certain direction of how to do things). > Anyway I find it hard to believe enabling suicide is a good thing "War is peace, Freedom is slavery, Ignorance is strength" ;) Seriously, if people want to write their programs in assembly code or whatever weird kink, just let th…

[flagged]

Re: C array types are weird

#100

Earlier quoted context omitted.

C is hardly any path forward

It's not much different from any other high level programming language. Just a different set of compromises to accept.

Yes why would anyone choose to make a fool of themselves when they could choose to build instead?
Post reply on HN