[ub] C provenance semantics proposal

Jens Maurer Jens.Maurer at gmx.net
Thu Apr 11 15:59:22 CEST 2019


On 11/04/2019 14.58, Arthur O'Dwyer wrote:
> On Wed, Apr 10, 2019 at 6:28 PM Jens Maurer <Jens.Maurer at gmx.net
> <mailto:Jens.Maurer at gmx.net>> wrote:
>
>     On 11/04/2019 00.06, Jens Gustedt wrote:
>      > On Wed, 10 Apr 2019 23:40:37 +0200 Jens Maurer
>     <Jens.Maurer at gmx.net <mailto:Jens.Maurer at gmx.net>> wrote:
>      >>> Inter-object equality comparison has to be supported,
>      >>
>      >> "Supported" in the sense of "getting a well-defined, stable answer",
>      >> I presume. Why do you need that?
>      >
>      > Because this is all that pointer equality is about.  I have to be
>     able
>      > to compare pointers to whatever objects for equality. If I can only
>      > use it for pointers for which I know that they point to the same
>      > object, I don't a need a `==` operator in the language :)
>
>     Not quite.  It does make sense to compare pointers to subobjects
>     within the same larger object,
>
>
> And also to compare pointers to different objects.
>
>      void somefunc(int n) {
>          char local_buffer[100];
>          char *p = (n > 100 ? malloc(n) : local_buffer);
>          use(p);
>          if (p != local_buffer) free(p);
>      }
>
> This idiom is supported by standard C and C++ today (that is, equality
> comparison of arbitrary pointers is supported today), and there is lots
> of code in the wild that relies on this idiom continuing to work.
> (libstdc++'s std::string
> <https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/include/bits/basic_string.h#L222>
> and libc++'s std::function
> <https://github.com/llvm-mirror/libcxx/blob/master/include/functional#L1732>,
> for example. But also a lot of industry code.) Any proposal to change
> C/C++ so that this idiom stops working would be a non-starter IMHO. So
> it's good that this "provenance" work doesn't propose to change this
> aspect of C/C++.

Thanks, this is a useful example.

I think I confused myself by reading the special "one-past-the-end
may or may not be equal to the next object" exception in C++
as applying to any disjoint object.  Sorry for the noise.

Jens


More information about the ub mailing list