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 ... :-/
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....
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.
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.
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.
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.
Doesn't seem that big a stretch to imagine.
reply