I was recently recommended the library MPFR by a friend. Looking at it, I’m severely confused as to how it’s supposed to work—it doesn’t mesh with my understanding of C at all. To initialize an MPFR value it is suggested to write something like this:
mpfr_t number;
mpfr_init2(number, 1000);
How does this function communicate its output? It merely receives a[n] (unintialized) value of type mpfr_t
and gives nothing back. To communicate back it’d have to take a pointer, right? I first checked in the source code to see if it was a macro but it doesn’t seem to be (unless __MPFR_DECLSPEC
is black magic, but it seems to expand to nothing).
The only possibilities I can think of are: 1) MPFR somehow gets the address of the mpfr_t
(on the stack) and modifies that directly. But this seems like it should be impossible or only possible via technically undefined behaviour. 2) mpfr_t
is actually a pointer and through some quirk unintialized pointers are initialized to point to the stack. But this seems unlikely.
What the hell is happening here?
mpfr_t
is an array of size 1.
So you pass the reference (pointer) to the first element of this array as arrays decay to pointers if used in expressions.
It is one of the "organic" examples of why hiding pointers and arrays behind the typedef
is a very bad idea.