Hacker Read top | best | new | newcomments | leaders | about | bookmarklet login

A plain fixed size C array embedded in some data structure somewhere?

Doesn't seem that big a stretch to imagine.



sort by: page size:

Indeed, this has been done many times before and more carefully. I don't know why this was newsworthy, we used to implement dynamically-sized arrays for our programs in C 20 years ago ... :-/

Is there a plan to add a generic resizable array type? That's probably the number one thing I miss in C.

Or perhaps just some helper functions in C that wrap array and pointer allocation/access to provide sanity checks. Seems like moving to a new language is rather extreme....

You can if you make them fixed-size arrays inside a struct. I agree with the thesis of the article.

Dynamically allocated static array might be closer to truth for what they implemented, I think.

Couldn't it be done with a fixed-size array of runes or bytes? It's definitely possible in C/C++.

None of the primitive data structures will match up. JVM languages, CLR languages, Rust, Go, et al all have array bound checking, so their arrays won't be compatible with C libraries without a wrapper.

Why not just implement this as an array of constant width pointers to the heterogeneous data types?

C has actual arrays; what's missing is actual array references: C uses pointers to the first element and implicit pointer arithmetic. Rust's slice types are a start, but I'd also like to see checked indexes:

    size_t zfoo = ...;
    /* todo: better typecheck of malloc */
    int[zfoo]* foo = malloc(zfoo * sizeof *foo);
    [zfoo] ifoo = ...; /* allowable values 0,1,...,zfoo-1 */
    /* zero-length arrays cause problems here */
    return foo[ifoo]; /* no runtime check; type guarantees in-bounds access */
There are several problems with this, but it sure would be nice.

0: Stupid dependent type system tricks: have x % y typed as [y], have x < y typed as Maybe([y]), make for([n] i=0; i<n ;i++) do the useful thing.


Nice, yeah arrays + structure packing looks like it would do the trick.

The author didn't say it was impossible altogether -- just that he would want actual malleable first-class multi-dimensional array types.

Try writing a generic, reusable data structure in C. It's agony.

Of course C can do static array:

    int array[N]; 
is one.

No, I am not talking about a runtime-allocated structure. I’m talking about a function that can accept and operate on arrays of different sizes, all of which are fixed size at their declaration site, and all stored on the stack.

There is no reason this shouldn’t be possible. It should not require fat pointers at all because the size information is known at compile time.


It's common to structure data as arrays in Fortran and other scientific computing. No reason you couldn't in C or C++.

What do you mean by “static” exactly? It could be referring to an array that doesn’t change size. Or the contents are read-only. Or do you mean C/C++ static linking? Or something else?

In C, C++ and CUDA I use constant fixed-size arrays with lookup tables of specialized data all the time. I also use small arrays to hold temporary results that need to get first collected, and then sorted or processed together. Seems like very common in C++, especially embedded, game, and GPU programming. I’d love to hear about why static arrays seem uncommon in your world.


Yes, needing a fixed-sized array seems like a fairly common need to me.

    a := [5]int32{0, 1, 2, 3, 4} // Go

    let mut array: [i32; 5] = [0, 1, 2, 3, 4]; // Rust

Having real arrays would make both the compiler faster and array operations faster.

range and mod types as in Ada would also squelch a bunch of C's shitastic undefined behavior bullshit.

And your comment about just implementing it via a feature flag would be the way to go.


LLs are often used to manage fixed sized object pools. So yes, statically sized arrays, but with pointers between the elements acting as linked lists.
next

Legal | privacy