C++ Logo

std-proposals

Advanced search

Re: [std-proposals] std::big_int

From: Jan Schultke <janschultke_at_[hidden]>
Date: Thu, 2 Apr 2026 11:48:33 +0200
On Thu, 2 Apr 2026 at 10:48, Sebastian Wittmeier via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> You were mentioning returning or passing by value a std::big_int.
>
>
>
> How large would it be in common implementations? Same as shared_ptr<> with
> pointer to control block + pointer to object (or alternatively SSO)? = size
> of 2 pointers
>
>
>
> Or 2 pointers + sign bool
>

The layout would be something like:

struct big_int {
    bool is_negative;
    std::size_t limb_count; // if zero, small_value is active
    union {
        std::uint64_t small_value;
        __big_int_control_block* dynamic_value;
    };
};

struct __big_int_control_block {
    std::atomic_size_t counter;
    __big_int_limb limbs[]; // flexible array member
};

Obviously you can compact is_negative and limb_count into a single integer,
so the size of std::big_int is two pointers. You don't need two pointers to
separate limb data and control blocks. The only reason std::shared_ptr
needs two pointers is so that you can create a std::shared_ptr from a raw
pointer to pre-allocated data. If everyone always used std::shared_ptr,
there would be no need for separate allocations.

Received on 2026-04-02 09:48:51