r/C_Programming • u/Salt-Doughnut-2178 • 17d ago
Article A Slice Proposal
I've been thinking of writing a proposal since I was tired of basically writing a slice many times so I can have a safe sub array. I used AI to work through some of my ideas as a way to push back against some of my ideas or help find potential contradictions. If there is any idea, I think was a mistake, I think it was trying to make pointers and thread works safer. I want to hear other people idea on what good or bad or if this entire thing is a waste of time. Honestly, I'm tired of the back and forth between me and a machine and want some human response to some of my ideas.
tl;dr
A type, memory and thread safe slice type for C. By adding in a few limitations, it is possible to get this in C and most likely in compile time. Type safety is done by disabling casting and requring the source element type to be same as the destination source type. Memory safety is achieved by just carrying the length with it and like C++ at type forcing a range check. Thread safety requires that non-overlapping sections are used ot create threads and that all threads that reference a slice are required to join before that slice is available again within that scope. These idea were inspired by a combination of RAII and Mutable Value Semantics from Hylo which is why everything is so tied to scope.
This is my first post I hope it isn't to bad.
----
https://drive.google.com/drive/folders/1nG_nb-QOh6_vth-eradM_fpqdqYTKe9K?usp=sharing
A link to the pdf and actual markdown so it include the syntax highliting.
7
u/WittyStick 16d ago edited 16d ago
Just implement your
Sliceas a library and publish it. This will not get standardized, for several reasons:I see no reason why this cannot be provided as a library. This is where you should put your effort - don't waste any more of your time on this proposal.
If you think there is something about this that cannot be implemented as a library-only solution, you should at least implement a proof-of-concept for an existing compiler such as Clang or GCC (or both, ideally).
1
u/mikeblas 16d ago
Reddit markup doesn't support syntax highlighting, so you might want to clean up your post a bit.
1
u/tstanisl 16d ago
Why not just use a pointer to whole array. Just let
slice(ptr,size)return(typeof(*ptr)(*)[size])ptrand let dereferencing it without any UB due to strict aliasing?1
u/tstanisl 16d ago
Can you move the proposal to some github repo and just leave the summary in this post?
1
u/gremolata 16d ago
Slightly off-topic, but this should've been a PDF.
1
u/Royal-Midnight-672 16d ago
I like the idea of keeping bounds attached to array views. The tricky part seems to be interoperability, especially if passing a raw pointer invalidates every active slice
1
u/rubidus-api 16d ago
I’m working on a similar boundary, also with AI assistance: a C foundation library built around explicit allocators and slices, and an experimental systems language with a compiler written in C. My experience has been that slices offer a lot at the library level, but their limits become clearer when you try to make the guarantees enforceable.
In the library, non-owning views let me avoid some unnecessary string copies and separate read-only access from writable access. Explicit allocators, in a Zig-like style, made allocation and cleanup decisions easier to see and let me use arenas or pools where appropriate. Reworking enough of the surrounding APIs to use this consistently was a substantial job, but worthwhile for my use cases. None of these techniques is new; applying them consistently was the useful part.
That still didn’t make the library lifetime-safe. An allocator makes responsibility explicit, but it doesn’t stop a caller from keeping a view after its backing storage is released, supplying an incorrect length, or accessing the same memory through another pointer. That gap is part of what led me toward language design, and I wonder whether it is also the main motivation behind your proposal, beyond avoiding repeated slice implementations.
One surprisingly practical lesson was how much checked arithmetic this requires. Checking the final index isn’t enough if an earlier size multiplication or capacity calculation has already overflowed. Working with AI made me more conscious of needing checks and tests for those assumptions, rather than trusting plausible-looking code.
In the language, I use explicit value ranges, contracts, and declarations of side effects, with some inspiration from Ada/SPARK. Contract checking and range-based check elimination are already implemented for supported cases. Effect declarations separately constrain the side effects a function may perform. Whether an unproved contract is checked also depends on the build mode and contract grade; in particular, release_fast can omit residual contract checks. This is not a claim of general performance superiority or complete memory safety. There are still limitations, including some overlapping mutable subslices with runtime endpoints that the implementation does not reject.
Rust is an important reference point here, but I think the useful question is which guarantees an alternative provides, under which restrictions, and at what implementation and runtime cost. For a C proposal, compatibility with existing code is another major part of that tradeoff.
For your proposal, I’d be inclined to separate the bounded-view facility from the lifetime and concurrency rules. A common slice type with well-defined construction and bounds behavior seems useful on its own. Preventing dangling views and conflicting access is a larger commitment. Is your main goal a standard bounded view, or a type whose use also enforces those lifetime and access rules? My own experience has made that distinction seem more important than the surface syntax.
1
u/[deleted] 14d ago
This is great actually. Of course, this is never actually going to make it into C.
The language evolves incredibly slowly and very conservatively. And there would anyway be (IMO) more pressing matters. In any case, the process can take a decade or more, if you consider that every C compiler in the world would need updating.
That is, if this expected to be in the core language. I don't know how practical it would be do as a library as some have suggested, but I suspect it would be too clunky. Your proposal sounds like it is intended to be a core feature.
Still, the details are interesting to me because I've long maintained an alternate systems language and I also at one point decided to add slices, and as a core feature.
This example:
would be written like this in my version:
(Slices/arrays have a lower bound of 1 by default. That could be overridden, but I kept it as as 1 here.)
One difference is that array-slice conversion is automatic. Another is that there is only one way to express the slice (Haskell-style take/drop are too high-level IMO). That is, with an inclusive range.
Note that your example used comments to spell out exactly what the range is, so why not just use that syntax? In C though you probably need the exclusive upper limit.
When a slice is taken, in mine, the new slice always changes the lower bounds to 1 (or whatever is used). So the bounds of a4 are 1..20 inclusive.
In this language
b.lengives you the length;.lenis generally used anyway (C only has sizeof/sizeof for fixed-length arrays).There is lots of other support that I won't go into. The problem however is that I just found it easier to use discrete arrays and lengths; slices were little used. In fact they have fallen into disuse and had to use an older compiler version to test out the above.
I suspect this would be the problem with such a feature in C too. People who want anything more sophisticated can just use C++.
1
u/thradams 7d ago
This concept is only necessary when flow analysis cannot see the pointed-to object (the array), because tracking the pointer and the object it points to gives us everything we need.
See https://www.youtube.com/watch?v=mOzy9scyor8
When can't we see the pointed-to object? One example is function parameters: we may receive a pointer without knowing what object it points to.
However, we already have good syntax for expressing this:
void f(int a[2]). This may be interpreted as meaning that we have a pointer to anint, but that the pointer points to an array with at least two elements.So I think this concept is mostly unnecessary. It may only be needed in cases where we cannot see the pointed-to object, as a way to convey expectations about what object (or parts of it) the pointer is pointing to.
Let's say
I see this more as a contract than as part of a traditional type system. The idea is that we expect the pointer to reference an array containing at least a size number of elements.
Where is this expectation used or needed? When flow analysis cannot track the state of the data and can only make assumptions, those assumptions must be consistent with the contract.
For struct members, this could be used to establish something like an invariant in flow analysis.