🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #325

Re: AVL tree insertion - programming exercise

#325
From: Keith Thompson
Date: Tue, 13 Oct 2026 11:29
43 lines
3052 bytes
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.  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.)

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.

-- 
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: <11al4mu$fqtl$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> <11akqm4$c6h9$1@dont-email.me> <11akuf1$bq6c$1@dont-email.me>

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: Tue, 13 Oct 2026 04:29:34 -0700
Organization: None to speak of
Lines: 43
Message-ID: <11al4mu$fqtl$1@kst.eternal-september.org>
References: <86ece2kppv.fsf@linuxsc.com> <11akqm4$c6h9$1@dont-email.me>
	<11akuf1$bq6c$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Date: Tue, 13 Oct 2026 11:29:35 +0000 (UTC)
Injection-Info: kst.eternal-september.org;
	logging-data="519093"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX18eLsWAoSyA7OjIZcOiA86y"; posting-host="11ac7c4b547d7ca6b42084bf5cca4a8e"
User-Agent: Gnus/5.13 (Gnus v5.13)
Cancel-Lock: sha1:9Fi18lX9t69NndqGRH9X5lJm6Qo= sha256:01eW2aMrWJzHY0F3Xq62OloU0oIsKu5owVKkZ98oOu8=
	sha1:cgU7U9/xJOid8CH06duG8WQw7O8= sha256:01DVV7ySjh4H6rXNfA3rBcu9YBp7OCUaEnThyR35gV0=