Subject: Re: SIGSEGV error when using big float matrix under pvm
To: der Mouse <mouse@Rodents.Montreal.QC.CA>
From: Andrew Reilly <andrew@zeta.org.au>
List: current-users
Date: 05/19/1997 10:39:31
On Sat, May 17, 1997 at 01:09:53PM -0400, der Mouse wrote:
> This really is a C question, not a NetBSD question, but oh well.

This even more so...  Tell me to go away if you feel it's
not relevant...

> 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.)

Well, I agree that using malloc is better since you probably
don't want the zeros anyway, but I'm pretty sure that IEEE
floats are organised so that binary zero is float and double
zero too.  I doubt whether there are any machines that
support NetBSD that don't have IEEE floats.  (Vax maybe?  Is
that port running?)

> > 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:

If you're trying to do any serious math, that is NOT the way
you want to do it: it's just too inefficient.  C doesn't
have good multi-dimensional matrix support, so you just have
to grin and bear it.  The "right way" to do it is

float *M = (float *)malloc(x_size * y_size * sizeof(float));

and you have to do your indexing as

	float a_x_y = M[y * x_size + x];

and trust your compiler to do as much common sub-expression
hoisting and strength reduction as it can.

Using arrays of arrays may let you use the indexing syntax,
but it costs you an extra, usually non-cacheable, usually
pipeline stalling memory reference for every element.  The
index math is going to be much faster on a modern processor,
and will usually go away altogether with a good compiler.

-- 
Andrew

"The steady state of disks is full."
				-- Ken Thompson