Table of Contents

Name

dynamem, freeup - multidimensional dynamic array handling

Synopsis


char *dynamem(pointer,element_size,
number_dimensions,dimensions ...)
char **pointer;


freeup(pointer)
char *pointer;

Description

dynamem is the multidimensional analogue to malloc(). It is passed a number of arguments: a pointer which on exiting the procedure will point to the begining of the array, the element size, the number of dimensions required, followed by a list of the dimension sizes. To declare a 4 dimensional array normally one would code:

int array[10][11][12][13];

however, this array is then fixed at compile time. This same array can be declared dynamically at run time using the following code:

int ****array;

array = (int ****) dynamem(&array, sizeof(int), 4, 10, 11, 12, 13);

(Note that the number of levels of indirection in the cast is equal to the number of dimensions in the array.) This enables array sizes to be fixed via, for example, command line arguments.

freeup is the dynamem analogue to free(). When passed an array previously dynamically declared by dynamem the function returns this memory to the system.

dynamem attempts to set up the array required in the same way that it would be set up by the compiler at compile time. Thus a multidimensional dynamically array declared using dynamem can be used in exactly the same way as a fixed array declared by the compiler. There is obviously some overhead in the actual setting up of the array; however, this is minimal: when dynamically allocating 2 arrays of 346000 unsigned characters and one of the same number of shorts all in two dimensions, the run time of a convolution of a 7x7 Lapacian- Marr filter over an image of size 720 by 480 varied as follows:

time convolve -fbfilt -X720 -Y480 -e < bubble2 > test.1
    222.0 real 213.4 user 1.6 sys

time convolve -fbfilt -e < bubble2 > test.2
    225.2 real 212.5 user 2.7 sys

which is probably adequate. From this we can see that it takes 1.1 secs for the fixed array to be set up and zeroed and only 0.9 secs for the array to be dynamically declared using dynamem; however, using dynamem the array is not initialized to 0 and this is the reason for the 0.2 speed increase.

Files

/users/alv/everson/usr/lib/dynamem.a

See Also

malloc(3) , convolve(1)

Diagnostics


dynamem returns NULL if it is unable to allocate sufficient memory for the array.


Table of Contents