Article View: comp.lang.c
Article #328Re: AVL tree insertion - programming exercise
From: David Brown
Date: Tue, 13 Oct 2026 12:01
Date: Tue, 13 Oct 2026 12:01
59 lines
3784 bytes
3784 bytes
On 13/10/2026 13:29, Keith Thompson 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. 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.) > Would it be safe to assume that the 3 LSB's have a consistent value? (I am not suggesting it's a good idea.) You can check for "static_assert(alignof(long long int) >= 8);" to be sure you have 64-bit alignment at minimum. And that would probably give a compile-time error on any 64-bit system where the 3 LSB's are not always 0, as those probably don't support C23 ! > 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. Definitely a possibility. > > 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. > There are, I think, some systems where some bits of addresses are used for tags or other purposes - but that is usually higher bits (and obviously implementation dependent). Messing with them yourself could interfere with them.
Technical Details
Message-ID:
<11al6i1$gct5$1@dont-email.me>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-mailReferences:
<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!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail From: David Brown <david.brown@hesbynett.no> Newsgroups: comp.lang.c Subject: Re: AVL tree insertion - programming exercise Date: Tue, 13 Oct 2026 14:01:05 +0200 Organization: A noiseless patient Spider Lines: 59 Message-ID: <11al6i1$gct5$1@dont-email.me> References: <86ece2kppv.fsf@linuxsc.com> <11akqm4$c6h9$1@dont-email.me> <11akuf1$bq6c$1@dont-email.me> <11al4mu$fqtl$1@kst.eternal-september.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Tue, 13 Oct 2026 12:01:07 +0000 (UTC) Injection-Info: dont-email.me; logging-data="537509"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19l8UdmWF7dKKAxMgLJ/GXCoxJ9n04DYaw="; posting-host="0453d936fb977b9ac4728f0f8db2cabc" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:r6A7BJYCSvJVivMLmYig2MsmDIA= sha256:0p39+wPjLixGguUOkgZRGWr+f3JLtpQrRDNJyh7wkD0= sha1:uElkZHDrY0HHW5/rjVsSlzZW2D4= sha256:PQFFQ7MntiZws+bnoTwzBspgDEnTb9x/YkUW9VeCt+s= In-Reply-To: <11al4mu$fqtl$1@kst.eternal-september.org> Content-Language: en-GB, nb-NO