Date: Fri, 31 Oct 2025 17:07:57 +0000
Bo Persson wrote:
> On 2025-10-31 at 11:41, Marcin Jaczewski via Std-Proposals wrote:
> > Would it be possible to recover this information from the pointer
> > itself? If it had alignment to some specific limit then the allocator
> > is required to return the same on the new pointer.
> > It could sometimes have "false positives" but overall it probably
> > should not have a negative impact on total memory usage.
>
> I guess it would be more than "sometimes". Assuming that your very first
> allocation from a fresh heap happens to be page aligned, what is its "true"
> alignment?
On average, my guess is everything would be twice too much aligned,
either creating a huge amount of fragmentation or taking up twice the
allocation space depending on implementation.
A pointer aligned to bit N has a 50% probability of being aligned to bit N+1
(twice the expected alignment), 25% aligned to N+2 (4 times expected), etc...
Summing this converges on average to twice the overall expected alignments.
Marc
> On 2025-10-31 at 11:41, Marcin Jaczewski via Std-Proposals wrote:
> > Would it be possible to recover this information from the pointer
> > itself? If it had alignment to some specific limit then the allocator
> > is required to return the same on the new pointer.
> > It could sometimes have "false positives" but overall it probably
> > should not have a negative impact on total memory usage.
>
> I guess it would be more than "sometimes". Assuming that your very first
> allocation from a fresh heap happens to be page aligned, what is its "true"
> alignment?
On average, my guess is everything would be twice too much aligned,
either creating a huge amount of fragmentation or taking up twice the
allocation space depending on implementation.
A pointer aligned to bit N has a 50% probability of being aligned to bit N+1
(twice the expected alignment), 25% aligned to N+2 (4 times expected), etc...
Summing this converges on average to twice the overall expected alignments.
Marc
Received on 2025-10-31 17:08:03
