variable argument lenth mpz init

enders game endersgame99@yahoo.com
Fri, 27 Dec 2002 20:24:58 -0800 (PST)


--- Kevin Ryde <user42@zip.com.au> wrote:
> enders game <endersgame99@yahoo.com> writes:
> >
> > I tried
> > passing the actual mpz_t variables, and printing
> the
> > memory addresses, but I got copied mpz_ts and not
> > pointers.  
> 
> I think you'll find "a" and "&a" give the same
> address passed.  It
> might be easier to use "void *" in the mpz_init_va
> to see that.
> 
> This all comes from mpz_t being a one element array
> of a certain
> structure, so as a variable it makes space but as a
> function parameter
> it's a pointer.
> 

I didn't understand that before, I saw how it was
typedeffed to a one element structure but didnt see
why.  That's pretty cool, but could use a note under
integer internals.

> I guess "va_arg (ap, mpz_t)" doesn't do what might
> be hoped.  It seems
> to step by the size of the structure, not a pointer.
> 
> I guess we don't document "mpz_ptr".  "MP_INT *" as
> a throwback to gmp
> 1.x days would be a possibility.  Or forget about
> wimpy type checking
> and use "void *".
> 
> Should put an example of this in the manual I
> suppose :).

I changed the code to be more pointer oriented.  I
used a pointer to mpz_t which required a typecast from
mpz_t upon assignment, but hey a pointer is a pointer
is a pointer.

Here is the revised code:

#include <stdarg.h>
#include <stdio.h>
#include <gmp.h>

mpz_init_va(mpz_t first, ...);

main()
{
	mpz_t a, b, c;

	mpz_init_va(a, b, c, NULL);

	mpz_set_ui(a, 53);
	mpz_set_ui(b, 34);
	mpz_set_ui(c, 583);
	
	gmp_printf("a: %Zd b: %Zd c: %Zd\n", a, b, c);
}

mpz_init_va(mpz_t first, ...)
{
	mpz_t *ptr;
	va_list ap;

	ptr = (mpz_t *)first;

	va_start(ap, first);
	
	do
	{
		mpz_init(*ptr);
		ptr = va_arg(ap, mpz_t *);
	}while(ptr != NULL);

	va_end(ap);
}

PS: is it always safe to use the operand of a function
as the result receiving operand?  If the result of a
calculation is stored in the receiving operand before
the calculation is completed then the results could be corrupted.

__________________________________________________
Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now.
http://mailplus.yahoo.com