Boost logo

Boost :

Subject: Re: [boost] [xint] Question about suitability, portability, and "Boostiness"
From: Scott McMurray (me22.ca+boost_at_[hidden])
Date: 2010-04-13 00:33:29


On 12 April 2010 22:57, Chad Nelson <chad.thecomfychair_at_[hidden]> wrote:
>
> Right now, the XInt library uses a pointer to a (privately defined)
> struct. The struct has several items, including a small array of "quick
> digits" and a pointer to a dynamically allocated array of digit_t types,
> both for containing the magnitude of the integer.
>

The most important optimization for me is the SBO. If the magnitude
of the int in question is less than 2**30, it should take no dynamic
storage at all. (In addition to space gains, this is a big win for
exception-safety too.)

>
> It's an old C trick, apparently legitimized in the C99 standard as the
> "struct hack". For those not familiar with it, the idea is that I'd
> allocate a single array of digit_t types, of the required size plus
> sizeof(new_data_t), then just typecast it as a pointer of type new_data_t.
>
> Does anyone know if there's any reason not to do this? Maybe some
> portability or alignment problem with it?
>

I believe the official C way is that the last element is an array
without a listed length (digits_type digits_[];), so you malloc once
for the whole type and use the array being sure not to overrun the
space you allocated. I'm not convinced such tricks are officially
allowed in C++, given that the new/delete model doesn't offer any way
to use them.

Since you just have integral types, why not just allocate new
digits_type[allocated+2] and know that index 0 is used and index 1 is
allocated? I doubt anyone would be too concerned about not being able
to store 2**37-bit integers.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk