Multi-dimensional array memory allocation.
Author Message Multi-dimensional array memory allocation.

Quote:

>I want to dynamically allocate memory to a multi-dimensional float
>array.
>I have searched around in a lot of text books and cannot find a
>solution.

Try the FAQ, it contains some pointers [sic.]

For a two-dimensional array of floats, the standard approach is to
allocate an array of pointers to float, and then to allocate an array
of floats to each of those pointers. This scheme is easily extended to
an arbitrary number of dimensions.

-- Mat.

Mon, 28 May 2001 03:00:00 GMT  Multi-dimensional array memory allocation.

Quote:

>> >I want to dynamically allocate memory to a multi-dimensional float
>> >array.
>> >I have searched around in a lot of text books and cannot find a
>> >solution.

>> Try the FAQ, it contains some pointers [sic.]

>> For a two-dimensional array of floats, the standard approach is to
>> allocate an array of pointers to float, and then to allocate an array
>> of floats to each of those pointers. This scheme is easily extended to
>> an arbitrary number of dimensions.

>> -- Mat.

>While the above works well, it is quite slow. For my numerical work I
>allocate N*M*sizeof(float) to a single bloack, then take care of
>addressing it by calculating the offset

>    position=i*M+j

>where i is the row, j is the column and M is the number of columns.
>The approach above allows you to use standard array notation foo[i][j]
>rather than calulating your own position.

You can meet half-way by allocating

M * sizeof( float * ) + M * N * sizeof( float ) + frig

and storing both the float * array and the float array in the same
block of memory. ("frig" is the possible gap between the two arrays
needed to provide proper alignment for both types)

-- Mat.

Tue, 29 May 2001 03:00:00 GMT  Multi-dimensional array memory allocation.

Quote:

> >I want to dynamically allocate memory to a multi-dimensional float
> >array.
> >I have searched around in a lot of text books and cannot find a
> >solution.

> Try the FAQ, it contains some pointers [sic.]

> For a two-dimensional array of floats, the standard approach is to
> allocate an array of pointers to float, and then to allocate an array
> of floats to each of those pointers. This scheme is easily extended to
> an arbitrary number of dimensions.

> -- Mat.

While the above works well, it is quite slow. For my numerical work I
allocate N*M*sizeof(float) to a single bloack, then take care of
addressing it by calculating the offset

position=i*M+j

where i is the row, j is the column and M is the number of columns.
The approach above allows you to use standard array notation foo[i][j]
rather than calulating your own position.
--
-------------------------------------------------------------------------

MIT Course XIII-A  Missile Subspecialty
-------------------------------------------------------------------------

Thu, 31 May 2001 03:00:00 GMT  Multi-dimensional array memory allocation.

Quote:

>> >I want to dynamically allocate memory to a multi-dimensional float
>> >array.
>> >I have searched around in a lot of text books and cannot find a
>> >solution.

>> Try the FAQ, it contains some pointers [sic.]

>> For a two-dimensional array of floats, the standard approach is to
>> allocate an array of pointers to float, and then to allocate an array
>> of floats to each of those pointers. This scheme is easily extended to
>> an arbitrary number of dimensions.

>> -- Mat.

>While the above works well, it is quite slow.

As a general statement this is simply false. There may be platforms and
access pattersn where this is slower than a calculated position but there
will also be platforms and access patterns where it is just as fast or
faster.

Quote:
>For my numerical work I
>allocate N*M*sizeof(float) to a single bloack, then take care of
>addressing it by calculating the offset

>        position=i*M+j

>where i is the row, j is the column and M is the number of columns.
>The approach above allows you to use standard array notation foo[i][j]
>rather than calulating your own position.

For numerical work you very often know the array bounds at compile time (i.e.
here M is a compile time constant). Where that is the case you can write
something like

float (*ptr)(M) = malloc(N * sizeof *ptr);

if (ptr != NULL) {
/* access ptr[i][j] */
}

--
-----------------------------------------

-----------------------------------------

Thu, 31 May 2001 03:00:00 GMT

 Page 1 of 1 [ 4 post ]

Relevant Pages