C++ Logo


Advanced search

Re: [wg14/wg21 liaison] (SC22WG14.18841) [Fwd: sudo buffer overlow]

From: Niall Douglas <s_sourceforge_at_[hidden]>
Date: Sat, 30 Jan 2021 15:00:55 +0000
On 28/01/2021 21:48, Uecker, Martin via Liaison wrote:

> In C we discussed something like:
> struct foo {
> int n;
> float (*ap)[.n];
> };
> An alternative could a wide pointer type:
> struct foo {
> float (*ap)[:];
> };
> If one does not want to introduce a general wide pointer
> type, one could allow this only inside aggregates.
> The bound would be stored in a hidden variable
> and set automatically when the pointer is assigned.
> This should be relatively simple to specify.

Speaking personally, I have always felt C could sorely do with a
language built-in equivalent to span<T>/string_view from C++ as a
lightweight range. Even if built-in native objects for arrays or strings
is too much to ask for now, just spans/views (borrowed refs in Rust
parlance) would be an enormous value add.

The most obvious formulation of implementation is a wide pointer type,
as you suggest. So, you'd have some bits for indicating which kind of
wide pointer type it is, the pointer itself, and either one or two
offsets from that pointer. The zero, one or two offsets may immediately
follow the pointer value, or live just before the pointed at storage.

On top of such a fundamental construct, you can build fixed capacity
array storage but with dynamically resized contents i.e. a deterministic
std::vector. You can build strings, arrays, with or without runtime
enforced checking. You can pass around subslices of all those, safely.
The opportunities enabled here are huge.

I think the chances of C++ adopting VLAs are indistinguishable from
zero. But C supporting a subset of C++ ranges (specifically, contiguous
ranges), well that I can see plenty of WG21 support for, in principle at


Received on 2021-01-30 09:01:03