🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #460

Re: AVL tree insertion - programming exercise

#460
From: Keith Thompson
Date: Fri, 16 Oct 2026 22:27
97 lines
6222 bytes
Kaz Kylheku <046-301-5902@kylheku.com> writes:
> 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.

I don't see that that's more serious than the defect that's already
been acknowledged.

Clearly a pointer returned by malloc(1) can't be used to point either
to a long long int or to a struct HUGE.  The C17 wording specifies
only the alignment of the returned pointer.  It's poorly worded,
but I don't think anyone has interpreted it to mean that malloc(1)
must allocate more than one byte.  If alignof (max_align_t) is 16,
for example, malloc(1) could return a 16-byte-aligned pointer to
one byte of allocated space.  (The following 15 bytes might be
used for metadata.)  C23 relaxes the alignment requirement for
small allocations.

C17 guarantees that
    long double *p = malloc(1);
has defined behavior.  C23 does not.

IMHO the intent of the C17 wording is clear, even though it's fairly
obviously wrong.

-- 
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: <11au8bp$3mdne$3@kst.eternal-september.org>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!peer02.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!feeder1.feed.ams11.usenet.farm!feed.usenet.farm!2.eu.feeder.erje.net!feeder.erje.net!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> <11artp4$2tcf0$1@kst.eternal-september.org> <20261016130835.671@kylheku.com>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!peer02.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!feeder1.feed.ams11.usenet.farm!feed.usenet.farm!2.eu.feeder.erje.net!feeder.erje.net!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: Fri, 16 Oct 2026 15:27:05 -0700
Organization: None to speak of
Lines: 97
Message-ID: <11au8bp$3mdne$3@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>
	<11artp4$2tcf0$1@kst.eternal-september.org>
	<20261016130835.671@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Date: Fri, 16 Oct 2026 22:27:06 +0000 (UTC)
Injection-Info: kst.eternal-september.org;
	logging-data="3880686"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX19xwFYQ4gSB+0k3TgVWf4f7"; posting-host="cf7ed1e612447f7784dc01a34361c57c"
User-Agent: Gnus/5.13 (Gnus v5.13)
Cancel-Lock: sha1:ogd/te/nqloeK9+oS+aTnirf8es= sha256:6tRiu4Z+7inLeWt8EG5Hn4Sj3zcqlLLF4cRZ+pr/21c=
	sha1:KeXOPeQUN1A3yPgORaZpKp2xomk= sha256:EXccSf1M9jLsHA3HryCB8iHht0zT+ZEWWG91SZE0U50=
X-Received-Bytes: 6024