[Beowulf] Stroustrup regarding multicore
Perry E. Metzger
perry at piermont.com
Tue Aug 26 11:25:43 PDT 2008
Kyle Spaans <kspaans at student.math.uwaterloo.ca> writes:
> Either way, I've recently started writing Conway's Game of Life in
> C, as an exercise. I needed to figure out how to dynamically
> allocate a 2D array. I found an answer on the comp.lang.c FAQ[1].
> It's not terribly complex, but it seems to me like it's more
> involved than with Fortran.
It is pretty easy, as easy as Fortran. I'm not sure I understand the
issue. Lets say you have a 100*100 array type...
typedef double my_array_t[100][100]
If you have a function, and you only want the thing
allocated for the life of that function...
int foo(blah...)
{
my_array_t bar;
....
}
Or of course, you can just do it without an advance type declaration...
int foo(blah...)
{
double bar[100][100];
....
}
If you don't know the size in advance, and you're just allocating for
the life of a function:
int foo(int dim_a, int dim_b,....)
{
double bar[dim_a][dim_b]
}
works just fine (and indeed, you can use variables like that in any
block scope, not just at the start of a function block).
If you wish to allocate a my_array_t with malloc, it isn't really an
issue either.
ptr = malloc(sizeof(my_array_t))
Of course, if you don't know in advance how large your array is going
to be, you have to multiply, for example:
ptr = malloc(sizeof(double)*100*100)
> And if it came to an array of dimension >> 3, wouldn't that be a lot
> of array indicies and deferences to calculate as the dimensions
> added up.
C compilers are no worse than Fortran compilers at that.
--
Perry E. Metzger perry at piermont.com
More information about the Beowulf
mailing list