Article View: comp.lang.c
Article #406Re: AVL tree insertion - programming exercise
From: Keith Thompson
Date: Wed, 14 Oct 2026 22:42
Date: Wed, 14 Oct 2026 22:42
114 lines
6481 bytes
6481 bytes
cross@spitfire.i.gajendra.net (Dan Cross) writes:
> In article <11amvh2$14pou$3@kst.eternal-september.org>,
> Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
>>cross@spitfire.i.gajendra.net (Dan Cross) writes:
>>> In article <86v775h9o2.fsf@linuxsc.com>,
>>> Tim Rentsch <tr.17687@z991.linuxsc.com> wrote:
>>>>cross@spitfire.i.gajendra.net (Dan Cross) writes:
>>[...]
>>>>> I don't think this is true anymore, as of C23. Here's the
>>>>> relevant text from n3220, sec 7.24.3, describing the return
>>>>> value from `malloc` et al:
>>>>>
>>>>> |The pointer returned if the allocation succeeds is suitably
>>>>> |aligned so that it may be assigned to a pointer to any type of
>>>>> |object with a fundamental alignment requirement and size less
>>>>> |than or equal to the size requested.
>>>>>
>>>>> This sentence is somewhat ambiguous because it's unclear whether
>>>>> the "less than or equal to the size requested" criteria applies
>>>>> to both the pointer type's size _and_ fundamental alignment
>>>>> requirement, or _just_ the size, [...]
>>>>
>>>>It's poorly constructed but in English it has only one
>>>>grammatical parse.
>>>
>>> Nope. Not even close.
>>
>>I think Tim is right about there being only one grammatical parse.
>
> No, he is not. There are over a thousand ways to parse that
> sentence "grammatically", according to the "standard" rules of
> the English language (a language infamous for having little in
> the way of standard rules).
>
> We tend to discount those since we're close to the domain, but
> ask a linguist what they think about this sentence and they will
> point out ambiguities. Or use a tool like spaCy to visualize
> the parse trees (from which one can see that those ambiguities
> lead to a combinatorial explosion of valid parses).
>
> Fortunately, most of these have overlapping meanings, and there
> are only two potentially meaningful interpretations, one where
> the size requirement of the request attaches only to the size of
> the pointed-to type, or one where it attaches to both the size
> and fundamental alignment requirement of the pointed-to type.
>
>>I'll try to use indentation to indicate (imperfectly) how I parse it:
>>
>>The pointer returned if the allocation succeeds
>> is suitably aligned so that
>> it may be assigned to a pointer to any type of object
>> with
>> a fundamental alignment requirement
>> and
>> size less than or equal to the size requested.
>>
>>So "a fundamental alignment requirement" and "size less than or equal
>>to the size requested" are parallel requirements, both applying to
>>the type of the pointed-to object.
>
> That is one of the two reasonable interpretations. The other is
> that the requirements placed on size and fundamental alignment
> of the pointer type are cumulative and dependent on the size of
> the allocation. If so, then nothing in the C standard prevents
> an implementation from from returning a pointer aligned to
> `alignof(char)` for `malloc(1)`.
I do see the parsing ambiguity now.
I believe that's consistent with the way I parsed it. A successful
malloc(1) returns a value that can be assigned to a pointer of type
char*, which points to the object type "char", which does have (a
fundamental alignment requirement) and (a size less than or equal
to the size requested).
>>I do see a problem with the size requirement being *under* the
>>"suitably aligned" requirement. It wrongly implies that satisfying
>>the size requirement contributes to the "suitably aligned" requirement.
>>But that's not an ambiguous parse.
>>
>>Do you see a different way to parse the sentence?
>
> Adopting your notation, the other meaningful way is
>
> If the allocation succeeds:
> The pointer returned is suitably aligned so that:
> It may be asigned to a pointer to any type of object with:
> a fundamental alignment requirement _and_ size,
> less than or equal to the size requested.
>
> This is a grammatically valid parse of that sentence. Given the
> existence of `aligned_alloc`, I can't think of a great reason
> why this second interpretation should be prohibited; there are
> esoteric hardware structures that do things like align 32-bit
> registers on 128-bit boundaries (e.g., the MMIO registers for
> the x86 xAPIC), but pointers to those are not the domain of
> `malloc`.
If you allow an object type to have an alignment greater than its
size, then the ambiguity can result in an actual difference in the
requirements. But then arrays don't work.
> Btw, this kind of ambiguity is the primary objection I have to
> attempts at using informal English prose to precisely define a
> programming language.
I'm not sure that anything other than English (or other human
language) prose would be a practical way to define C. There have
been attempts to define formal semantics for C, but they haven't
caught on.
--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */
Technical Details
Message-ID:
<11ap0fo$1t8gq$2@kst.eternal-september.org>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!kst.eternal-september.org!.POSTED!not-for-mailReferences:
<86ece2kppv.fsf@linuxsc.com> <86v775h9o2.fsf@linuxsc.com> <11am74c$54g$1@reader2.panix.com> <11amvh2$14pou$3@kst.eternal-september.org> <11ao2mr$4l9$1@reader2.panix.com>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!kst.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson <Keith.S.Thompson+u@gmail.com> Newsgroups: comp.lang.c Subject: Re: AVL tree insertion - programming exercise Date: Wed, 14 Oct 2026 15:42:00 -0700 Organization: None to speak of Lines: 114 Message-ID: <11ap0fo$1t8gq$2@kst.eternal-september.org> References: <86ece2kppv.fsf@linuxsc.com> <86v775h9o2.fsf@linuxsc.com> <11am74c$54g$1@reader2.panix.com> <11amvh2$14pou$3@kst.eternal-september.org> <11ao2mr$4l9$1@reader2.panix.com> MIME-Version: 1.0 Content-Type: text/plain Injection-Date: Wed, 14 Oct 2026 22:42:01 +0000 (UTC) Injection-Info: kst.eternal-september.org; logging-data="2007578"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+irb4DuwESUam2N+u9X/QX"; posting-host="8bc08a5e85dad83e60fe86919d5c20fb" User-Agent: Gnus/5.13 (Gnus v5.13) Cancel-Lock: sha1:evDmSt53AbPjuSkuTFZqWXiIaWQ= sha256:KOGWFBheJtbZZxTjvoUSO+k+J3V7QOtQWVyD6FYDimA= sha1:H9Hxg3054JFsA6+EHhRmIlow+4w= sha256:rBxyWZUE0x4K8xBGzBX6cj9H9QMOdvkqvnqFq1uq3MA=