🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #336

Re: AVL tree insertion - programming exercise

#336
From: Dan Cross
Date: Tue, 13 Oct 2026 15:17
85 lines
4569 bytes
In article <11al4mu$fqtl$1@kst.eternal-september.org>,
Keith Thompson  <Keith.S.Thompson+u@gmail.com> wrote:
>David Brown <david.brown@hesbynett.no> writes:
>> On 13/10/2026 10:38, chine.bleu wrote:
>>> Tim Rentsch wrote:
>>>> sons[2] array.  These links are basically pointers, but held
>>>> as uintptr_t so they can take one of the bits to mean "tall",
>>>> which means taller than the other subtree of the node.  The
>>>> tree structure should maintain an invariant that at most one
>>> Memory is cheap. 
>>
>> Not any more!
>>
>>> Do not encode tags into low bits of an address unless you really
>>> know what your doing.
>>
>> Agreed, as a principle.
>>
>>> As a precaution check the return address of malloc
>>>      p = malloc(m);
>>>      if ((uintptr_t)p)&1!=0) abort();
>>
>> malloc always returns an address with suitable alignment for any
>> fundamental alignment on the target.

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, but I believe that it is the
former (that is, the "and" applies the size requirement to both
alignment and returned size).  Thus, a conforming implementation
of `malloc(1)` could return a pointer that is `alignof(char)`,
but not `alignof(int)`.

I argue that examining the text from C17 and how it has changed
further supports this interpretation.  From C17, sec 7.22.3:

|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).

Here, no mention of the requested size is made at all, and this
clearly requires the return value from a successful allocation
by `malloc(1)` to be aligned so that one could assign it to a
pointer referring to any object of any type with a "fundamental
alignment requirement."

>> Typically that means at least
>> 64-bit alignment (and thus the three LSB's are always zero), but it is
>> implementation dependent.
>
>Typically yes, but the standard says nearly nothing about how
>pointers are represented.  If your assume a 64-bit aligned pointer
>has zeros in the 3 LSBs, it's at least potentionally non-portable.
>(The systems I've used that broke that assumption are most likely
>no longer in use.)

This.  It's unlikely that any real world system will do anything
differently these 

>Another possible issue appear if you're using garbage collection.
>If you alter the low bits of a pointer, you might clobber the only
>live reference to the object it points to, and the garbage collector
>might deallocate it behind your back.
>
>Perhaps more realistically, by *not* playing games with pointer
>representations, you'll have one less thing to worry about when 
>tracking down why your code is mysteriously misbehaving.

Playing games with pointer values can be useful for implementing
things like, e.g, Harris's lock-free linked list.

	- Dan C.
Technical Details
Message-ID: <11ali2e$pi$1@reader2.panix.com>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!panix!.POSTED.spitfire.i.gajendra.net!not-for-mail
References: <86ece2kppv.fsf@linuxsc.com> <11akqm4$c6h9$1@dont-email.me> <11akuf1$bq6c$1@dont-email.me> <11al4mu$fqtl$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!panix!.POSTED.spitfire.i.gajendra.net!not-for-mail
From: cross@spitfire.i.gajendra.net (Dan Cross)
Newsgroups: comp.lang.c
Subject: Re: AVL tree insertion - programming exercise
Date: Tue, 13 Oct 2026 15:17:34 -0000 (UTC)
Organization: PANIX Public Access Internet and UNIX, NYC
Message-ID: <11ali2e$pi$1@reader2.panix.com>
References: <86ece2kppv.fsf@linuxsc.com> <11akqm4$c6h9$1@dont-email.me> <11akuf1$bq6c$1@dont-email.me> <11al4mu$fqtl$1@kst.eternal-september.org>
Injection-Date: Tue, 13 Oct 2026 15:17:34 -0000 (UTC)
Injection-Info: reader2.panix.com; posting-host="spitfire.i.gajendra.net:166.84.136.80";
	logging-data="818"; mail-complaints-to="abuse@panix.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: cross@spitfire.i.gajendra.net (Dan Cross)