I couldn't find anything related to this question. Suppose I have two mpfr::mpreal
arrays, a
and b
, in the heap, as new mpfr::mpreal[n]
. I have to use C-style arrays because of a function that calls and uses the arrays. I can't modify it, I tried, but it gives wrong results, plus crashes, it's a fairly big and complicated function for my level. If the arrays can get large (both size and type/precision), I'd like to avoid a loop for copying and I'd like to use memcpy()
instead. What should I use as the 3rd argument?
I tried sizeof(mpfr::mpreal)
but it always gives 32
, no matter what I use in mpfr_set_default_prec()
. In the home page I have seen that I can use mpfr::machine_epsilon()
to better display this, but how can I use it in memcpy()
?
The mpfr::mpreal
variable (or array of them) is C++ object, it cannot be copied by memcpy
correctly. Use std::copy or loop instead.
The memcpy
just copies memory blocks bit-by-bit, which works fine for simple POD C-style data structures. C++ objects should be copied by calling the assignment operator so that object can take care of memory allocations, etc. by itself.
The size of mpfr::mpreal
is always the same since it just contains pointer to mantissa, which is allocated in heap in different memory location. The memcpy
copies just the pointer, it doesn't reallocate mantissa and thus source and destination mpreals will point to the same mantissa in memory. This is exactly what should be avoided. In turn, std::copy
takes care of this well - by copying every object in a loop using assignment operator of mpreal class (which does the necessary reallocations, etc.)
(I am author of MPFR C++).