Re: n-dimension dynamic allocated array with malloc()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Fabio Miranda Hamburger wrote:

> What's wrong with the following code:

>          double ****array;

>          for(i=0;i< A; i++)
>          {
>                  array = malloc(A * sizeof(double));
>          }
>          for(i=0;i< B; i++)
>          {
>                  array[i]=malloc(B * sizeof(double));
>          }
>          for(i=0;i<C;i++)
>          {
>                  array[i][i]=malloc(C * sizeof(double));
>          }
>          for(i=0;i<D;i++)
>          {
>                  array[i][i][i]=malloc(D * sizeof(double));
>          }

You are only allocating the elements along the main diagonal.

Also, you are assuming that:

sizeof(double ***) == sizeof(double **) == sizeof(double *) == sizeof(double)

Fixed version:

	array = malloc(A * sizeof(double ***));
	for(i=0;i<A; i++)
	{
		array[i] = malloc(B * sizeof(double **));
		for(j=0;j<B;j++)
		{
			array[i][j] = malloc(C * sizeof(double *));
			for(k=0;k<C;k++)
				array[i][j][k] = malloc(D * sizeof(double));
		}
	}

In most cases, you would probably be better off without the pointers,
i.e.:

	array = malloc(A * B * C * D * sizeof(double));

	#define ARRAY(i,j,k,l) (array[(((i) * B + (j)) * C + (k)) * D + (l)])

If you access the array (or parts of it) in a linear fashion, the
compiler will optimise the multiplies away. Similarly, if any of the
constants are powers of two, the compiler will convert the multiplies
to shifts.

The only case where having 3 levels of pointers would be advantageous
is if you are accessing the array in a random (unpredictable) order
and the CPU is relatively slow at multiplication and relatively fast
at memory access. On a modern CPU with fast multiplication and slow
memory access (relative to the CPU speed), I would guess that the
version using pointers would probably be slower even for random
access.

-- 
Glynn Clements <glynn@xxxxxxxxxxxxxxxxxx>
-
: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Assembler]     [Git]     [Kernel List]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [C Programming]     [Yosemite Campsites]     [Yosemite News]     [GCC Help]

  Powered by Linux