Article View: comp.lang.c
Article #435Re: AVL tree insertion - programming exercise
From: 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 */
Technical Details
Message-ID:
<11arlj9$2q39c$1@kst.eternal-september.org>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!kst.eternal-september.org!.POSTED!not-for-mailReferences:
<86ece2kppv.fsf@linuxsc.com> <11amvh2$14pou$3@kst.eternal-september.org> <11ao2mr$4l9$1@reader2.panix.com> <11ap0fo$1t8gq$2@kst.eternal-september.org> <11aqnp0$sb$1@reader2.panix.com>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!kst.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson <Keith.S.Thompson+u@gmail.com> Newsgroups: comp.lang.c Subject: Re: AVL tree insertion - programming exercise Date: Thu, 15 Oct 2026 15:54:33 -0700 Organization: None to speak of Lines: 154 Message-ID: <11arlj9$2q39c$1@kst.eternal-september.org> References: <86ece2kppv.fsf@linuxsc.com> <11amvh2$14pou$3@kst.eternal-september.org> <11ao2mr$4l9$1@reader2.panix.com> <11ap0fo$1t8gq$2@kst.eternal-september.org> <11aqnp0$sb$1@reader2.panix.com> MIME-Version: 1.0 Content-Type: text/plain Injection-Date: Thu, 15 Oct 2026 22:54:34 +0000 (UTC) Injection-Info: kst.eternal-september.org; logging-data="2952492"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/Ky2oaLBqwNP418nT+5JG9"; posting-host="9e4f1682c3cf302f0c6a1df7f7085b98" User-Agent: Gnus/5.13 (Gnus v5.13) Cancel-Lock: sha1:cSGOdu6CAaXxaq4XvKmBUFapStY= sha256:5L9qTzZJcNyGNDPgMrRPJVglSZ5aCmEWk18jDEYZV6I= sha1:OrMD85/TE3I3tx8A61ciqGLnRJI= sha256:s92JSM2qGeCxnY15i5DY1ix9m6epck3VhdLkx7npPWQ=