5

I am experiencing a problem while using mpir library in C++. Is it possible to return mpz_t value from a function? When I try to do it, I have the following error:

RSA.cpp:50:36: error: ‘HASHtemp’ declared as function returning an array
mpz_t RSA::HASHtemp( mpz_t message )
adn
  • 53
  • 2
  • 3
  • Adding the type `mpz_t` serves as an alias for or the associated `class/struct/union/enum` would probably enable folks without experience with this particular library to answer this question just as well. Just a note. – cadaniluk Jan 25 '16 at 21:45
  • The GMP calling convention is designed to forbid this. Instead, take the "return value" as an extra parameter. – Tavian Barnes Jan 25 '16 at 21:45
  • 1
    [For example](https://gmplib.org/manual/Integer-Arithmetic.html) – Tavian Barnes Jan 25 '16 at 21:46
  • thank you, but I do not understand why there are function declarations who are said to return mpz_t in the documentation. – adn Jan 25 '16 at 21:49
  • 1
    @adn: There aren't. There are a couple of macros that are documented as returning an `mpz_t` result. – Keith Thompson Jan 25 '16 at 23:19
  • You can try returning values as char* and reassign them upon return. – Redsam121 May 03 '22 at 05:11

1 Answers1

5

No, it's not possible. The type mpz_t is defined as an array type:

typedef __mpz_struct mpz_t[1];

and a function cannot return an array.

It means that you can define an object of type mpz_t and then pass it as an argument to a function, allowing the function to modify its value (since arrays decay to pointers).

In a comment, you wrote:

I do not understand why there are function declarations who are said to return mpz_t in the documentation.

The documentation shows a couple of macros, not actual functions, mpq_numref and mpq_numden, that it describes as returning mpz_t values. In fact they both yield a result whose type is a pointer to the element type of the mpz_t array (__mpz_struct*). That value can be passed to a function that's documented as taking an mpz_t argument, but in fact all such functions take pointer arguments.

C and C++ do not permit either parameters of array type or functions returning array values, but they have several features that let you write code that looks as if that were possible. An expression of array type is, in most contexts, converted to a pointer, and a function parameter of array type is "adjusted" to be a pointer parameter. (Personally, I'm not a huge fan of the way GMP / MPIR takes advantage of this.)

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • Thank you for clarification. http://www.mpir.org/mpir-2.7.2.pdf in page 47, there are 2 functions returning mpz_t but I guess it is not related to what I asked. – adn Jan 25 '16 at 22:58
  • Those are macros, not actual functions. I found some examples where the result of `mpq_numref()` is passed to `mpz_add()`. Apparently it yields, not an `mpz_t`, but a pointer to the element type -- which is what `mpz_add` takes as an argument. You cannot, for example, assign the result of `mpq_numref` to an `mpz_t` object. – Keith Thompson Jan 25 '16 at 23:18