Thread View: comp.lang.c
114 messages
114 total messages
Page 2 of 3
Started by Tim Rentsch
Wed, 07 Oct 2026 06:08
Page 2 of 3 • 114 total messages
Re: AVL tree insertion - programming exercise
Author: Dan Cross
Date: Mon, 12 Oct 2026 18:26
Date: Mon, 12 Oct 2026 18:26
157 lines
9002 bytes
9002 bytes
In article <11adef7$1ol9b$1@dont-email.me>, David Brown <david.brown@hesbynett.no> wrote: >On 09/10/2026 21:48, Dan Cross wrote: >> In article <11aasbg$r60s$1@dont-email.me>, >> David Brown <david.brown@hesbynett.no> wrote: >>> [snip] >>> It would undoubtedly have been less astonishing in 1968 (when the first >>> book was published) than in 1991 (when I first saw it), but I would have >>> thought that even people familiar with 1960's mainframes would see it as >>> strange to use a limited and highly specific artificial assembly that >>> matched poorly with existing systems. >> >> That's the thing: I don't know that it did match poorly against >> many of those systems. Word oriented machines, with word sizes >> that were not powers-of-two, abstruse IO instructions; all of >> that was fairly common back then. True, the IBM 360 had bucked >> that trend and would set the precedent for what came later, but >> that hand't happened yet. > >You misunderstand me, I think. Yes, cpus had widely varying >architectures, word sizes, data representations, etc., and could have >instructions dedicated to particular hardware devices - and if all else >failed in your code, a "halt and catch fire" instruction. It's not so >much that MIX matched poorly to existing systems - they all matched >poorly to each other. Implementing an algorithm on different >processors' assemblies meant figuring out how to make it work with that >particular word size, or how to make efficient use of the particular >indexing registers and addressing modes that the cpu used. Translating >from one assembly to another on a radically different architecture means >two steps - you first have to figure out what the original code is >really trying to do, and abstract away from the messy implementation >details. Then you have to fit it into the new architecture, and add the >new messy implementation details. > >So by using MIX, rather than a higher level, more abstract pseudo-code, >anyone trying to use the code in the book on a real cpu has to do twice >the work. I mean, maybe? I believe he thought that MIX was close enough to the other common processors of the day that someone familiar with them would be able to translate relatively easily; whether that was true or not, I can't say. I don't know what he was thinking other than what he's said about it publicly. >> Moreover, assembly language programming for applications was >> still common. Using assembler would have had a lot of reach, >> even if it's a fairly opaque way to present algorithms. > >Sure, assembly was very commonly used for applications - and for a long >time afterwards. It's a major reason why the computing world is >dominated by the polished turd that is the modern x86 processor - >translating assembly programs into anything else is difficult, expensive >and risky, so it is rarely done. Yup. Funnily, he mentions this in Volume 1; in section 1.4.3.1, he builds a MIX simulator (in MIX, naturally). The prefatory material includes, |The motivation for simulators is simple: A computer |installation buys a new machine and still wants to run programs |written for the old machine (rather than rewriting the |programs). However, this usually costs more and gives poorer |results than if a special task force of programmers were given |temporary employment to do the reprogramming. For example, the |author once participated in such a reprogramming project, and a |serious error was discovered in the original program, which had |been in use for several years; the new program worked at five |times the speed of the old, besides giving the right answers |for a change! (Not all simulators are bad; for example, it is |usually advantageous for a computer manufacturer to simulate a |new machine before it has been built, so that software for the |new machine may be developed as soon as possible. But that is |a very specialized application.) An extreme example of the |inefficient use of computer simulators is the true story of |machine _A_ simulating machine _B_ running a program that |simulates machine _C_! This is the way to make a large, |expensive computer give poorer results than its cheaper cousin. YMMV, but this seems to make clear his position on the matter. :-D (Of course, the counter argument was that a high-level language could be presented to a compiler targetting a new CPU, or a high level pseudocode could be translated to a concrete implementation by hand.) >>> I am sure people were already >>> familiar with the pain of converting code written in assembly for one >>> system into assembly for a very different system - the only advantage of >>> the MIX code was that it was equally useless to everyone, and favoured >>> no one! >> >> Ha; great description. But had he chosen Burroughs, or IBM 360, >> or PDP-6, or the GE-635, or whatever else, it would have been >> seen as favoring a particular vendor (and therefore irrelevant >> to those using other machines) or too specific to be considered >> general. > >I can fully understand neutrality here. He may even have wanted to >avoid favouring one existing high-level language for another. But the >guy was pretty smart - he could easily have invented his own >hypothetical high-level language to use instead of such a low-level one. Probably! He clearly thought that assembly language was an important use case, but if I were speculating about why he did not use a HLL or pseudocode beyond what he's already said would just be guessing. *shrug* >>> [snip] >>> My bookshelves are too scattered for me to easily see what I have. But >>> I am not convinced that books are good references at all these days, for >>> most purposes. They are, however, still good for study if you are doing >>> courses or really learning a subject. >> >> Oh? Nowdays we have far more available resources, so I can see >> how one wouldn't necessarily reach for a book the way one would >> have in days past, but what do you see as the alternative? > >I don't know that there is one right answer - it depends on the depth >you want to go into, how specialist the topic is, and what you are >trying to get out of it all in the end. And it depends on how you >personally want to learn. For deeper study and for more specialist >topics, books are still the top choice for me. And I much prefer to >read books than websites over breakfast. > >But if I want a reference or refresher on AVL trees (to pick a random >topic :-) ), my first port of call is Wikipedia. If that is not enough, >I might be looking for videos - there's plenty around that can give nice >animations to help you understand what is going on, from five minute >quick introductions to hour-long lectures. I'd look for sample code in >a high level language - typically Python, but it might be interesting to >look at implementations in Haskell or other significantly different >languages. There are webpages full of explanations of them, and >comparisons to other tree structures. And there are online courses for >those looking for more academic treatment of data structures and algorithms. > >The internet is full of resources like this. Of course the quality is >varied - you definitely want to look at more than one site to get a >balanced view, and to minimise the risk of missing something important. >But for technical and non-controversial subjects like this, you are >going to be fairly safe with Wikipedia and a google search. Yeah, that's all fair. >[snip] >I wonder how CSP is taught now that people wave bank cards or mobile >phones at vending machines instead of inserting coins... > >(There's a fun family of microcontrollers with CSP at their heart from >XMOS.) Since you mentioned CSP _and_ videos, this talk from Rob Pike is pretty good, if indirect: https://www.youtube.com/watch?v=oV9rvDllKEg - Dan C.
Re: AVL tree insertion - programming exercise
Author: David Brown
Date: Mon, 12 Oct 2026 19:17
Date: Mon, 12 Oct 2026 19:17
23 lines
2193 bytes
2193 bytes
On 12/10/2026 20:26, Dan Cross wrote: > In article <11adef7$1ol9b$1@dont-email.me>, > David Brown <david.brown@hesbynett.no> wrote: (Thanks for the comments - I don't think there is anything to add, especially in the context of comp.lang.c) >> [snip] >> I wonder how CSP is taught now that people wave bank cards or mobile >> phones at vending machines instead of inserting coins... >> >> (There's a fun family of microcontrollers with CSP at their heart from >> XMOS.) > > Since you mentioned CSP _and_ videos, this talk from Rob Pike > is pretty good, if indirect: > https://www.youtube.com/watch?v=oV9rvDllKEg > I will add that to my "things to watch" list, though like my "things to read" and "programming languages to learn" lists, it grows faster than I can handle!
Re: AVL tree insertion - programming exercise
Author: chine.bleu
Date: Tue, 13 Oct 2026 08:38
Date: Tue, 13 Oct 2026 08:38
18 lines
2016 bytes
2016 bytes
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. Do not encode tags into low bits of an address unless you really know what your doing. As a precaution check the return address of malloc p = malloc(m); if ((uintptr_t)p)&1!=0) abort(); -- Siri Seal of Disavowal #777-000. Disavowed. Denied. @ NO KINGS For I desire mercy not sacrifice. /|\ The Church of the Holey Apple .signature 5.5 / \ of Discordian Mysteries. This post insults Islam. Mohamed
Re: AVL tree insertion - programming exercise
Author: David Brown
Date: Tue, 13 Oct 2026 09:42
Date: Tue, 13 Oct 2026 09:42
28 lines
2156 bytes
2156 bytes
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.
Re: AVL tree insertion - programming exercise
Author: James Kuyper
Date: Tue, 13 Oct 2026 10:20
Date: Tue, 13 Oct 2026 10:20
11 lines
1805 bytes
1805 bytes
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.
Re: AVL tree insertion - programming exercise
Author: Tim Rentsch
Date: Tue, 13 Oct 2026 10:52
Date: Tue, 13 Oct 2026 10:52
28 lines
2224 bytes
2224 bytes
"chine.bleu" <chine.bleu@yahoo.com> writes:
> 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. Do not encode tags into low bits of an address unless
> you really know what your doing. As a precaution check the return
> address of malloc
>
> p = malloc(m);
> if ((uintptr_t)p)&1!=0) abort();
Yes, in general the interface described will fall down on some
systems. But it's just an exercise. Feel free to use code along
the lines of the following (disclaimer: not tested) to confirm
the suitability of the pointer values being used:
Node *
new_node( int key ){
Node *r = malloc( sizeof *r );
assert( (uintptr_t)r %2 ==0 );
*r = (Node){ .key = key };
return r;
}
Re: AVL tree insertion - programming exercise
Author: Dan Cross
Date: Tue, 13 Oct 2026 10:55
Date: Tue, 13 Oct 2026 10:55
13 lines
1211 bytes
1211 bytes
In article <11akuf1$bq6c$1@dont-email.me>, David Brown <david.brown@hesbynett.no> wrote: >On 13/10/2026 10:38, chine.bleu wrote: >> [snip] >> Memory is cheap. > >Not any more! "Hey Dan, what would _you_ do with a million dollars?" "Two sticks of DDR5 at the same time...." - Dan C.
Re: AVL tree insertion - programming exercise
Author: Keith Thompson
Date: Tue, 13 Oct 2026 11:29
Date: Tue, 13 Oct 2026 11:29
43 lines
3052 bytes
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 */
Re: AVL tree insertion - programming exercise
Author: 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.
Re: AVL tree insertion - programming exercise
Author: Keith Thompson
Date: Tue, 13 Oct 2026 12:09
Date: Tue, 13 Oct 2026 12:09
35 lines
2829 bytes
2829 bytes
David Brown <david.brown@hesbynett.no> writes:
> On 13/10/2026 13:29, Keith Thompson wrote:
[...]
>> 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.)
No.
I didn't go into detail because I've described it here a number of
times, but it's been a while.
The systems in question were Cray vector systems, T90 and others. A
machine address pointed to a 64-bit word, but CHAR_BIT==8 because it
had a Unix-like OS (Unicos). A pointer to a 64-bit word could have
any arbitrary values in its 3 LSBs. Byte pointers were synthesized
by the compiler, with the 3-bit byte offset within a word stored
in the otherwise unused *upper* 3 bits of the 64-bit pointer.
C string operations were remarkably slow -- but of course these
systems were heavily optimized for floating-point.
I actually fixed some C code that computed the difference between
two pointers by converting them and doing an integer subtraction.
A direct pointer subtraction was simpler (at least in source code)
and had the virtue of being correct.
--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */
Re: AVL tree insertion - programming exercise
Author: Dan Cross
Date: Tue, 13 Oct 2026 14:34
Date: Tue, 13 Oct 2026 14:34
25 lines
1851 bytes
1851 bytes
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(); - Dan C.
Re: AVL tree insertion - programming exercise
Author: Dan Cross
Date: Tue, 13 Oct 2026 15:17
Date: Tue, 13 Oct 2026 15:17
85 lines
4569 bytes
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.
Re: AVL tree insertion - programming exercise
Author: chine.bleu
Date: Tue, 13 Oct 2026 15:19
Date: Tue, 13 Oct 2026 15:19
37 lines
2508 bytes
2508 bytes
David Brown wrote: > 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! I started on 65K. > >> 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. > -- Siri Seal of Disavowal #777-000. Disavowed. Denied. @ NO KINGS For I desire mercy not sacrifice. /|\ The Church of the Holey Apple .signature 5.5 / \ of Discordian Mysteries. This post insults Islam. Mohamed
Re: AVL tree insertion - programming exercise
Author: David Brown
Date: Tue, 13 Oct 2026 15:39
Date: Tue, 13 Oct 2026 15:39
51 lines
3551 bytes
3551 bytes
On 13/10/2026 17:17, Dan Cross wrote: > 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)`. > Your interpretation seems sound to me. (I looked at the C11 standards when I made my earlier post - it had not occurred to me that C23 might have changed the wording here. The change is not mentioned in the change history, as far as I can see.)
Re: AVL tree insertion - programming exercise
Author: Tim Rentsch
Date: Tue, 13 Oct 2026 16:01
Date: Tue, 13 Oct 2026 16:01
48 lines
3070 bytes
3070 bytes
cross@spitfire.i.gajendra.net (Dan Cross) writes: > 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, [...] It's poorly constructed but in English it has only one grammatical parse.
Re: AVL tree insertion - programming exercise
Author: Scott Lurndal
Date: Tue, 13 Oct 2026 17:15
Date: Tue, 13 Oct 2026 17:15
24 lines
1999 bytes
1999 bytes
cross@spitfire.i.gajendra.net (Dan Cross) writes: >In article <11al4mu$fqtl$1@kst.eternal-september.org>, >Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote: > >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." Note that the processor ABI documents for x86_64 and arm64 also require the return value always be aligned congruent to zero module 16.
Re: AVL tree insertion - programming exercise
Author: Waldek Hebisch
Date: Tue, 13 Oct 2026 19:01
Date: Tue, 13 Oct 2026 19:01
16 lines
1728 bytes
1728 bytes
David Brown <david.brown@hesbynett.no> wrote:
>
> 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.
AFAICS in software systems most common case is use of low bits. Simply
32-bit address space is to small to use upper bits for tags and until
recently there was tendency to support 32-bit machines using the same code.
Of course, once low bits are used for one purpose, you can not use
the same bits at the same time for different putpose.
--
Waldek Hebisch
Re: AVL tree insertion - programming exercise
Author: Chris M. Thomasson
Date: Tue, 13 Oct 2026 20:16
Date: Tue, 13 Oct 2026 20:16
238 lines
6999 bytes
6999 bytes
On 10/13/2026 2:42 AM, David Brown wrote:
> 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.
>
Right, and sometimes that can be wasteful on certain memory constrained
environments. Fwiw, I wrote a region allocator a while back that tends
to pack thing on their alignment, but never needs a max a alignment for
all.
Oh shit! I cannot get into google groups, I get a god damn: 429 Too Many
Requests
Well, here is a mutated version of it that uses C++. I had the C only
version in some of my saved google groups links. Sigh. Anyway:
My code:
____________________________________________
#if ! defined (RALLOC_H)
# define RALLOC_H
# if defined (__cplusplus)
extern "C" {
# endif
/**************************************************************/
#include <cstddef>
#include <cassert>
#if defined (_MSC_VER)
/* warning C4116: unnamed type definition in parentheses */
# pragma warning (disable : 4116)
#endif
#if ! defined (NDEBUG)
# include <cstdio>
# define RALLOC_DBG_PRINTF(mp_exp) std::printf mp_exp
#else
# define RALLOC_DBG_PRINTF(mp_exp) ((void)0)
#endif
#if ! defined (RALLOC_UINTPTR_TYPE)
# define RALLOC_UINTPTR_TYPE size_t
#endif
typedef RALLOC_UINTPTR_TYPE ralloc_uintptr_type;
typedef char ralloc_static_assert[
sizeof(ralloc_uintptr_type) == sizeof(void*) ? 1 : -1
];
#define RALLOC_ALIGN_OF(mp_type) alignof(mp_type)
#define RALLOC_ALIGN_MAX alignof(std::max_align_t)
#define RALLOC_ALIGN_UP(mp_ptr, mp_align) \
((void*)( \
(((ralloc_uintptr_type)(mp_ptr)) + ((mp_align) - 1)) \
& ~(((mp_align) - 1)) \
))
#define RALLOC_ALIGN_ASSERT(mp_ptr, mp_align) \
(((void*)(mp_ptr)) == RALLOC_ALIGN_UP(mp_ptr, mp_align))
struct region {
unsigned char* buffer;
size_t size;
size_t offset;
};
static void
rinit(
struct region* const self,
void* buffer,
size_t size
) {
self->buffer = (unsigned char*)buffer;
self->size = size;
self->offset = 0;
RALLOC_DBG_PRINTF((
"rinit(%p) {\n"
" buffer = %p\n"
" size = %lu\n"
"}\n\n\n",
(void*)self,
buffer,
(unsigned long int)size
));
}
static void*
rallocex(
struct region* const self,
size_t size,
size_t align
) {
unsigned char* align_buffer;
size_t offset = self->offset;
unsigned char* raw_buffer = self->buffer + offset;
if (! size) {
size = 1;
}
if (! align) {
align = RALLOC_ALIGN_MAX;
}
assert(align == 1 || RALLOC_ALIGN_ASSERT(align, 2));
align_buffer = (unsigned char*)RALLOC_ALIGN_UP(raw_buffer, align);
assert(RALLOC_ALIGN_ASSERT(align_buffer, align));
size += align_buffer - raw_buffer;
if (offset + size > self->size) {
return NULL;
}
self->offset = offset + size;
RALLOC_DBG_PRINTF((
"rallocex(%p) {\n"
" size = %llu\n"
" alignment = %llu\n"
" origin offset = %llu\n"
" final offset = %llu\n"
" raw_buffer = %p\n"
" align_buffer = %p\n"
" size adjustment = %llu\n"
" final size = %llu\n"
"}\n\n\n",
(void*)self,
(unsigned long long int)size - (align_buffer - raw_buffer),
(unsigned long long int)align,
(unsigned long long int)offset,
(unsigned long long int)self->offset,
(void*)raw_buffer,
(void*)align_buffer,
(unsigned long long int)(align_buffer - raw_buffer),
(unsigned long long int)size
));
return align_buffer;
}
#define ralloc(mp_self, mp_size) \
rallocex((mp_self), (mp_size), RALLOC_ALIGN_MAX)
#define ralloct(mp_self, mp_count, mp_type) \
rallocex( \
(mp_self), \
sizeof(mp_type) * (mp_count),\
RALLOC_ALIGN_OF(mp_type) \
)
static void
rflush(
struct region* const self
) {
self->offset = 0;
RALLOC_DBG_PRINTF((
"rflush(%p) {}\n\n\n",
(void*)self
));
}
#undef RALLOC_DBG_PRINTF
#undef RALLOC_UINTPTR_TYPE
/**************************************************************/
# if defined (__cplusplus)
}
# endif
#endif
Re: AVL tree insertion - programming exercise
Author: Michael S
Date: Tue, 13 Oct 2026 20:28
Date: Tue, 13 Oct 2026 20:28
17 lines
1752 bytes
1752 bytes
On Tue, 13 Oct 2026 13:16:44 -0700 "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> wrote: > > Oh shit! I cannot get into google groups, I get a god damn: 429 Too > Many Requests You can get into google groups but you would have to do it manually, step by step starting from top https://groups.google.com Shortcuts, even the simplest ones like https://groups.google.com/g/comp.lang.c nowadays work very rarely. So it goes.
Re: AVL tree insertion - programming exercise
Author: Dan Cross
Date: Tue, 13 Oct 2026 21:17
Date: Tue, 13 Oct 2026 21:17
55 lines
2871 bytes
2871 bytes
In article <86v775h9o2.fsf@linuxsc.com>, Tim Rentsch <tr.17687@z991.linuxsc.com> wrote: >cross@spitfire.i.gajendra.net (Dan Cross) writes: > >> 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, [...] > >It's poorly constructed but in English it has only one >grammatical parse. Nope. Not even close. - Dan C.
Re: AVL tree insertion - programming exercise
Author: Dan Cross
Date: Tue, 13 Oct 2026 21:30
Date: Tue, 13 Oct 2026 21:30
33 lines
2463 bytes
2463 bytes
In article <fHtzS.249963$iAH2.193623@fx14.iad>, Scott Lurndal <slp53@pacbell.net> wrote: >cross@spitfire.i.gajendra.net (Dan Cross) writes: >>In article <11al4mu$fqtl$1@kst.eternal-september.org>, >>Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote: >>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." > >Note that the processor ABI documents for x86_64 and >arm64 also require the return value always be aligned >congruent to zero module 16. Is that true? Looking at the psABI for x86_64, I don't see that mentioned anywhere; it does say that `_Alignof(max_align_t)` is 16, but it doesn't mention `malloc` at all. POSIX is closer, and uses language close to that in C17. Arm64 refers to the AAPCS64, which doesn't go into the return type of `malloc`, either. - Dan C.
Re: AVL tree insertion - programming exercise
Author: chine.bleu
Date: Wed, 14 Oct 2026 01:01
Date: Wed, 14 Oct 2026 01:01
19 lines
2293 bytes
2293 bytes
cross@spitfire.i.gajendra.net (Dan Cross) wrote: >>> 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, [...] >> > >> >It's poorly constructed but in English it has only one >> >grammatical parse. Suitable alignment does not guarantee anything about how bits are used in the integer representation. That leads to machine dependent assumptions. Assumptions are deadly if not documented. Testing assumptions is better, -- Siri Seal of Disavowal #777-000. Disavowed. Denied. @ NO KINGS For I desire mercy not sacrifice. /|\ The Church of the Holey Apple .signature 5.5 / \ of Discordian Mysteries. This post insults Islam. Mohamed
Re: AVL tree insertion - programming exercise
Author: Keith Thompson
Date: Wed, 14 Oct 2026 03:45
Date: Wed, 14 Oct 2026 03:45
35 lines
2742 bytes
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 */
Re: AVL tree insertion - programming exercise
Author: Keith Thompson
Date: Wed, 14 Oct 2026 04:13
Date: Wed, 14 Oct 2026 04:13
50 lines
3398 bytes
3398 bytes
cross@spitfire.i.gajendra.net (Dan Cross) writes:
> In article <86v775h9o2.fsf@linuxsc.com>,
> Tim Rentsch <tr.17687@z991.linuxsc.com> wrote:
>>cross@spitfire.i.gajendra.net (Dan Cross) writes:
[...]
>>> 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, [...]
>>
>>It's poorly constructed but in English it has only one
>>grammatical parse.
>
> Nope. Not even close.
I think Tim is right about there being only one grammatical parse.
I'll try to use indentation to indicate (imperfectly) how I parse it:
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.
So "a fundamental alignment requirement" and "size less than or equal
to the size requested" are parallel requirements, both applying to
the type of the pointed-to object.
I do see a problem with the size requirement being *under* the
"suitably aligned" requirement. It wrongly implies that satisfying
the size requirement contributes to the "suitably aligned" requirement.
But that's not an ambiguous parse.
Do you see a different way to parse the sentence?
--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */
Re: AVL tree insertion - programming exercise
Author: David Brown
Date: Wed, 14 Oct 2026 07:43
Date: Wed, 14 Oct 2026 07:43
101 lines
5606 bytes
5606 bytes
On 14/10/2026 06:13, Keith Thompson wrote:
> cross@spitfire.i.gajendra.net (Dan Cross) writes:
>> In article <86v775h9o2.fsf@linuxsc.com>,
>> Tim Rentsch <tr.17687@z991.linuxsc.com> wrote:
>>> cross@spitfire.i.gajendra.net (Dan Cross) writes:
> [...]
>>>> 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, [...]
>>>
>>> It's poorly constructed but in English it has only one
>>> grammatical parse.
>>
>> Nope. Not even close.
>
> I think Tim is right about there being only one grammatical parse.
>
> I'll try to use indentation to indicate (imperfectly) how I parse it:
>
> 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.
>
> So "a fundamental alignment requirement" and "size less than or equal
> to the size requested" are parallel requirements, both applying to
> the type of the pointed-to object.
>
> I do see a problem with the size requirement being *under* the
> "suitably aligned" requirement. It wrongly implies that satisfying
> the size requirement contributes to the "suitably aligned" requirement.
> But that's not an ambiguous parse.
>
> Do you see a different way to parse the sentence?
>
I do. "Operators" in English do not have the same clearly defined
grammar rules as operators in C :
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.
equivalent to :
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
less than or equal to the size requested.
and
size
less than or equal to the size requested.
If you consider "fundamental alignment requirement" as evaluating to a
number - the result of "alignas" - then I think this parse is also
possible. I would even go as far as to say this was the intended
parsing, because the parsing you gave above would mean the paragraph
only applies to "objects with a fundamental alignment requirement" - and
all complete object types have alignment requirements.
Let us suppose a hypothetical implementation where CHAR_BIT == 8,
sizeof(int) == 4, and alignof(int) == 8.
What guarantees do you have for "void * p = malloc(4);" ?
With your parsing, "p" would be correctly aligned for an int - the
alignment of the type "int" is not relevant to whether the "p" is
correctly aligned or not, only the size of "int" matters.
With the second parsing, "p" may not be correctly aligned for an int -
it is only guaranteed to be aligned for types with alignment
requirements of 4 or less.
That second parsing seems to result in a more logical and consistent
conclusion. But the first parsing seems more reasonable when reading it.
(I do not think any of this will make a difference in practice.)
Re: AVL tree insertion - programming exercise
Author: Dan Cross
Date: Wed, 14 Oct 2026 14:13
Date: Wed, 14 Oct 2026 14:13
95 lines
5106 bytes
5106 bytes
In article <11amvh2$14pou$3@kst.eternal-september.org>,
Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
>cross@spitfire.i.gajendra.net (Dan Cross) writes:
>> In article <86v775h9o2.fsf@linuxsc.com>,
>> Tim Rentsch <tr.17687@z991.linuxsc.com> wrote:
>>>cross@spitfire.i.gajendra.net (Dan Cross) writes:
>[...]
>>>> 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, [...]
>>>
>>>It's poorly constructed but in English it has only one
>>>grammatical parse.
>>
>> Nope. Not even close.
>
>I think Tim is right about there being only one grammatical parse.
No, he is not. There are over a thousand ways to parse that
sentence "grammatically", according to the "standard" rules of
the English language (a language infamous for having little in
the way of standard rules).
We tend to discount those since we're close to the domain, but
ask a linguist what they think about this sentence and they will
point out ambiguities. Or use a tool like spaCy to visualize
the parse trees (from which one can see that those ambiguities
lead to a combinatorial explosion of valid parses).
Fortunately, most of these have overlapping meanings, and there
are only two potentially meaningful interpretations, one where
the size requirement of the request attaches only to the size of
the pointed-to type, or one where it attaches to both the size
and fundamental alignment requirement of the pointed-to type.
>I'll try to use indentation to indicate (imperfectly) how I parse it:
>
>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.
>
>So "a fundamental alignment requirement" and "size less than or equal
>to the size requested" are parallel requirements, both applying to
>the type of the pointed-to object.
That is one of the two reasonable interpretations. The other is
that the requirements placed on size and fundamental alignment
of the pointer type are cumulative and dependent on the size of
the allocation. If so, then nothing in the C standard prevents
an implementation from from returning a pointer aligned to
`alignof(char)` for `malloc(1)`.
>I do see a problem with the size requirement being *under* the
>"suitably aligned" requirement. It wrongly implies that satisfying
>the size requirement contributes to the "suitably aligned" requirement.
>But that's not an ambiguous parse.
>
>Do you see a different way to parse the sentence?
Adopting your notation, the other meaningful way is
If the allocation succeeds:
The pointer returned is suitably aligned so that:
It may be asigned to a pointer to any type of object with:
a fundamental alignment requirement _and_ size,
less than or equal to the size requested.
This is a grammatically valid parse of that sentence. Given the
existence of `aligned_alloc`, I can't think of a great reason
why this second interpretation should be prohibited; there are
esoteric hardware structures that do things like align 32-bit
registers on 128-bit boundaries (e.g., the MMIO registers for
the x86 xAPIC), but pointers to those are not the domain of
`malloc`.
Btw, this kind of ambiguity is the primary objection I have to
attempts at using informal English prose to precisely define a
programming language.
- Dan C.
Re: AVL tree insertion - programming exercise
Author: Dan Cross
Date: Wed, 14 Oct 2026 14:25
Date: Wed, 14 Oct 2026 14:25
52 lines
3179 bytes
3179 bytes
In article <11amttk$14pou$2@kst.eternal-september.org>, Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote: >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. It was just meant to `abort` if `malloc` could not allocate for whatever reason, and was unrelated to this business of stealing low bits to cache other data. The assertion is that, if the program survives these tests, then `p` is suitable for use. Sorry, I should have been clear about that. >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. I think the point is that if one wants to repurpose the low bits of the integer representation of `p` for some purpose, then one ought to verify the base assumption that those bits are available. One need not do that on every malloc call, however. In the face of weird implementations and esoteric hardware, I don't know that one can have a more general test suitable for e.g. a `static_assert()`. - Dan C.
Re: AVL tree insertion - programming exercise
Author: Janis Papanagnou
Date: Wed, 14 Oct 2026 16:03
Date: Wed, 14 Oct 2026 16:03
15 lines
1885 bytes
1885 bytes
On 2026-10-14 16:13, Dan Cross wrote: > > Btw, this kind of ambiguity is the primary objection I have to > attempts at using informal English prose to precisely define a > programming language. I'm curious; are there any more "recent" languages (i.e. younger than 50 years old) that don't use English (or any other informal language) (as opposed to a ["fully"] formal specification)? Beyond Algol 68 I found a respective statement only about ML/SML; is that all? Janis
Re: AVL tree insertion - programming exercise
Author: Tim Rentsch
Date: Wed, 14 Oct 2026 17:39
Date: Wed, 14 Oct 2026 17:39
34 lines
2578 bytes
2578 bytes
Keith Thompson <Keith.S.Thompson+u@gmail.com> writes: > cross@spitfire.i.gajendra.net (Dan Cross) writes: > >> In article <86v775h9o2.fsf@linuxsc.com>, >> Tim Rentsch <tr.17687@z991.linuxsc.com> wrote: >> >>> cross@spitfire.i.gajendra.net (Dan Cross) writes: > > [...] > >>>> 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, [...] >>> >>> It's poorly constructed but in English it has only one >>> grammatical parse. >> >> Nope. Not even close. > > I think Tim is right about there being only one grammatical parse. > [...] Thanks Keith! I owe you one. :)
Re: AVL tree insertion - programming exercise
Author: Dan Cross
Date: Wed, 14 Oct 2026 18:21
Date: Wed, 14 Oct 2026 18:21
19 lines
1608 bytes
1608 bytes
In article <11ao94e$4o6l$6@dont-email.me>, Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote: >On 2026-10-14 16:13, Dan Cross wrote: >> >> Btw, this kind of ambiguity is the primary objection I have to >> attempts at using informal English prose to precisely define a >> programming language. > >I'm curious; are there any more "recent" languages (i.e. younger >than 50 years old) that don't use English (or any other informal >language) (as opposed to a ["fully"] formal specification)? > >Beyond Algol 68 I found a respective statement only about ML/SML; >is that all? Beyond SML? I'm afraid I don't know. - Dan C.
Re: AVL tree insertion - programming exercise
Author: Dan Cross
Date: Wed, 14 Oct 2026 20:11
Date: Wed, 14 Oct 2026 20:11
84 lines
4736 bytes
4736 bytes
In article <11anbq5$16fmd$1@dont-email.me>, David Brown <david.brown@hesbynett.no> wrote: >On 14/10/2026 06:13, Keith Thompson wrote: >> [snip] >> So "a fundamental alignment requirement" and "size less than or equal >> to the size requested" are parallel requirements, both applying to >> the type of the pointed-to object. >> >> I do see a problem with the size requirement being *under* the >> "suitably aligned" requirement. It wrongly implies that satisfying >> the size requirement contributes to the "suitably aligned" requirement. >> But that's not an ambiguous parse. >> >> Do you see a different way to parse the sentence? > >I do. "Operators" in English do not have the same clearly defined >grammar rules as operators in C : > > 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. > >equivalent to : > > 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 > less than or equal to the size requested. > and > size > less than or equal to the size requested. Indeed. And in fact, I found some direct evidence for exactly this interpretation (the one I claimed was correct). Draft 3054 (https://open-std.org/JTC1/SC22/WG14/www/docs/n3054.pdf) includes this in the abstract: |The following documents, for all intents and purposes, have |been applied to this draft from before and during the October |2019 Meeting: | ... (on down to the 6th from the top on the second page): | - N2293 Alignment requirements for memory management functions In turn, N2293 goes into detail about the change, the motivation and the present wording in C23; specifically, it is clear that they mean for the requested allocation size to factor in the fundamental alignment requirement of the pointed-to type. The Summary section reads: |The alignment requirements of `malloc`, `calloc`, and `realloc` |are somewhat confusingly phrased, in a way that affects small |allocations (sizes less than `_Alignof(max_align_t))`. Some |readers (and implementations) interpret them to demand |`_Alignof(max_align_t)`-alignment even for allocation sizes |that could not hold an object with that alignment. We call |this the "strong-alignment" reading. Other readers (and |implementations) interpret them as requiring the returned |memory to be aligned only enough to accommodate those types |that could inhabit the returned memory. In particular, since |`sizeof(T) >= _Alignof(T)` for all portably defined types `T`, |allocations with sizes smaller than `_Alignof(max_align_t)` |need only be aligned to the largest power of two less than or |equal to the requested size. We call this the "weak-alignment" |reading. | |Many implementations only provide weak-alignment guarantees, |and some cases these guarantees are important in conserving |both CPU and memory. Therefore, we propose clarifying the |wording such that the weak-alignment implementation is |unambiguously allowed. Strong-alignment implementations remain |correct, and may document the guarantee as an extension. (https://www.open-std.org/JTC1/SC22/WG14/www/docs/n2293.htm) I don't think we can get much more definitive than that. - Dan C.
Re: AVL tree insertion - programming exercise
Author: Keith Thompson
Date: Wed, 14 Oct 2026 22:25
Date: Wed, 14 Oct 2026 22:25
118 lines
6408 bytes
6408 bytes
David Brown <david.brown@hesbynett.no> writes:
> On 14/10/2026 06:13, Keith Thompson wrote:
>> cross@spitfire.i.gajendra.net (Dan Cross) writes:
>>> In article <86v775h9o2.fsf@linuxsc.com>,
>>> Tim Rentsch <tr.17687@z991.linuxsc.com> wrote:
>>>> cross@spitfire.i.gajendra.net (Dan Cross) writes:
>> [...]
>>>>> 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, [...]
>>>>
>>>> It's poorly constructed but in English it has only one
>>>> grammatical parse.
>>>
>>> Nope. Not even close.
>>
>> I think Tim is right about there being only one grammatical parse.
>> I'll try to use indentation to indicate (imperfectly) how I parse
>> it:
>>
>> 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.
>>
>> So "a fundamental alignment requirement" and "size less than or equal
>> to the size requested" are parallel requirements, both applying to
>> the type of the pointed-to object.
>>
>> I do see a problem with the size requirement being *under* the
>> "suitably aligned" requirement. It wrongly implies that satisfying
>> the size requirement contributes to the "suitably aligned" requirement.
>> But that's not an ambiguous parse.
>>
>> Do you see a different way to parse the sentence?
>
> I do. "Operators" in English do not have the same clearly defined
> grammar rules as operators in C :
>
> 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.
>
> equivalent to :
>
> 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
> less than or equal to the size requested.
> and
> size
> less than or equal to the size requested.
>
> If you consider "fundamental alignment requirement" as evaluating to a
> number - the result of "alignas" - then I think this parse is also
> possible. I would even go as far as to say this was the intended
> parsing, because the parsing you gave above would mean the paragraph
> only applies to "objects with a fundamental alignment requirement" -
> and all complete object types have alignment requirements.
I think you're missing the fact that "fundamental alignment" is a
defined term. N3220 6.2.8p2:
A *fundamental alignment* is a valid alignment less than or equal to
alignof(max_align_t).
And p3:
An *extended alignment* is represented by an alignment greater
than alignof(max_align_t). It is implementation-defined whether
any extended alignments are supported and the storage durations
for which they are supported. A type having an extended alignment
requirement is an *over-aligned* type.
The point is that malloc() cannot reliably allocate objects of
over-aligned types. C23 adds aligned_alloc() for that.
I read "a fundamental alignment requirement" as a predicate; a
give object type always has an alignment, and may or may not have
a fundamental alignment requirement.
I think I now agree about the ambiguous parse, but I don't think it
matters, because an object type's alignment cannot be greater than its
size. I'm not sure that's stated explicitly, but otherwise arrays
wouldn't work (or the language would have to allow alignment gaps
between array elements).
> Let us suppose a hypothetical implementation where CHAR_BIT == 8,
> sizeof(int) == 4, and alignof(int) == 8.
That's not possible. Arrays of int cannot have gaps between the
elements.
[...]
--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */
Re: AVL tree insertion - programming exercise
Author: Keith Thompson
Date: Wed, 14 Oct 2026 22:42
Date: Wed, 14 Oct 2026 22:42
114 lines
6481 bytes
6481 bytes
cross@spitfire.i.gajendra.net (Dan Cross) writes:
> In article <11amvh2$14pou$3@kst.eternal-september.org>,
> Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
>>cross@spitfire.i.gajendra.net (Dan Cross) writes:
>>> In article <86v775h9o2.fsf@linuxsc.com>,
>>> Tim Rentsch <tr.17687@z991.linuxsc.com> wrote:
>>>>cross@spitfire.i.gajendra.net (Dan Cross) writes:
>>[...]
>>>>> 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, [...]
>>>>
>>>>It's poorly constructed but in English it has only one
>>>>grammatical parse.
>>>
>>> Nope. Not even close.
>>
>>I think Tim is right about there being only one grammatical parse.
>
> No, he is not. There are over a thousand ways to parse that
> sentence "grammatically", according to the "standard" rules of
> the English language (a language infamous for having little in
> the way of standard rules).
>
> We tend to discount those since we're close to the domain, but
> ask a linguist what they think about this sentence and they will
> point out ambiguities. Or use a tool like spaCy to visualize
> the parse trees (from which one can see that those ambiguities
> lead to a combinatorial explosion of valid parses).
>
> Fortunately, most of these have overlapping meanings, and there
> are only two potentially meaningful interpretations, one where
> the size requirement of the request attaches only to the size of
> the pointed-to type, or one where it attaches to both the size
> and fundamental alignment requirement of the pointed-to type.
>
>>I'll try to use indentation to indicate (imperfectly) how I parse it:
>>
>>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.
>>
>>So "a fundamental alignment requirement" and "size less than or equal
>>to the size requested" are parallel requirements, both applying to
>>the type of the pointed-to object.
>
> That is one of the two reasonable interpretations. The other is
> that the requirements placed on size and fundamental alignment
> of the pointer type are cumulative and dependent on the size of
> the allocation. If so, then nothing in the C standard prevents
> an implementation from from returning a pointer aligned to
> `alignof(char)` for `malloc(1)`.
I do see the parsing ambiguity now.
I believe that's consistent with the way I parsed it. A successful
malloc(1) returns a value that can be assigned to a pointer of type
char*, which points to the object type "char", which does have (a
fundamental alignment requirement) and (a size less than or equal
to the size requested).
>>I do see a problem with the size requirement being *under* the
>>"suitably aligned" requirement. It wrongly implies that satisfying
>>the size requirement contributes to the "suitably aligned" requirement.
>>But that's not an ambiguous parse.
>>
>>Do you see a different way to parse the sentence?
>
> Adopting your notation, the other meaningful way is
>
> If the allocation succeeds:
> The pointer returned is suitably aligned so that:
> It may be asigned to a pointer to any type of object with:
> a fundamental alignment requirement _and_ size,
> less than or equal to the size requested.
>
> This is a grammatically valid parse of that sentence. Given the
> existence of `aligned_alloc`, I can't think of a great reason
> why this second interpretation should be prohibited; there are
> esoteric hardware structures that do things like align 32-bit
> registers on 128-bit boundaries (e.g., the MMIO registers for
> the x86 xAPIC), but pointers to those are not the domain of
> `malloc`.
If you allow an object type to have an alignment greater than its
size, then the ambiguity can result in an actual difference in the
requirements. But then arrays don't work.
> Btw, this kind of ambiguity is the primary objection I have to
> attempts at using informal English prose to precisely define a
> programming language.
I'm not sure that anything other than English (or other human
language) prose would be a practical way to define C. There have
been attempts to define formal semantics for C, but they haven't
caught on.
--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */
Re: AVL tree insertion - programming exercise
Author: Tim Rentsch
Date: Thu, 15 Oct 2026 03:54
Date: Thu, 15 Oct 2026 03:54
59 lines
3474 bytes
3474 bytes
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. :)
Re: AVL tree insertion - programming exercise
Author: Keith Thompson
Date: Thu, 15 Oct 2026 05:47
Date: Thu, 15 Oct 2026 05:47
76 lines
4433 bytes
4433 bytes
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.
--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */
Re: AVL tree insertion - programming exercise
Author: David Brown
Date: Thu, 15 Oct 2026 07:28
Date: Thu, 15 Oct 2026 07:28
140 lines
7329 bytes
7329 bytes
On 15/10/2026 00:25, Keith Thompson wrote: > David Brown <david.brown@hesbynett.no> writes: >> On 14/10/2026 06:13, Keith Thompson wrote: >>> cross@spitfire.i.gajendra.net (Dan Cross) writes: >>>> In article <86v775h9o2.fsf@linuxsc.com>, >>>> Tim Rentsch <tr.17687@z991.linuxsc.com> wrote: >>>>> cross@spitfire.i.gajendra.net (Dan Cross) writes: >>> [...] >>>>>> 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, [...] >>>>> >>>>> It's poorly constructed but in English it has only one >>>>> grammatical parse. >>>> >>>> Nope. Not even close. >>> >>> I think Tim is right about there being only one grammatical parse. >>> I'll try to use indentation to indicate (imperfectly) how I parse >>> it: >>> >>> 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. >>> >>> So "a fundamental alignment requirement" and "size less than or equal >>> to the size requested" are parallel requirements, both applying to >>> the type of the pointed-to object. >>> >>> I do see a problem with the size requirement being *under* the >>> "suitably aligned" requirement. It wrongly implies that satisfying >>> the size requirement contributes to the "suitably aligned" requirement. >>> But that's not an ambiguous parse. >>> >>> Do you see a different way to parse the sentence? >> >> I do. "Operators" in English do not have the same clearly defined >> grammar rules as operators in C : >> >> 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. >> >> equivalent to : >> >> 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 >> less than or equal to the size requested. >> and >> size >> less than or equal to the size requested. >> >> If you consider "fundamental alignment requirement" as evaluating to a >> number - the result of "alignas" - then I think this parse is also >> possible. I would even go as far as to say this was the intended >> parsing, because the parsing you gave above would mean the paragraph >> only applies to "objects with a fundamental alignment requirement" - >> and all complete object types have alignment requirements. > > I think you're missing the fact that "fundamental alignment" is a > defined term. N3220 6.2.8p2: > > A *fundamental alignment* is a valid alignment less than or equal to > alignof(max_align_t). > I am aware of that definition, and did not miss it. > And p3: > > An *extended alignment* is represented by an alignment greater > than alignof(max_align_t). It is implementation-defined whether > any extended alignments are supported and the storage durations > for which they are supported. A type having an extended alignment > requirement is an *over-aligned* type. Again, I read that too. > > The point is that malloc() cannot reliably allocate objects of > over-aligned types. C23 adds aligned_alloc() for that. Yes. > > I read "a fundamental alignment requirement" as a predicate; a > give object type always has an alignment, and may or may not have > a fundamental alignment requirement. Yes, I can see that is how you read it. And I also think that is the more obvious reading of the paragraph. I just don't think it is the only one. > > I think I now agree about the ambiguous parse, but I don't think it > matters, because an object type's alignment cannot be greater than its > size. I'm not sure that's stated explicitly, but otherwise arrays > wouldn't work (or the language would have to allow alignment gaps > between array elements). > >> Let us suppose a hypothetical implementation where CHAR_BIT == 8, >> sizeof(int) == 4, and alignof(int) == 8. > > That's not possible. Arrays of int cannot have gaps between the > elements. > That does sound right. An implementation that wanted to keep its 32-bit ints in 64-bit slots would have to have the size of the int as the full 64-bit, with 32 of those bits being padding. So my hypothetical implementation here could not exist. (Dan's recent post - as is often the case - is more complete and considered, and better researched than mine, so I defer to that for why the changes were made to the standard and how they should be interpreted.)
Re: AVL tree insertion - programming exercise
Author: Tim Rentsch
Date: Thu, 15 Oct 2026 11:42
Date: Thu, 15 Oct 2026 11:42
99 lines
5502 bytes
5502 bytes
Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:
> 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.
I took the tests with the abort() calls to be simply protection
against an unsuitable pointer being returned. Nothing more
complicated than that.
The point I was making is different. It doesn't matter if the
bit in question is always zero as long as it is zero a fair
amount of the time. Also it doesn't have to be a low order bit,
or any pre-chosen bit, as long as some particular bit is zero
often enough. We can imagine doing a lot of malloc() calls at
program startup and tallying how often a zero appears at each bit
position, and using one of the bit positions with a high count.
Simply do enough malloc() calls until the value returned has a
zero bit at the selected bit position. The code above looks at a
fixed low order bit, but it could just as well look at a bit
whose position was chosen at run time during program startup.
> 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.
I understand what you're saying. It's mostly orthogonal to what
I was saying.
Re: AVL tree insertion - programming exercise
Author: Dan Cross
Date: Thu, 15 Oct 2026 14:25
Date: Thu, 15 Oct 2026 14:25
100 lines
5889 bytes
5889 bytes
In article <11ap0fo$1t8gq$2@kst.eternal-september.org>, Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote: >cross@spitfire.i.gajendra.net (Dan Cross) writes: >> In article <11amvh2$14pou$3@kst.eternal-september.org>, >> Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote: >>>[snip] >>>So "a fundamental alignment requirement" and "size less than or equal >>>to the size requested" are parallel requirements, both applying to >>>the type of the pointed-to object. >> >> That is one of the two reasonable interpretations. The other is >> that the requirements placed on size and fundamental alignment >> of the pointer type are cumulative and dependent on the size of >> the allocation. If so, then nothing in the C standard prevents >> an implementation from from returning a pointer aligned to >> `alignof(char)` for `malloc(1)`. > >I do see the parsing ambiguity now. > >I believe that's consistent with the way I parsed it. A successful >malloc(1) returns a value that can be assigned to a pointer of type >char*, which points to the object type "char", which does have (a >fundamental alignment requirement) and (a size less than or equal >to the size requested). Right, but I think it's a bit deeper than that. Somewhat informally, the C17 wording could be read as requiring that the pointer returned from `malloc` must be suitable for assignment to any pointer to a type with `alignof(max_align_t)` regardless of the requested size, which might be smaller. N2293 calls this the, '"strong-alignment" reading.' Under that reading, even if the requested size can't hold an object of a type with a stricter fundamental alignment requirement because it's too small, the return value from malloc is artificially constrained so that the pointer refers to an address that is aligned to `alignof(max_align_t)`. For example, an x86_64 implementation following the System V ABI would always return a pointer to an address aligned on a 16-byte boundary, even for `malloc(1)`. The N2293 wording adopted in C23 says that that need not be the case. An implementation can choose to return a pointer that is more strictly aligned for a small allocation (internally, lots of `malloc`s have some minimum allocation size that is greater than `sizeof(max_align_t)` and return all pointers aligned to whatever that is), but it does not have to (e.g., in some applications space is at a premium, so small allocations might be handled specially). >[snip] >If you allow an object type to have an alignment greater than its >size, then the ambiguity can result in an actual difference in the >requirements. But then arrays don't work. That's ok; this is only about the properties vis alignment of the pointer returned from malloc. Consider x86_64 with the System V ABI again or ARM64 with the AAPCS; suppose that `char *p = malloc(char[3]);` assigns a non-NULL ponter to `p`. The question is only whether or not the address `p` refers to is required to be aligned on a 16-byte boundary; under the '"strong-alignment" reading' in N2293, it is, even though the allocated size is only 3. Under the '"weak-alignment" reading', the return address can aligned on an even boundary, and C23 codified the weak-alignment reading. Note that neither reading says anything about the size of elements or their alignment in an allocation that is treated like an array; it only affects the alignment of the starting address. N2293 mentions this, but it probably bears repeating here: the utility of forcing `malloc` to align small allocations to e.g., `alignof(max_align_t)` seems mostly restricted to having some hope of overloading the low bits of the returned address for some program-specific purpose, which is how this came up in the first place in this thread. As has already been mentioned, the standard makes no guarnatee about the integer representation of addresses, so this is already somewhat fraught, but on many platforms we may assume that as a practical matter, for a pointer to a suitably aligned address, those bits will be clear as a practical matter; I suppose if one wants that for a small allocation, use `aligned_alloc`. Caveat emptor. >> Btw, this kind of ambiguity is the primary objection I have to >> attempts at using informal English prose to precisely define a >> programming language. > >I'm not sure that anything other than English (or other human >language) prose would be a practical way to define C. There have >been attempts to define formal semantics for C, but they haven't >caught on. Yeah. For C, I think that it would be nearly impossible to retrofit onto the language at this point. I have high hopes for Rust, but folks I've spoken to who have been active in the Rust formal methods interesting group seem pessimistic about a formally defined semantics, which is a shame. - Dan C.
Re: AVL tree insertion - programming exercise
Author: Chris M. Thomasson
Date: Thu, 15 Oct 2026 21:07
Date: Thu, 15 Oct 2026 21:07
83 lines
5150 bytes
5150 bytes
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.
Re: AVL tree insertion - programming exercise
Author: Keith Thompson
Date: Thu, 15 Oct 2026 22:54
Date: Thu, 15 Oct 2026 22:54
154 lines
8770 bytes
8770 bytes
cross@spitfire.i.gajendra.net (Dan Cross) writes:
> In article <11ap0fo$1t8gq$2@kst.eternal-september.org>,
> Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
>>cross@spitfire.i.gajendra.net (Dan Cross) writes:
>>> In article <11amvh2$14pou$3@kst.eternal-september.org>,
>>> Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
>>>>[snip]
>>>>So "a fundamental alignment requirement" and "size less than or equal
>>>>to the size requested" are parallel requirements, both applying to
>>>>the type of the pointed-to object.
>>>
>>> That is one of the two reasonable interpretations. The other is
>>> that the requirements placed on size and fundamental alignment
>>> of the pointer type are cumulative and dependent on the size of
>>> the allocation. If so, then nothing in the C standard prevents
>>> an implementation from from returning a pointer aligned to
>>> `alignof(char)` for `malloc(1)`.
>>
>>I do see the parsing ambiguity now.
>>
>>I believe that's consistent with the way I parsed it. A successful
>>malloc(1) returns a value that can be assigned to a pointer of type
>>char*, which points to the object type "char", which does have (a
>>fundamental alignment requirement) and (a size less than or equal
>>to the size requested).
>
> Right, but I think it's a bit deeper than that.
>
> Somewhat informally, the C17 wording could be read as requiring
> that the pointer returned from `malloc` must be suitable for
> assignment to any pointer to a type with `alignof(max_align_t)`
> regardless of the requested size, which might be smaller. N2293
> calls this the, '"strong-alignment" reading.'
The C17 wording is:
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).
I don't see any other way to interpret that. It requires that
a pointer returned by a successful malloc() must *always* be
suitably aligned. A pointer returned by malloc(1) must be aligned
so that it can point to a long long, for example.
It overlooks the fact that it can't point to a long long, because
there isn't room for it. And there's no portable way to tell if an
implementation violates this requirement. If an implementation's
malloc(1) returns an "odd" pointer, it violates the standard's
requirement, but that violation likely has no real effect.
For C23, it was recognized that requiring strict alignment for
tiny allocations is neither useful nor necessarily meaningful.
malloc(1) might as well be allowed to return an "odd" pointer.
> Under that reading, even if the requested size can't hold an
> object of a type with a stricter fundamental alignment
> requirement because it's too small, the return value from malloc
> is artificially constrained so that the pointer refers to an
> address that is aligned to `alignof(max_align_t)`. For example,
> an x86_64 implementation following the System V ABI would always
> return a pointer to an address aligned on a 16-byte boundary,
> even for `malloc(1)`.
And of course that conforms to both the C17 and the C2y requirements.
> The N2293 wording adopted in C23 says that that need not be the
> case. An implementation can choose to return a pointer that is
> more strictly aligned for a small allocation (internally, lots
> of `malloc`s have some minimum allocation size that is greater
> than `sizeof(max_align_t)` and return all pointers aligned to
> whatever that is), but it does not have to (e.g., in some
> applications space is at a premium, so small allocations might
> be handled specially).
Right.
>>[snip]
>>If you allow an object type to have an alignment greater than its
>>size, then the ambiguity can result in an actual difference in the
>>requirements. But then arrays don't work.
>
> That's ok; this is only about the properties vis alignment of
> the pointer returned from malloc. Consider x86_64 with the
> System V ABI again or ARM64 with the AAPCS; suppose that
> `char *p = malloc(char[3]);` assigns a non-NULL ponter to `p`.
Alas, that's a syntax error. The argument to malloc is an expression,
not a type. (I'm certain you knew that.) You can write
`malloc(sizeof (char[3])`, but that's just malloc(3); no type
information is passed.
And the alignment of char[3] is 1. An array of two char[3] objects has
a size of 6; there are no alignment gaps between the elements.
> The question is only whether or not the address `p` refers to is
> required to be aligned on a 16-byte boundary; under the
> '"strong-alignment" reading' in N2293, it is, even though the
> allocated size is only 3. Under the '"weak-alignment" reading',
> the return address can aligned on an even boundary, and C23
> codified the weak-alignment reading. Note that neither reading
> says anything about the size of elements or their alignment in
> an allocation that is treated like an array; it only affects
> the alignment of the starting address.
Under C17, the result of malloc(3) must be strongly aligned (8
bytes, for example). Under C23, it has to be at least 2 if there
are any types with 2-byte alignment.
> N2293 mentions this, but it probably bears repeating here: the
> utility of forcing `malloc` to align small allocations to e.g.,
> `alignof(max_align_t)` seems mostly restricted to having some
> hope of overloading the low bits of the returned address for
> some program-specific purpose, which is how this came up in the
> first place in this thread.
It hadn't occurred to me that that might be the reason. I assumed
it was just because strong alignment for small allocations is not
useful and should not be required. Allocating single bytes for
malloc(1) calls could have real advantages for some programs, and
the standard shouldn't prohibit that.
As I recall, there was a discussion about reusing low-order bits of
pointers, and the fact that malloc(1) no longer has to be strongly
aligned became relevant to that. It doesn't imply that that was
the reason.
There might be an ambiguity in the English grammar of the C23
statement about the alignment of malloc()ed pointers, but I don't
*think* we've seen a case where the two (or more?) different
interpretations actually lead to different semantics.
> As has already been mentioned, the
> standard makes no guarnatee about the integer representation of
> addresses, so this is already somewhat fraught, but on many
> platforms we may assume that as a practical matter, for a
> pointer to a suitably aligned address, those bits will be clear
> as a practical matter; I suppose if one wants that for a small
> allocation, use `aligned_alloc`. Caveat emptor.
Yes, if you need aligned pointers, aligned_alloc() is the way to
get them. aligned_alloc(8, 1) gives you an 8-byte aligned pointer
to a 1-byte object; if that's useful, it's because you're doing
something weird. And if you need to fiddle with the low-order
bits of aligned pointers, you could run into the problems we've
already discussed.
[snip]
--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */
Re: AVL tree insertion - programming exercise
Author: Kaz Kylheku
Date: Thu, 15 Oct 2026 23:51
Date: Thu, 15 Oct 2026 23:51
75 lines
4643 bytes
4643 bytes
On 2026-10-15, Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote: > cross@spitfire.i.gajendra.net (Dan Cross) writes: >> In article <11ap0fo$1t8gq$2@kst.eternal-september.org>, >> Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote: >>>cross@spitfire.i.gajendra.net (Dan Cross) writes: >>>> In article <11amvh2$14pou$3@kst.eternal-september.org>, >>>> Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote: >>>>>[snip] >>>>>So "a fundamental alignment requirement" and "size less than or equal >>>>>to the size requested" are parallel requirements, both applying to >>>>>the type of the pointed-to object. >>>> >>>> That is one of the two reasonable interpretations. The other is >>>> that the requirements placed on size and fundamental alignment >>>> of the pointer type are cumulative and dependent on the size of >>>> the allocation. If so, then nothing in the C standard prevents >>>> an implementation from from returning a pointer aligned to >>>> `alignof(char)` for `malloc(1)`. >>> >>>I do see the parsing ambiguity now. >>> >>>I believe that's consistent with the way I parsed it. A successful >>>malloc(1) returns a value that can be assigned to a pointer of type >>>char*, which points to the object type "char", which does have (a >>>fundamental alignment requirement) and (a size less than or equal >>>to the size requested). >> >> Right, but I think it's a bit deeper than that. >> >> Somewhat informally, the C17 wording could be read as requiring >> that the pointer returned from `malloc` must be suitable for >> assignment to any pointer to a type with `alignof(max_align_t)` >> regardless of the requested size, which might be smaller. N2293 >> calls this the, '"strong-alignment" reading.' > > The C17 wording is: > > 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). I suspect that this should be read as "any type of object which can fit into the requested size". > I don't see any other way to interpret that. It can be interpreted a a defect. Note that in a single sentence, the requirement is being given that: - the pointer from a successful allocation is aligned for any object - the pointer can be used to access such an object ... in the space allocated From that we can infer that "any type" cannot literally be any type Because if we have a successful malloc(1) and we choose "long long" to be any type, the literally interpreted requirements are that we can convert the pointer to "long long *" and use the resulting pointer for access. That requires a malloc(1) to be adjusted to malloc(sizeof (long long)); the requirement goes beyond alignment. It must be that "any type" means "any type that can be stored in the space allocated". Then the whole thing makes sense; yes, the pointer can be converted to that type and used to access such an object in the space allocated, because it fits. -- TXR Programming Language: http://nongnu.org/txr Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal Mastodon: @Kazinator@mstdn.ca
Re: AVL tree insertion - programming exercise
Author: Keith Thompson
Date: Fri, 16 Oct 2026 01:14
Date: Fri, 16 Oct 2026 01:14
109 lines
6297 bytes
6297 bytes
Kaz Kylheku <046-301-5902@kylheku.com> writes:
> On 2026-10-15, Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
>> cross@spitfire.i.gajendra.net (Dan Cross) writes:
>>> In article <11ap0fo$1t8gq$2@kst.eternal-september.org>,
>>> Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
>>>>cross@spitfire.i.gajendra.net (Dan Cross) writes:
>>>>> In article <11amvh2$14pou$3@kst.eternal-september.org>,
>>>>> Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
>>>>>>[snip]
>>>>>>So "a fundamental alignment requirement" and "size less than or equal
>>>>>>to the size requested" are parallel requirements, both applying to
>>>>>>the type of the pointed-to object.
>>>>>
>>>>> That is one of the two reasonable interpretations. The other is
>>>>> that the requirements placed on size and fundamental alignment
>>>>> of the pointer type are cumulative and dependent on the size of
>>>>> the allocation. If so, then nothing in the C standard prevents
>>>>> an implementation from from returning a pointer aligned to
>>>>> `alignof(char)` for `malloc(1)`.
>>>>
>>>>I do see the parsing ambiguity now.
>>>>
>>>>I believe that's consistent with the way I parsed it. A successful
>>>>malloc(1) returns a value that can be assigned to a pointer of type
>>>>char*, which points to the object type "char", which does have (a
>>>>fundamental alignment requirement) and (a size less than or equal
>>>>to the size requested).
>>>
>>> Right, but I think it's a bit deeper than that.
>>>
>>> Somewhat informally, the C17 wording could be read as requiring
>>> that the pointer returned from `malloc` must be suitable for
>>> assignment to any pointer to a type with `alignof(max_align_t)`
>>> regardless of the requested size, which might be smaller. N2293
>>> calls this the, '"strong-alignment" reading.'
>>
>> The C17 wording is:
>>
>> 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).
>
> I suspect that this should be read as "any type of object which can
> fit into the requested size".
>
>> I don't see any other way to interpret that.
>
> It can be interpreted a a defect.
Sure. My point is that I see no ambiguity in the C17 wording. A
non-null pointer returned by malloc() must be "suitably aligned".
Yes, it's a defect. What it says is clear. What it says is also
logically inconsistent.
Given a typical memory model (not mandated by the standard), it could
mean that a successful malloc(1) must return a pointer whose low-order N
bits are zero, where N is typically about 3 or 4. Of course the
standard couldn't express it that way.
> Note that in a single sentence, the requirement is being given that:
>
> - the pointer from a successful allocation is aligned for any object
>
> - the pointer can be used to access such an object ... in the space
> allocated
>
> From that we can infer that "any type" cannot literally be any type
> Because if we have a successful malloc(1) and we choose "long long" to
> be any type, the literally interpreted requirements are that we can
> convert the pointer to "long long *" and use the resulting pointer for
> access. That requires a malloc(1) to be adjusted to malloc(sizeof (long
> long)); the requirement goes beyond alignment.
>
> It must be that "any type" means "any type that can be
> stored in the space allocated".
But that's not what it says.
> Then the whole thing makes sense; yes, the pointer can be converted
> to that type and used to access such an object in the space allocated,
> because it fits.
Yes, the whole thing makes sense if you change the wording. That's
what C23 did (perhaps not entirely successfully); it corrected
the defect.
There is at least one possibly visible semantic change from the
C23 wording. Assume alignof(long double) > 16. In C17, this
is well defined (if malloc succeeds, and given the most nearly
reasonable interpretation of the defective C17 wording):
long double *p = malloc(1);
though any attempt to dereference p has undefined behavior. In C23,
it can have undefined behavior if the pointer returned by malloc
is not appropriately aligned. (On most implementations, the UB
doesn't do anything bad; the misaligned pointer value is simply
stored in p.)
The behavior isn't *always* undefined. The pointer returned by
malloc(1) could be appropriately aligned (always or just in this
one instance), or long double could have an alignment of 1 byte.
--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */
Re: AVL tree insertion - programming exercise
Author: Dan Cross
Date: Fri, 16 Oct 2026 03:09
Date: Fri, 16 Oct 2026 03:09
205 lines
10394 bytes
10394 bytes
In article <11arlj9$2q39c$1@kst.eternal-september.org>, Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote: >cross@spitfire.i.gajendra.net (Dan Cross) writes: >> In article <11ap0fo$1t8gq$2@kst.eternal-september.org>, >> Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote: >> >[snip] >> Somewhat informally, the C17 wording could be read as requiring >> that the pointer returned from `malloc` must be suitable for >> assignment to any pointer to a type with `alignof(max_align_t)` >> regardless of the requested size, which might be smaller. N2293 >> calls this the, '"strong-alignment" reading.' > >The C17 wording is: > > 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). > >I don't see any other way to interpret that. Nor do I, but N2293 says that some implementers did. Here's how David Goldblatt explains it: |The current wording | |C17, 7.22.3 states: "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". | |The two clauses of interest here are "it may be assigned to a |pointer to any type of object with a fundamental alignment |requirement" and "and then used to access such an object or an |array of such objects in the space allocated". Strong-alignment |readers tend to see both clauses as independent, whereas |weak-alignment readers tend to see the second as an adverbial |clause modifying the first. Neither reading is completely |satisfactory. The strong-alignment view implies an independent |clause with a tense mismatch or a missing verb; the |weak-alignment view uses "and then" when "then" would suffice, |which could imply a sequencing at odds with the interpretation. I don't see it, myself, and I'm a "strong-alignment reader", but he goes on: |In the author's view, attempting to declare one of these two |parsings correct is a waste of time. Many different |implementers, fluent in English and acting in good faith, have |parsed the sentence both ways. The question is how to |reconcile the divergence. Indeed. >It requires that >a pointer returned by a successful malloc() must *always* be >suitably aligned. A pointer returned by malloc(1) must be aligned >so that it can point to a long long, for example. > >It overlooks the fact that it can't point to a long long, because >there isn't room for it. And there's no portable way to tell if an >implementation violates this requirement. If an implementation's >malloc(1) returns an "odd" pointer, it violates the standard's >requirement, but that violation likely has no real effect. Agreed. >For C23, it was recognized that requiring strict alignment for >tiny allocations is neither useful nor necessarily meaningful. >malloc(1) might as well be allowed to return an "odd" pointer. Yes. >>>[snip] >>>If you allow an object type to have an alignment greater than its >>>size, then the ambiguity can result in an actual difference in the >>>requirements. But then arrays don't work. >> >> That's ok; this is only about the properties vis alignment of >> the pointer returned from malloc. Consider x86_64 with the >> System V ABI again or ARM64 with the AAPCS; suppose that >> `char *p = malloc(char[3]);` assigns a non-NULL ponter to `p`. > >Alas, that's a syntax error. The argument to malloc is an expression, >not a type. (I'm certain you knew that.) Indeed; I meant to write, `malloc(sizeof(char[3]))` but as you correctly surmised, I mistyped. >You can write >`malloc(sizeof (char[3])`, but that's just malloc(3); no type >information is passed. > >And the alignment of char[3] is 1. An array of two char[3] objects has >a size of 6; there are no alignment gaps between the elements. Right. The only reason I wanted to write it that way was to drive that point home; in the "strong-alignment" reading, for small allocations, the alignment of the address the pointer returned from `malloc` refers to is totally decoupled from the requested size: `sizeof(char[3])` is three, which reflects the fundamental alignment requirement of `char[3]` being 1; because `malloc` doesn't know whether the resulting allocation will be used to store something with a fundamental alignment criteria of two or not (assuming such a type exists in the implementation), it must return a pointer to an address that is at least aligned to an even boundary. In any event, whether `malloc` must return something with stronger alignment, or merely can at its own convenience, is the interesting high order bit here. But, you know, I can't type so flubbed the example. :-D >> The question is only whether or not the address `p` refers to is >> required to be aligned on a 16-byte boundary; under the >> '"strong-alignment" reading' in N2293, it is, even though the >> allocated size is only 3. Under the '"weak-alignment" reading', >> the return address can aligned on an even boundary, and C23 >> codified the weak-alignment reading. Note that neither reading >> says anything about the size of elements or their alignment in >> an allocation that is treated like an array; it only affects >> the alignment of the starting address. > >Under C17, the result of malloc(3) must be strongly aligned (8 >bytes, for example). Under C23, it has to be at least 2 if there >are any types with 2-byte alignment. Yes, that's my interpretation. >> N2293 mentions this, but it probably bears repeating here: the >> utility of forcing `malloc` to align small allocations to e.g., >> `alignof(max_align_t)` seems mostly restricted to having some >> hope of overloading the low bits of the returned address for >> some program-specific purpose, which is how this came up in the >> first place in this thread. > >It hadn't occurred to me that that might be the reason. I assumed >it was just because strong alignment for small allocations is not >useful and should not be required. Allocating single bytes for >malloc(1) calls could have real advantages for some programs, and >the standard shouldn't prohibit that. N2293 says that existing implementations differed; Goldblatt gives a fairly concise list of major implementations and which does what. David Goldblatt is one of the maintainers of `jemalloc`, and probably cares about the space efficiency of small allocations; I suspect that motivated researching the differing interpretations and became the real motivation. It came up in this thread specifically because of discussions around stealing the low bits from the integer representation of a pointer. >As I recall, there was a discussion about reusing low-order bits of >pointers, and the fact that malloc(1) no longer has to be strongly >aligned became relevant to that. It doesn't imply that that was >the reason. See above. I suspect Goldblatt wanted to reconcile the different interpretations so that the `jemalloc` behavior could be considering conformant with the standard. >There might be an ambiguity in the English grammar of the C23 >statement about the alignment of malloc()ed pointers, but I don't >*think* we've seen a case where the two (or more?) different >interpretations actually lead to different semantics. I suppose it depends on which semantics you mean. There are definitely implementations that the "weak-alignment" reading and others that take the "strong-alignment" reading. **On those latter implementations**, assuming `malloc` of some small size is successful, you can almost certainly cast the resulting pointer to `uintptr_t` and use the low bits of the result for stuff, whereas on the former implementations, you can't, unless you know that the fundamental alignment requirement for the size you request is sufficiently large so that the low bits will be clear; for `malloc(1)` it basically won't be. >> As has already been mentioned, the >> standard makes no guarnatee about the integer representation of >> addresses, so this is already somewhat fraught, but on many >> platforms we may assume that as a practical matter, for a >> pointer to a suitably aligned address, those bits will be clear >> as a practical matter; I suppose if one wants that for a small >> allocation, use `aligned_alloc`. Caveat emptor. > >Yes, if you need aligned pointers, aligned_alloc() is the way to >get them. aligned_alloc(8, 1) gives you an 8-byte aligned pointer >to a 1-byte object; if that's useful, it's because you're doing >something weird. And if you need to fiddle with the low-order >bits of aligned pointers, you could run into the problems we've >already discussed. Yup. Agreed on all counts. There _are_ some valid uses for stealing low bits, but they're pretty esoteric; for instance, this: https://dl.acm.org/doi/10.1145/3731569.3764807 But if someone is dabbling in those dark magicks, they'd better know just exactly what they're doing. - Dan C.
Re: AVL tree insertion - programming exercise
Author: Keith Thompson
Date: Fri, 16 Oct 2026 04:34
Date: Fri, 16 Oct 2026 04:34
69 lines
4511 bytes
4511 bytes
cross@spitfire.i.gajendra.net (Dan Cross) writes:
[Snip stuff I agree with]
> Right. The only reason I wanted to write it that way was to
> drive that point home; in the "strong-alignment" reading, for
> small allocations, the alignment of the address the pointer
> returned from `malloc` refers to is totally decoupled from the
> requested size: `sizeof(char[3])` is three, which reflects the
> fundamental alignment requirement of `char[3]` being 1; because
> `malloc` doesn't know whether the resulting allocation will be
> used to store something with a fundamental alignment criteria of
> two or not (assuming such a type exists in the implementation),
> it must return a pointer to an address that is at least aligned
> to an even boundary. In any event, whether `malloc` must return
> something with stronger alignment, or merely can at its own
> convenience, is the interesting high order bit here.
Just to avoid any misunderstanding, you refer to "the fundamental
alignment requirement of `char[3]` being 1".
The phrase "fundamental alignment requirement" might be misleading.
It could imply to someone unfamiliar with the terminology that
char[3] could have a fundamental alignment requirement and a
differing non-fundamental alignment requirement. (I had thought
the term "fundamental alignment" was new in C23, but it was added
in C11.)
It's possible that I'm the only one who thought this might be
confusing. If so, ignore this.
To over-explain: Each object type has an alignment, which is an integer
value.
A *fundamental alignment* is a valid alignment less than or equal to
alignof(max_align_t).
An *extended alignment* is represented by an alignment greater than
alignof(max_align_t).
As far as the language is concerned `char[3]` has an alignment of
1, and that alignment happens to be a fundamental alignment. (A
compiler might allocate standalone `char[3]` objects at a stricter
alignment, but the standard doesn't address that; compilers can
over-align anything they like.)
Which might be part of the confusion about the malloc() wording in C23.
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.
I read the phrase "with a fundamental alignment requirement" as a
predicate. A type has a fundamental alignment requirement or it
doesn't.
The alternative reading is "with a fundamental alignment requirement
(and size) less than or equal to the size requested", which says that
the alignment requirement (a) is fundamental *and* (b) is less than or
equal to the size requested. I think that's a rather strained
interpretation -- and I can't think of any cases where the distinction
between the two readings of the C23 wording could be significant, given
that an object type can't have an alignment bigger than its size.
[Snip more stuff I agree with]
--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */
Re: AVL tree insertion - programming exercise
Author: Dan Cross
Date: Fri, 16 Oct 2026 11:27
Date: Fri, 16 Oct 2026 11:27
115 lines
6273 bytes
6273 bytes
In article <11as9gl$2vtou$1@kst.eternal-september.org>, Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote: >cross@spitfire.i.gajendra.net (Dan Cross) writes: >[Snip stuff I agree with] > >> Right. The only reason I wanted to write it that way was to >> drive that point home; in the "strong-alignment" reading, for >> small allocations, the alignment of the address the pointer >> returned from `malloc` refers to is totally decoupled from the >> requested size: `sizeof(char[3])` is three, which reflects the >> fundamental alignment requirement of `char[3]` being 1; because >> `malloc` doesn't know whether the resulting allocation will be >> used to store something with a fundamental alignment criteria of >> two or not (assuming such a type exists in the implementation), >> it must return a pointer to an address that is at least aligned >> to an even boundary. In any event, whether `malloc` must return >> something with stronger alignment, or merely can at its own >> convenience, is the interesting high order bit here. > >Just to avoid any misunderstanding, you refer to "the fundamental >alignment requirement of `char[3]` being 1". What I'm trying to say (that I think you said better), is that the alignment requirement of `char` is fundamental, and that the aggregate type (the array, `char[3]`) has the same alignment requirement. In this case, the that is `1`. >The phrase "fundamental alignment requirement" might be misleading. >It could imply to someone unfamiliar with the terminology that >char[3] could have a fundamental alignment requirement and a >differing non-fundamental alignment requirement. (I had thought >the term "fundamental alignment" was new in C23, but it was added >in C11.) > >It's possible that I'm the only one who thought this might be >confusing. If so, ignore this. Hmm, I don't know that anyone else thought that, but I cannot say that they did not, and it was useful to make the point. >To over-explain: Each object type has an alignment, which is an integer >value. > > A *fundamental alignment* is a valid alignment less than or equal to > alignof(max_align_t). > > An *extended alignment* is represented by an alignment greater than > alignof(max_align_t). > >As far as the language is concerned `char[3]` has an alignment of >1, and that alignment happens to be a fundamental alignment. (A >compiler might allocate standalone `char[3]` objects at a stricter >alignment, but the standard doesn't address that; compilers can >over-align anything they like.) Yes, and succinctly put. A couple of minor quibbles: it bears mentioning that valid alignments are positive powers of two. Also, a point you had aluded to earlier: for e.g., `malloc(sizeof(char[3]))`, by the time the size argument gets to `malloc` the type information (including the alignment requirement) has been lost; malloc just sees `3`. For an implementation where there exists types with an alignment requirement of two and `sizeof(max_align_t)>=2`, such an alignment requirement is also fundamental. `malloc` cannot know that its return value won't be used to hold an object of such type, so it must return a pointer to an address that is aligned on an even boundary. >Which might be part of the confusion about the malloc() wording in C23. > > 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. > >I read the phrase "with a fundamental alignment requirement" as a >predicate. A type has a fundamental alignment requirement or it >doesn't. > >The alternative reading is "with a fundamental alignment requirement >(and size) less than or equal to the size requested", which says that >the alignment requirement (a) is fundamental *and* (b) is less than or >equal to the size requested. I think that's a rather strained >interpretation -- and I can't think of any cases where the distinction >between the two readings of the C23 wording could be significant, given >that an object type can't have an alignment bigger than its size. Yes, and I do see what you mean: it's a distinction without a difference due to other constraints imposed on types generally. Specifically (and speaking somewhat loosely), if a type has an alignment requirement that is fundamental, then it is <= the type's size; no such type may have alignment>size. Therefore, whether or not the "size" condition is to applied to the "fundamental alignment requirement" phrase in the C23 sentence, the result is the same. This is in contrast to the C17 phrasing, which was read by many good judges as requiring alignment to `>=alignof(max_align_t)` regardless of size. >[Snip more stuff I agree with] I think we're all agreed about the following: a conforming implementation may choose for a successful call to `malloc` et al to always return an "over-aligned" pointer to an address aligned to a boundary that is >= `alignof(max_align_t)`, but is not required to do so. If you want to steal the low bits of an address for something, be careful: there be dragons. - Dan C.
Re: AVL tree insertion - programming exercise
Author: Keith Thompson
Date: Fri, 16 Oct 2026 18:27
Date: Fri, 16 Oct 2026 18:27
28 lines
2416 bytes
2416 bytes
cross@spitfire.i.gajendra.net (Dan Cross) writes:
[...]
> A couple of minor quibbles: it bears mentioning that valid
> alignments are positive powers of two.
And that wasn't explicitly stated until C11. C99 talks about
alignment, but is a bit vagee about it. A hypothetical conforming
C99 implementation might have had addresses organized in powers of
3, for example. I doubt that any such implementation ever existed.
Even in C23, the definition of "alignment" is vague:
alignment
requirement that objects of a particular type be located on
storage boundaries with addresses that are particular multiples
of a byte (3.7) address
The meaning is obvious given a typical memory model, but I don't
know how you'd define "multiples of a byte address" in standardese.
I'm not even sure it's correctly stated.
In practice, I don't think this vagueness has been a problem;
everybody knows what it's *supposed* to mean.
--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */
Re: AVL tree insertion - programming exercise
Author: Kaz Kylheku
Date: Fri, 16 Oct 2026 20:26
Date: Fri, 16 Oct 2026 20:26
77 lines
4956 bytes
4956 bytes
On 2026-10-16, Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote: > Kaz Kylheku <046-301-5902@kylheku.com> writes: >> On 2026-10-15, Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote: >>> cross@spitfire.i.gajendra.net (Dan Cross) writes: >>>> In article <11ap0fo$1t8gq$2@kst.eternal-september.org>, >>>> Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote: >>>>>cross@spitfire.i.gajendra.net (Dan Cross) writes: >>>>>> In article <11amvh2$14pou$3@kst.eternal-september.org>, >>>>>> Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote: >>>>>>>[snip] >>>>>>>So "a fundamental alignment requirement" and "size less than or equal >>>>>>>to the size requested" are parallel requirements, both applying to >>>>>>>the type of the pointed-to object. >>>>>> >>>>>> That is one of the two reasonable interpretations. The other is >>>>>> that the requirements placed on size and fundamental alignment >>>>>> of the pointer type are cumulative and dependent on the size of >>>>>> the allocation. If so, then nothing in the C standard prevents >>>>>> an implementation from from returning a pointer aligned to >>>>>> `alignof(char)` for `malloc(1)`. >>>>> >>>>>I do see the parsing ambiguity now. >>>>> >>>>>I believe that's consistent with the way I parsed it. A successful >>>>>malloc(1) returns a value that can be assigned to a pointer of type >>>>>char*, which points to the object type "char", which does have (a >>>>>fundamental alignment requirement) and (a size less than or equal >>>>>to the size requested). >>>> >>>> Right, but I think it's a bit deeper than that. >>>> >>>> Somewhat informally, the C17 wording could be read as requiring >>>> that the pointer returned from `malloc` must be suitable for >>>> assignment to any pointer to a type with `alignof(max_align_t)` >>>> regardless of the requested size, which might be smaller. N2293 >>>> calls this the, '"strong-alignment" reading.' >>> >>> The C17 wording is: >>> >>> 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). >> >> I suspect that this should be read as "any type of object which can >> fit into the requested size". >> >>> I don't see any other way to interpret that. >> >> It can be interpreted a a defect. > > Sure. My point is that I see no ambiguity in the C17 wording. A > non-null pointer returned by malloc() must be "suitably aligned". > > Yes, it's a defect. What it says is clear. What it says is also > logically inconsistent. One problem is that "any type" if taken literally means all derived types, not just basic types. An example of "any type" is, say, struct tm. A program can declare much larger, more complicated struct types. The requirements, if interpreted literally, are saying that a successful pointer out of malloc(1) must be suitably aligned to point to a struct tm /and/ may be then used to access such an object (i.e a struct tm) in the space allocated, and the same for any conceivable struct type that fits into the implementation limits for conformance. It is not reasonable for malloc requests for a small size to be padded to accomodate that. -- TXR Programming Language: http://nongnu.org/txr Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal Mastodon: @Kazinator@mstdn.ca
Re: AVL tree insertion - programming exercise
Author: Keith Thompson
Date: Fri, 16 Oct 2026 22:27
Date: Fri, 16 Oct 2026 22:27
97 lines
6222 bytes
6222 bytes
Kaz Kylheku <046-301-5902@kylheku.com> writes:
> On 2026-10-16, Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
>> Kaz Kylheku <046-301-5902@kylheku.com> writes:
>>> On 2026-10-15, Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
>>>> cross@spitfire.i.gajendra.net (Dan Cross) writes:
>>>>> In article <11ap0fo$1t8gq$2@kst.eternal-september.org>,
>>>>> Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
>>>>>>cross@spitfire.i.gajendra.net (Dan Cross) writes:
>>>>>>> In article <11amvh2$14pou$3@kst.eternal-september.org>,
>>>>>>> Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
>>>>>>>>[snip]
>>>>>>>>So "a fundamental alignment requirement" and "size less than or equal
>>>>>>>>to the size requested" are parallel requirements, both applying to
>>>>>>>>the type of the pointed-to object.
>>>>>>>
>>>>>>> That is one of the two reasonable interpretations. The other is
>>>>>>> that the requirements placed on size and fundamental alignment
>>>>>>> of the pointer type are cumulative and dependent on the size of
>>>>>>> the allocation. If so, then nothing in the C standard prevents
>>>>>>> an implementation from from returning a pointer aligned to
>>>>>>> `alignof(char)` for `malloc(1)`.
>>>>>>
>>>>>>I do see the parsing ambiguity now.
>>>>>>
>>>>>>I believe that's consistent with the way I parsed it. A successful
>>>>>>malloc(1) returns a value that can be assigned to a pointer of type
>>>>>>char*, which points to the object type "char", which does have (a
>>>>>>fundamental alignment requirement) and (a size less than or equal
>>>>>>to the size requested).
>>>>>
>>>>> Right, but I think it's a bit deeper than that.
>>>>>
>>>>> Somewhat informally, the C17 wording could be read as requiring
>>>>> that the pointer returned from `malloc` must be suitable for
>>>>> assignment to any pointer to a type with `alignof(max_align_t)`
>>>>> regardless of the requested size, which might be smaller. N2293
>>>>> calls this the, '"strong-alignment" reading.'
>>>>
>>>> The C17 wording is:
>>>>
>>>> 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).
>>>
>>> I suspect that this should be read as "any type of object which can
>>> fit into the requested size".
>>>
>>>> I don't see any other way to interpret that.
>>>
>>> It can be interpreted a a defect.
>>
>> Sure. My point is that I see no ambiguity in the C17 wording. A
>> non-null pointer returned by malloc() must be "suitably aligned".
>>
>> Yes, it's a defect. What it says is clear. What it says is also
>> logically inconsistent.
>
> One problem is that "any type" if taken literally means all derived
> types, not just basic types.
>
> An example of "any type" is, say, struct tm. A program can declare
> much larger, more complicated struct types.
>
> The requirements, if interpreted literally, are saying that a successful
> pointer out of malloc(1) must be suitably aligned to point to a struct
> tm /and/ may be then used to access such an object (i.e a struct tm)
> in the space allocated, and the same for any conceivable struct type
> that fits into the implementation limits for conformance.
>
> It is not reasonable for malloc requests for a small size to be padded
> to accomodate that.
I don't see that that's more serious than the defect that's already
been acknowledged.
Clearly a pointer returned by malloc(1) can't be used to point either
to a long long int or to a struct HUGE. The C17 wording specifies
only the alignment of the returned pointer. It's poorly worded,
but I don't think anyone has interpreted it to mean that malloc(1)
must allocate more than one byte. If alignof (max_align_t) is 16,
for example, malloc(1) could return a 16-byte-aligned pointer to
one byte of allocated space. (The following 15 bytes might be
used for metadata.) C23 relaxes the alignment requirement for
small allocations.
C17 guarantees that
long double *p = malloc(1);
has defined behavior. C23 does not.
IMHO the intent of the C17 wording is clear, even though it's fairly
obviously wrong.
--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */
Re: AVL tree insertion - programming exercise
Author: Tim Rentsch
Date: Sat, 17 Oct 2026 15:21
Date: Sat, 17 Oct 2026 15:21
129 lines
8172 bytes
8172 bytes
Keith Thompson <Keith.S.Thompson+u@gmail.com> writes: > cross@spitfire.i.gajendra.net (Dan Cross) writes: > >> In article <11ap0fo$1t8gq$2@kst.eternal-september.org>, >> Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote: >> >>> cross@spitfire.i.gajendra.net (Dan Cross) writes: >>> >>>> In article <11amvh2$14pou$3@kst.eternal-september.org>, >>>> Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote: >>>> >>>>> [snip] >>>>> So "a fundamental alignment requirement" and "size less than or >>>>> equal to the size requested" are parallel requirements, both >>>>> applying to the type of the pointed-to object. >>>> >>>> That is one of the two reasonable interpretations. The other is >>>> that the requirements placed on size and fundamental alignment >>>> of the pointer type are cumulative and dependent on the size of >>>> the allocation. If so, then nothing in the C standard prevents >>>> an implementation from from returning a pointer aligned to >>>> `alignof(char)` for `malloc(1)`. >>> >>> I do see the parsing ambiguity now. >>> >>> I believe that's consistent with the way I parsed it. A successful >>> malloc(1) returns a value that can be assigned to a pointer of type >>> char*, which points to the object type "char", which does have (a >>> fundamental alignment requirement) and (a size less than or equal >>> to the size requested). >> >> Right, but I think it's a bit deeper than that. >> >> Somewhat informally, the C17 wording could be read as requiring >> that the pointer returned from `malloc` must be suitable for >> assignment to any pointer to a type with `alignof(max_align_t)` >> regardless of the requested size, which might be smaller. N2293 >> calls this the, '"strong-alignment" reading.' > > The C17 wording is: > > 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). > > I don't see any other way to interpret that. It requires that > a pointer returned by a successful malloc() must *always* be > suitably aligned. A pointer returned by malloc(1) must be aligned > so that it can point to a long long, for example. > > It overlooks the fact that it can't point to a long long, because > there isn't room for it. [...] Let's see if we can get this discussion back on the rails. Put yourself back in 1999. Forget about anything from 2000 onwards. There is just one relevant sentence in the C99 standard: The pointer returned if the allocation succeeds is suitably aligned so that it may be assigned to a pointer to any type of object and then used to access such an object in the space allocated (until the space is explicitly deallocated). The original C standard says the same thing except it says "... is explicitly freed or reallocated" rather than deallocated. The intended meaning here is straightforward. A pointer returned from malloc() may be assigned to any pointer no matter what type it points to. Non-null pointers may be used to access a suitably sized array. If the size requested is smaller than the pointed-to type, using the pointer follows the rule for zero-length flexible array members: it acts like an array of size 1 except that trying to access the element or advance to the next element is undefined behavior. In other words it's okay to use the pointer as a pointer but it can't be incremented or dereferenced. Now go forward to 2011. We want the same behavior for malloc() as in C99, only now we have to consider types with overly large alignments. For obvious reasons we don't want to force malloc() to make worst-case assumptions about what the alignment needs will be. A new function aligned_alloc() is added, with an extra parameter to specify what alignment is needed, to take care of types with overly large alignment needs. The general rule for what pointer types are covered is changed to admit the same kinds as when there were no overly large alignments; that change is put in place by adding the clause "with a fundamental alignment requirement" to the general description of memory management functions. The description of aligned_alloc() says specifically that it allocates an object having the given alignment value (and so the returned pointer value presumably may be used as such). What happens with requests whose size is smaller than what the assigned-to pointer type implies is the same as before, following the FAM rule; that rule applies to aligned_alloc() the same as is does for malloc(). Note by the way that aligned_alloc() may be used to ask for a space whose alignment is smaller than normal as well as any overly large alignment. So malloc() should be used for generic alignments, and aligned_alloc() should be used any too-small or too-large alignment. I don't have a copy of the C17 standard handy, but I expect it says the same thing about malloc() and friends as the C11 standard does. Regarding the discussion in N2293, I'm sorry to say that either the author is not too bright or he just didn't have his Wheaties that morning. He reads the words but he doesn't understand the meaning. A fair guess is that he made two common mistakes: one, he didn't consider the historical context, and two, he looked at passages in isolation without taking into account a broader context. The Standard is not meant to be read like a legal document. All the parts are expected to fit together, and in deciding what a particular passage means one needs to consider other surrounding and related passages and how they might interact. As for the arguments about how various implementations behave, the reasoning is backwards. It's the tail wagging the dog. We don't judge what the C standard means based on what implementations do; rather we decide what the C standard is meant to say and then assess whether a proposed implementation is conforming. The possibility of potential performance benefits does not excuse changing the behavior required under previous C standards. If someone thinks that's important they should use aligned_alloc(); otherwise they should just use malloc() and know that it will behave the way it always has. Bottom line: the discussion in N2293 is misguided. A technical corrigendum should be issued, putting back the language used in C11 and C17, and adding a footnote explaining that aligned_alloc() can be used for any allocation whose alignment is other than the default alignment provided by malloc().
Re: AVL tree insertion - programming exercise
Author: Keith Thompson
Date: Sat, 17 Oct 2026 17:48
Date: Sat, 17 Oct 2026 17:48
196 lines
11260 bytes
11260 bytes
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 <11ap0fo$1t8gq$2@kst.eternal-september.org>,
>>> Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
>>>> cross@spitfire.i.gajendra.net (Dan Cross) writes:
>>>>> In article <11amvh2$14pou$3@kst.eternal-september.org>,
>>>>> Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
>>>>>
>>>>>> [snip]
>>>>>> So "a fundamental alignment requirement" and "size less than or
>>>>>> equal to the size requested" are parallel requirements, both
>>>>>> applying to the type of the pointed-to object.
>>>>>
>>>>> That is one of the two reasonable interpretations. The other is
>>>>> that the requirements placed on size and fundamental alignment
>>>>> of the pointer type are cumulative and dependent on the size of
>>>>> the allocation. If so, then nothing in the C standard prevents
>>>>> an implementation from from returning a pointer aligned to
>>>>> `alignof(char)` for `malloc(1)`.
>>>>
>>>> I do see the parsing ambiguity now.
>>>>
>>>> I believe that's consistent with the way I parsed it. A successful
>>>> malloc(1) returns a value that can be assigned to a pointer of type
>>>> char*, which points to the object type "char", which does have (a
>>>> fundamental alignment requirement) and (a size less than or equal
>>>> to the size requested).
>>>
>>> Right, but I think it's a bit deeper than that.
>>>
>>> Somewhat informally, the C17 wording could be read as requiring
>>> that the pointer returned from `malloc` must be suitable for
>>> assignment to any pointer to a type with `alignof(max_align_t)`
>>> regardless of the requested size, which might be smaller. N2293
>>> calls this the, '"strong-alignment" reading.'
>>
>> The C17 wording is:
>>
>> 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).
>>
>> I don't see any other way to interpret that. It requires that
>> a pointer returned by a successful malloc() must *always* be
>> suitably aligned. A pointer returned by malloc(1) must be aligned
>> so that it can point to a long long, for example.
>>
>> It overlooks the fact that it can't point to a long long, because
>> there isn't room for it. [...]
>
> Let's see if we can get this discussion back on the rails.
>
> Put yourself back in 1999. Forget about anything from 2000 onwards.
> There is just one relevant sentence in the C99 standard:
>
> The pointer returned if the allocation succeeds is suitably
> aligned so that it may be assigned to a pointer to any type of
> object and then used to access such an object in the space
> allocated (until the space is explicitly deallocated).
>
> The original C standard says the same thing except it says "... is
> explicitly freed or reallocated" rather than deallocated.
Yes. That wording is flawed. It implies that the pointer returned
by a successful malloc(1) may be "used to access" a long double
object in the space allocated.
If we set aside the "used to access" clause, it's still clear that the
returned pointer is suitably aligned for any object. So malloc(1) must
return, for example, a 16-byte aligned pointer, even though no 16-byte
object can be stored in the allocated space.
> The intended meaning here is straightforward. A pointer returned from
> malloc() may be assigned to any pointer no matter what type it points
> to. Non-null pointers may be used to access a suitably sized array.
> If the size requested is smaller than the pointed-to type, using the
> pointer follows the rule for zero-length flexible array members: it
> acts like an array of size 1 except that trying to access the element
> or advance to the next element is undefined behavior. In other words
> it's okay to use the pointer as a pointer but it can't be incremented
> or dereferenced.
I agree that the intended meaning is straightforward, but the
words do not correctly express that intent. To understand the
intent, you have to set aside the incorrect assertion that a pointer
returned by malloc() can be used to access an object bigger than
the requested allocation.
On the other hand, here's the C17 wording:
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).
I suppose that could be read so it applies only if such an object *can*
exist in the space allocated. But I still find it ambiguous at best.
> Now go forward to 2011. We want the same behavior for malloc() as in
> C99, only now we have to consider types with overly large alignments.
> For obvious reasons we don't want to force malloc() to make worst-case
> assumptions about what the alignment needs will be. A new function
> aligned_alloc() is added, with an extra parameter to specify what
> alignment is needed, to take care of types with overly large alignment
> needs. The general rule for what pointer types are covered is changed
> to admit the same kinds as when there were no overly large alignments;
> that change is put in place by adding the clause "with a fundamental
> alignment requirement" to the general description of memory management
> functions. The description of aligned_alloc() says specifically that
> it allocates an object having the given alignment value (and so the
> returned pointer value presumably may be used as such). What happens
> with requests whose size is smaller than what the assigned-to pointer
> type implies is the same as before, following the FAM rule; that rule
> applies to aligned_alloc() the same as is does for malloc().
"FAM"?
> Note by the way that aligned_alloc() may be used to ask for a space
> whose alignment is smaller than normal as well as any overly large
> alignment. So malloc() should be used for generic alignments, and
> aligned_alloc() should be used any too-small or too-large alignment.
Suppose alignof (max_align_t) == 16. aligned_malloc(1, 1024) must still
return a 16-byte aligned pointer. This is stated explicitly in a
footnote in C23 (N3220).
> I don't have a copy of the C17 standard handy, but I expect it says
> the same thing about malloc() and friends as the C11 standard does.
>
> Regarding the discussion in N2293, I'm sorry to say that either the
> author is not too bright or he just didn't have his Wheaties that
> morning. He reads the words but he doesn't understand the meaning.
He reads the words and recognizes the flaw.
I personally don't think the "weak-alignment" reading described in
N2293 is valid, but if some implementers have actually interpreted
it that way, it's worth fixing the wording to clear it up.
> A fair guess is that he made two common mistakes: one, he didn't
> consider the historical context, and two, he looked at passages in
> isolation without taking into account a broader context. The Standard
> is not meant to be read like a legal document. All the parts are
> expected to fit together, and in deciding what a particular passage
> means one needs to consider other surrounding and related passages and
> how they might interact.
If some wording in the standard requires historical context to
understand it, that wording is flawed.
> As for the arguments about how various
> implementations behave, the reasoning is backwards. It's the tail
> wagging the dog. We don't judge what the C standard means based on
> what implementations do; rather we decide what the C standard is
> meant to say and then assess whether a proposed implementation is
> conforming. The possibility of potential performance benefits does
> not excuse changing the behavior required under previous C standards.
> If someone thinks that's important they should use aligned_alloc();
> otherwise they should just use malloc() and know that it will behave
> the way it always has.
>
> Bottom line: the discussion in N2293 is misguided. A technical
> corrigendum should be issued, putting back the language used in C11
> and C17, and adding a footnote explaining that aligned_alloc() can be
> used for any allocation whose alignment is other than the default
> alignment provided by malloc().
I don't want to restore the flawed wording from C17 and earlier.
Are you saying that malloc(1) *should* be required to return a fully
aligned pointer? Since C17 and earlier did require that, perhaps there
should have been more discussion about relaxing it by allowing malloc(1)
to return an "odd" pointer. I don't think that was a bad change, but
it could in principle break some existing code.
I see two separate issues.
1. The wording in C17 and earlier can be read to require the ability to
access a large object in a small allocated space. That's a wording
issue.
2. C17 and earlier require small allocations to be strongly aligned.
C23 drops that requirement.
If there's a good reason to continue to require the result of
malloc(1) to be strongly aligned, I wouldn't object to it - but
C23 already dropped that requirement, so imposing it again in C2y
could be problematic.
--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */
Page 2 of 3 • 114 total messages
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads