🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #476

Re: AVL tree insertion - programming exercise

#476
From: Keith Thompson
Date: Sat, 17 Oct 2026 17:48
196 lines
11260 bytes
Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
> Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:
>> 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 don't see any other way to interpret that.  It requires that
>> a pointer returned by a successful malloc() must *always* be
>> suitably aligned.  A pointer returned by malloc(1) must be aligned
>> so that it can point to a long long, for example.
>>
>> It overlooks the fact that it can't point to a long long, because
>> there isn't room for it.  [...]
>
> Let's see if we can get this discussion back on the rails.
>
> Put yourself back in 1999.  Forget about anything from 2000 onwards.
> There is just one relevant sentence in the C99 standard:
>
>    The pointer returned if the allocation succeeds is suitably
>    aligned so that it may be assigned to a pointer to any type of
>    object and then used to access such an object in the space
>    allocated (until the space is explicitly deallocated).
>
> The original C standard says the same thing except it says "... is
> explicitly freed or reallocated" rather than deallocated.

Yes.  That wording is flawed.  It implies that the pointer returned
by a successful malloc(1) may be "used to access" a long double
object in the space allocated.

If we set aside the "used to access" clause, it's still clear that the
returned pointer is suitably aligned for any object.  So malloc(1) must
return, for example, a 16-byte aligned pointer, even though no 16-byte
object can be stored in the allocated space.

> The intended meaning here is straightforward.  A pointer returned from
> malloc() may be assigned to any pointer no matter what type it points
> to.  Non-null pointers may be used to access a suitably sized array.
> If the size requested is smaller than the pointed-to type, using the
> pointer follows the rule for zero-length flexible array members:  it
> acts like an array of size 1 except that trying to access the element
> or advance to the next element is undefined behavior.  In other words
> it's okay to use the pointer as a pointer but it can't be incremented
> or dereferenced.

I agree that the intended meaning is straightforward, but the
words do not correctly express that intent.  To understand the
intent, you have to set aside the incorrect assertion that a pointer
returned by malloc() can be used to access an object bigger than
the requested allocation.

On the other hand, here's the C17 wording:

    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 suppose that could be read so it applies only if such an object *can*
exist in the space allocated.  But I still find it ambiguous at best.

> Now go forward to 2011.  We want the same behavior for malloc() as in
> C99, only now we have to consider types with overly large alignments.
> For obvious reasons we don't want to force malloc() to make worst-case
> assumptions about what the alignment needs will be.  A new function
> aligned_alloc() is added, with an extra parameter to specify what
> alignment is needed, to take care of types with overly large alignment
> needs.  The general rule for what pointer types are covered is changed
> to admit the same kinds as when there were no overly large alignments;
> that change is put in place by adding the clause "with a fundamental
> alignment requirement" to the general description of memory management
> functions.  The description of aligned_alloc() says specifically that
> it allocates an object having the given alignment value (and so the
> returned pointer value presumably may be used as such).  What happens
> with requests whose size is smaller than what the assigned-to pointer
> type implies is the same as before, following the FAM rule;  that rule
> applies to aligned_alloc() the same as is does for malloc().

"FAM"?

> Note by the way that aligned_alloc() may be used to ask for a space
> whose alignment is smaller than normal as well as any overly large
> alignment.  So malloc() should be used for generic alignments, and
> aligned_alloc() should be used any too-small or too-large alignment.

Suppose alignof (max_align_t) == 16.  aligned_malloc(1, 1024) must still
return a 16-byte aligned pointer.  This is stated explicitly in a
footnote in C23 (N3220).

> I don't have a copy of the C17 standard handy, but I expect it says
> the same thing about malloc() and friends as the C11 standard does.
>
> Regarding the discussion in N2293, I'm sorry to say that either the
> author is not too bright or he just didn't have his Wheaties that
> morning.  He reads the words but he doesn't understand the meaning.  

He reads the words and recognizes the flaw.

I personally don't think the "weak-alignment" reading described in
N2293 is valid, but if some implementers have actually interpreted
it that way, it's worth fixing the wording to clear it up.

> A fair guess is that he made two common mistakes:  one, he didn't
> consider the historical context, and two, he looked at passages in
> isolation without taking into account a broader context.  The Standard
> is not meant to be read like a legal document.  All the parts are
> expected to fit together, and in deciding what a particular passage
> means one needs to consider other surrounding and related passages and
> how they might interact.

If some wording in the standard requires historical context to
understand it, that wording is flawed.

>                           As for the arguments about how various
> implementations behave, the reasoning is backwards.  It's the tail
> wagging the dog.  We don't judge what the C standard means based on
> what implementations do;  rather we decide what the C standard is
> meant to say and then assess whether a proposed implementation is
> conforming.  The possibility of potential performance benefits does
> not excuse changing the behavior required under previous C standards.
> If someone thinks that's important they should use aligned_alloc();
> otherwise they should just use malloc() and know that it will behave
> the way it always has.
>
> Bottom line:  the discussion in N2293 is misguided.  A technical
> corrigendum should be issued, putting back the language used in C11
> and C17, and adding a footnote explaining that aligned_alloc() can be
> used for any allocation whose alignment is other than the default
> alignment provided by malloc().

I don't want to restore the flawed wording from C17 and earlier.

Are you saying that malloc(1) *should* be required to return a fully
aligned pointer?  Since C17 and earlier did require that, perhaps there
should have been more discussion about relaxing it by allowing malloc(1)
to return an "odd" pointer.  I don't think that was a bad change, but
it could in principle break some existing code.

I see two separate issues.

1. The wording in C17 and earlier can be read to require the ability to
   access a large object in a small allocated space.  That's a wording
   issue.

2. C17 and earlier require small allocations to be strongly aligned.
   C23 drops that requirement.

If there's a good reason to continue to require the result of
malloc(1) to be strongly aligned, I wouldn't object to it - but
C23 already dropped that requirement, so imposing it again in C2y
could be problematic.

-- 
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: <11b0ce3$f6tl$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> <86h5ikpd2n.fsf@linuxsc.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: Sat, 17 Oct 2026 10:48:47 -0700
Organization: None to speak of
Lines: 196
Message-ID: <11b0ce3$f6tl$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>
	<86h5ikpd2n.fsf@linuxsc.com>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Date: Sat, 17 Oct 2026 17:48:52 +0000 (UTC)
Injection-Info: kst.eternal-september.org;
	logging-data="498613"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1/Gf96LthDapgKA3OkO/VGm"; posting-host="1c9c611a4b938d37c51084ac6587c6b8"
User-Agent: Gnus/5.13 (Gnus v5.13)
Cancel-Lock: sha1:LDJrrJ2iiRO6rLkZLGLAOnjKi3c= sha256:oqhMvSITh7Bq9H3WG0VO+Tt7LmVv6wPBcwAYqJdv/zA=
	sha1:T1pKIY5ZmEpiARN8rmzWqp62KSQ= sha256:oVKJLopNRN5Fc7aIy2x/Se/WMmpOOkf2FVr49J1xbi4=