Subject: Re: SIGSEGV error when using big float matrix under pvm
To: None <current-users@NetBSD.ORG>
From: der Mouse <mouse@Rodents.Montreal.QC.CA>
List: current-users
Date: 05/17/1997 13:09:53
This really is a C question, not a NetBSD question, but oh well.

> You are almost certainly running out of stack space.

If the array is automatic, yes.

You surely don't want to allocate one of these enormous things every
time through some recursive routine, so, just make it static, to push
it into the data segment instead of the stack segment, and all should
be well.

> The other (more correct) option would be to use malloc() or calloc(),

malloc().  calloc() is not correct for floats; all-0-bits (which is
what calloc gives you) may be nonzero; it may not even be a valid float
at all.  (Of course, you can use calloc() if you like; you just have to
treat the memory more or less as uninitialized, so the only difference
is that calloc performs more work for no benefit, compared to malloc.)

> but I dont know how to malloc multidimentional arrays offhand

You allocate an array of arrays (which is what a "multidimensional"
array in C actually is) the same way you allocate an array of anything
else:

	extern use_array(float [ARRAY_Y][ARRAY_X]);
	/* we want an array [ARRAY_Y] of array [ARRAY_X] of float */
	float (*array)[ARRAY_X]; /* pointer to array[ARRAY_X] of float */

	array = malloc(ARRAY_Y*sizeof(*array));
	... check for malloc failure ...
	use_array(array);

Compare this to the analogous code for an array of "struct foo"s:

	extern use_array(struct foo [ARRAY_SIZE]);
	/* we want an array [ARRAY_SIZE] of struct foo */
	struct foo *array; /* pointer to struct foo */

	array = malloc(ARRAY_SIZE*sizeof(*array));
	... check for malloc failure ...
	use_array(array);

					der Mouse

			       mouse@rodents.montreal.qc.ca
		     7D C8 61 52 5D E7 2D 39  4E F1 31 3E E8 B3 27 4B