🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #372

Re: AVL tree insertion - programming exercise

#372
From: Keith Thompson
Date: Wed, 14 Oct 2026 03:45
35 lines
2742 bytes
cross@spitfire.i.gajendra.net (Dan Cross) writes:
> In article <11al0l0$eefv$1@dont-email.me>,
> James Kuyper  <jameskuyper@alumni.caltech.edu> wrote:
>>On 13/10/2026 10:38, chine.bleu wrote:
>>...> As a precaution check the return
>>> address of malloc
>>> 
>>>  p = malloc(m);
>>>  if ((uintptr_t)p)&1!=0) abort();
>>
>>Just to be pedantic, the standard doesn't guarantee that the expression
>>you use will test what you want to test. Since there is a guarantee that
>>malloc() returns a universally aligned memory, I think it's actually
>>more likely that the test will fail than that malloc() will fail, though
>>both probabilities are quite negligible, so it's hard to be sure.
>
> The syntax and precedence errors aside, the test is clearly
> meant to ensure that the low bit in the return from `malloc` is
> clear, so that it can be repurposed for other things.  It should
> likely be:
>
> 	p = malloc(m);
> 	if (p == NULL || ((uintptr_t)p & 1) != 0) abort();

I'm not sure the NULL test adds anything useful.  The low-order bits of
a null pointer are very likely to be 0 on any real-world system.

Also, the test only looks at the low-order bits of the result of
the current malloc() call.  If you have an exotic system where
an aligned pointer can have a low-order bit set to 1, it might
*sometimes* be set to 0.

-- 
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: <11amttk$14pou$2@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> <11al0l0$eefv$1@dont-email.me> <11alfgs$t7q$1@reader2.panix.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: Tue, 13 Oct 2026 20:45:56 -0700
Organization: None to speak of
Lines: 35
Message-ID: <11amttk$14pou$2@kst.eternal-september.org>
References: <86ece2kppv.fsf@linuxsc.com> <11akqm4$c6h9$1@dont-email.me>
	<11al0l0$eefv$1@dont-email.me> <11alfgs$t7q$1@reader2.panix.com>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Date: Wed, 14 Oct 2026 03:45:57 +0000 (UTC)
Injection-Info: kst.eternal-september.org;
	logging-data="1206046"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX19fVv5QzmlGM+j04u2eZhiq"; posting-host="8bc08a5e85dad83e60fe86919d5c20fb"
User-Agent: Gnus/5.13 (Gnus v5.13)
Cancel-Lock: sha1:ppKUxR+VCs7v2aj8tHq3xfO3xo4= sha256:D/niphYykZ8e5hstvH6QKjv7aO8YqujNalNgej7bWt8=
	sha1:ALarfgu3MiSgFoaqWZ9dtAMox9k= sha256:HC6bd1COdumWkfG0Isq53R+DNCJyvpXv1TP2XSOrILQ=