Public mpz_ptr and mpz_srcptr

Torbjorn Granlund tg at
Mon Feb 4 14:26:38 CET 2013

Vincent Lefevre <vincent at> writes:

  On 2013-02-04 13:57:16 +0100, Torbjorn Granlund wrote:
  > I chose to not provide mpz_ptr since I think it would be prone to
  > errors.  I expect code like this to be created:
  > mpz_ptr
  > foo (mpz_t a, mpz_t b)
  > {
  >   mpz_t c;
  >   mpz_init (c);
  >   mpz_add (c, a, b);
  >   return c;
  > }
  This is not specific to GMP: every C coder should know that returning
  a local variable is incorrect.

It is?  What is wrong with

    foo (int a, int b)
      int c;
      c = a + b;
      return c;


I don't think it is self-evident that mpz_t is special.  We could infact
had defined it so that it wasn't special at all, but letting it be a
pointer to, say, an array of size, allocation, followed by an array of

  A good compiler (like GCC) should be able to detect that and output a
  warning (GCC does that by default).  So, in practice, there are no
  risks of errors.



More information about the gmp-devel mailing list