Article View: comp.lang.c
Article #483Re: AVL tree insertion - programming exercise
From: Chris M. Thomasson
Date: Sun, 18 Oct 2026 03:28
Date: Sun, 18 Oct 2026 03:28
91 lines
5613 bytes
5613 bytes
On 10/15/2026 2:07 PM, Chris M. Thomasson wrote:
> On 10/14/2026 10:47 PM, Keith Thompson wrote:
>> 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 <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.
>>>
>>> Surely we would like to protect against a null pointer being used
>>> to access a non-existent object.
>>>
>>>> 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.
>>>
>>> As long as the bit in question /is/ zero it doesn't matter if it
>>> might have been one. The bit will always be cleared before using
>>> the pointer as a pointer, so the pointer value in use will match
>>> the pointer value originally returned.
>>>
>>> Thus we might write a safe allocation function as follows:
>>>
>>> void *
>>> safe_new( size_t n ){
>>> void *p = 0;
>>>
>>> while( !p || (uintptr_t)p %2 != 0 ) free( p ), p =
>>> malloc( n++ );
>>>
>>> return p;
>>> }
>>>
>>> Of course it might not succeed in some cases, but it will be
>>> quite obvious if it doesn't. :)
>>
>> I might be missing the point of "chine.bleu"'s suggestion. I was
>> assuming that the point was to check whether it's possible *in
>> general* to play with the low-order bits of malloc'ed pointers.
>>
>> On most systems, you can probably make that assumption based on
>> what you know about the system's memory model. But it can't be
>> checked reliably at run time.
>>
>> If I wanted my program to play with low-order bits of pointers, and
>> refuse to run if the system doesn't support that, I might write a
>> program that does a *lot* of malloc() calls and invoke it during
>> configuration, refusing to proceed with the build if it reports that
>> malloc() can return an "odd" pointer. Aborting during execution
>> feels sloppy.
>>
>
> Playing the the low order bits is common in some exotic lock-free
> algorihtms. BUT, its much better to over allocate using say malloc, then
> manually align the pointer it returns. Then we can say that pointer is
> aligned on say a 8192 boundary. We know it size if 8192. But we had to
> ask malloc for more memory to make sure we can align and have the right
> size data block. THEN! We can play with the aligned address and store
> whatever data we want. Just make sure to strip it out before we use the
> aligned and packed pointer.
If somebody uses packed pointers, be sure to have a strip function, say
get back to the raw pointer. Or It might start playing the following music:
Lisa Lisa & Cult Jam - Head To Toe
https://youtu.be/51Iq8JmmfxY?list=RDMM51Iq8JmmfxY
Technical Details
Message-ID:
<11b1edl$q92a$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> <11al0l0$eefv$1@dont-email.me> <11alfgs$t7q$1@reader2.panix.com> <11amttk$14pou$2@kst.eternal-september.org> <86se27r52l.fsf@linuxsc.com> <11appe9$24dl1$1@kst.eternal-september.org> <11arfb8$2ov0f$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!.POSTED!not-for-mail From: "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> Newsgroups: comp.lang.c Subject: Re: AVL tree insertion - programming exercise Date: Sat, 17 Oct 2026 20:28:52 -0700 Organization: A noiseless patient Spider Lines: 91 Message-ID: <11b1edl$q92a$1@dont-email.me> References: <86ece2kppv.fsf@linuxsc.com> <11akqm4$c6h9$1@dont-email.me> <11al0l0$eefv$1@dont-email.me> <11alfgs$t7q$1@reader2.panix.com> <11amttk$14pou$2@kst.eternal-september.org> <86se27r52l.fsf@linuxsc.com> <11appe9$24dl1$1@kst.eternal-september.org> <11arfb8$2ov0f$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Sun, 18 Oct 2026 03:28:54 +0000 (UTC) Injection-Info: dont-email.me; logging-data="861258"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/lJ5ecK2/6zhkoXbi6y2xCmOeWR7LTz5I="; posting-host="f3e663e33009b75e0973c148e80f63cf" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:SAD7C6L/a8bA2NnHf0XlPvJUngs= sha256:paxwQrqtjosj9t3p4NxAPMoX+YkLWuTByjtAzI8GeCs= sha1:By2lbiqafP8Zn1+UWwlx57DX7i8= sha256:98lsZbvGDAfCynw94ZWklD8DhJ2j/pBy88lBBMXLOjM= Content-Language: en-US In-Reply-To: <11arfb8$2ov0f$1@dont-email.me>