help with memory usage

Christ Schlacta aarcane at
Tue Mar 10 23:16:03 CET 2009

On Tue, Mar 10, 2009 at 1:38 PM, Marc Glisse <marc.glisse at> wrote:
> On Tue, 10 Mar 2009, Christ Schlacta wrote:
>>> I believe you should store the mpz_class directly, not a pointer to it (a
>>> mpz_class is not much more than a pointer itself). It will save some
>>> amount
>>> of overhead and simplify the code (no need for new/delete).
>> I tried storing an mpz_class inside the prime_t class, and I
>> experienced string corruption throughout the program.  when I switched
>> to using a pointer and a new in the constructor, the problem went
>> away.
> That only means you didn't do it properly, not that it is a wrong approach.
for the record, I'm using C++ and libgmpxx, can you paste a snippett
that can handle mpz_class in a linked list container object without
pointers correctly?
>>> You could also stop relying on the default memory allocator and either
>>> tell
>>> gmp to use some other allocation function or allocate the buffers
>>> yourself
>>> (for read-only never-deallocate numbers like your prime numbers seem to
>>> be
>>> this should not be hard).
>> I have no clue how I would even do that :(
> If all your numbers fit in say 4 limbs, you could try using mpz_array_init.

my largest number so far is 1339101911.  I'm not sure if that would
fit in 4 limbs, but I'm sure I can read the documentation and find
out.  my major concern is how would this scale as the numbers get
larger as the program runs, it generates new numbers to add to the end
of the list.

on an aside, I wrote a new version that doesn't rely on storing the
whole list in memory, but because it's simpler, it's disk bound :(
I'll have to write a third version later.

> --
> Marc Glisse

 (\_/)  This is Bunny. Copy and paste Bunny
(='.'=) into your signature to help him gain
(")_(") world domination.

More information about the gmp-discuss mailing list