🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #437

Re: AVL tree insertion - programming exercise

#437
From: Keith Thompson
Date: Fri, 16 Oct 2026 01:14
109 lines
6297 bytes
Kaz Kylheku <046-301-5902@kylheku.com> writes:
> On 2026-10-15, Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
>> cross@spitfire.i.gajendra.net (Dan Cross) writes:
>>> In article <11ap0fo$1t8gq$2@kst.eternal-september.org>,
>>> Keith Thompson  <Keith.S.Thompson+u@gmail.com> wrote:
>>>>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:
>>>>>>[snip]
>>>>>>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).
>>>
>>> Right, but I think it's a bit deeper than that.
>>>
>>> Somewhat informally, the C17 wording could be read as requiring
>>> that the pointer returned from `malloc` must be suitable for
>>> assignment to any pointer to a type with `alignof(max_align_t)`
>>> regardless of the requested size, which might be smaller.  N2293
>>> calls this the, '"strong-alignment" reading.'
>>
>> The C17 wording is:
>>
>>     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 then used to access such an
>>     object or an array of such objects in the space allocated (until the
>>     space is explicitly deallocated).
>
> I suspect that this should be read as "any type of object which can
> fit into the requested size".
>
>> I don't see any other way to interpret that.
>
> It can be interpreted a a defect.

Sure.  My point is that I see no ambiguity in the C17 wording.  A
non-null pointer returned by malloc() must be "suitably aligned".

Yes, it's a defect.  What it says is clear.  What it says is also
logically inconsistent.

Given a typical memory model (not mandated by the standard), it could
mean that a successful malloc(1) must return a pointer whose low-order N
bits are zero, where N is typically about 3 or 4.  Of course the
standard couldn't express it that way.

> Note that in a single sentence, the requirement is being given that:
>
> - the pointer from a successful allocation is aligned for any object
>
> - the pointer can be used to access such an object ... in the space
>   allocated
>
> From that we can infer that "any type" cannot literally be any type
> Because if we have a successful malloc(1) and we choose "long long" to
> be any type, the literally interpreted requirements are that we can
> convert the pointer to "long long *" and use the resulting pointer for
> access. That requires a malloc(1) to be adjusted to malloc(sizeof (long
> long)); the requirement goes beyond alignment.
>
> It must be that "any type" means "any type that can be
> stored in the space allocated".

But that's not what it says.

> Then the whole thing makes sense; yes, the pointer can be converted
> to that type and used to access such an object in the space allocated,
> because it fits.

Yes, the whole thing makes sense if you change the wording.  That's
what C23 did (perhaps not entirely successfully); it corrected
the defect.

There is at least one possibly visible semantic change from the
C23 wording.  Assume alignof(long double) > 16.  In C17, this
is well defined (if malloc succeeds, and given the most nearly
reasonable interpretation of the defective C17 wording):

    long double *p = malloc(1);

though any attempt to dereference p has undefined behavior.  In C23,
it can have undefined behavior if the pointer returned by malloc
is not appropriately aligned.  (On most implementations, the UB
doesn't do anything bad; the misaligned pointer value is simply
stored in p.)

The behavior isn't *always* undefined.  The pointer returned by
malloc(1) could be appropriately aligned (always or just in this
one instance), or long double could have an alignment of 1 byte.

-- 
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: <11artp4$2tcf0$1@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-mail
References: <86ece2kppv.fsf@linuxsc.com> <11amvh2$14pou$3@kst.eternal-september.org> <11ao2mr$4l9$1@reader2.panix.com> <11ap0fo$1t8gq$2@kst.eternal-september.org> <11aqnp0$sb$1@reader2.panix.com> <11arlj9$2q39c$1@kst.eternal-september.org> <20261015163445.76@kylheku.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: Thu, 15 Oct 2026 18:14:07 -0700
Organization: None to speak of
Lines: 109
Message-ID: <11artp4$2tcf0$1@kst.eternal-september.org>
References: <86ece2kppv.fsf@linuxsc.com>
	<11amvh2$14pou$3@kst.eternal-september.org>
	<11ao2mr$4l9$1@reader2.panix.com>
	<11ap0fo$1t8gq$2@kst.eternal-september.org>
	<11aqnp0$sb$1@reader2.panix.com>
	<11arlj9$2q39c$1@kst.eternal-september.org>
	<20261015163445.76@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Date: Fri, 16 Oct 2026 01:14:13 +0000 (UTC)
Injection-Info: kst.eternal-september.org;
	logging-data="3060192"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX190xQ3oUSKVGlG/NsJvBZz3"; posting-host="cf7ed1e612447f7784dc01a34361c57c"
User-Agent: Gnus/5.13 (Gnus v5.13)
Cancel-Lock: sha1:XHx2YvviZzfAfYeGF2/41FmYYJo= sha256:jnvgKM8hcwnLsfXjshBVZ81SCbBAqG0zhhrrPTNUVN8=
	sha1:fgeD5Uk473y8vrIjA7OuhqVgagk= sha256:UXLljROXkxBaormqYHS3CloGUWTOTC4CPKF+JaKpsBo=