🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #453

Re: AVL tree insertion - programming exercise

#453
From: Kaz Kylheku
Date: Fri, 16 Oct 2026 20:26
77 lines
4956 bytes
On 2026-10-16, Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
> 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.

One problem is that "any type" if taken literally means all derived
types, not just basic types.

An example of "any type" is, say, struct tm. A program can declare
much larger, more complicated struct types.

The requirements, if interpreted literally, are saying that a successful
pointer out of malloc(1) must be suitably aligned to point to a struct
tm /and/ may be then used to access such an object (i.e a struct tm)
in the space allocated, and the same for any conceivable struct type
that fits into the implementation limits for conformance.

It is not reasonable for malloc requests for a small size to be padded
to accomodate that.

-- 
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca
Technical Details
Message-ID: <20261016130835.671@kylheku.com>
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!.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> <11artp4$2tcf0$1@kst.eternal-september.org>

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!.POSTED!not-for-mail
From: Kaz Kylheku <046-301-5902@kylheku.com>
Newsgroups: comp.lang.c
Subject: Re: AVL tree insertion - programming exercise
Date: Fri, 16 Oct 2026 20:26:12 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 77
Message-ID: <20261016130835.671@kylheku.com>
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>
 <11artp4$2tcf0$1@kst.eternal-september.org>
Injection-Date: Fri, 16 Oct 2026 20:26:13 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="3810125"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX18kRABLAG/UvCB/XsIZmt6ucmk+omVNf7o="; posting-host="8733afa1a8603d2f258354477f1b7f01"
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:PmhFNnAuf1RlQSBNqRxbirWW+00= sha256:IXFXAkrrLOV1kiIyVH3kxyY7dKmMGq85XTJtIbb0b4I=
	sha1:H04Am8Y2r5aLM3drvkTYSNW9juo= sha256:I1DDryeGLM+40gXqLJUQdX/3HArhj+eXdJQNqqWHVOQ=