[tz] [PROPOSED 1/7] Avoid undefined behavior if no Link lines

Clive D.W. Feather clive at davros.org
Wed Oct 26 16:24:03 UTC 2022


Guy Harris via tz said:
> However, C18, at least, says
> 
> 	An array type describes a contiguously allocated nonempty set of objects with a particular member object type, called the element type. The element type shall be complete whenever the array type is specified. Array types are characterized by their element type and by the number of elements in the array. An array type is said to be derived from its element type, and if its element type is T, the array type is sometimes called "array of T". The construction of an array type from an element type is called "array type derivation".
> 
> emphasis on the "nonempty"

In all my time in WG14 (i.e. early 90s to early noughties), we were clear
that there were no zero-sized objects in C.

> There are cases where the array size can be omitted, such as
> 	extern int foo[];
>
> or
> 
> 	struct bar {
> 		int	nelem;
> 		int	elements[];	// flexible array member
> 	};
> 
> but, in the first case, I'm not sure "foo" could be zero-length (as it would have to be defined elsewhere, and you couldn't just have "int foo[0]"),

Exactly.

> and in the latter case, you could pass "elements" to qsort(), along with "nelem", but C18, at least, says of this case:
[...]

That's a special case that I got added to C99 to handle a common usage that
we'd just ruled undefined.

For the full story, see these two WG14 working papers:

http://www.davros.org/c/wg14n791.txt
http://www.davros.org/c/wg14n861.txt (item 2)

-- 
Clive D.W. Feather          | If you lie to the compiler,
Email: clive at davros.org     | it will get its revenge.
Web: http://www.davros.org  |   - Henry Spencer
Mobile: +44 7973 377646


More information about the tz mailing list