linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* n-dimension dynamic allocated array with malloc()
@ 2006-07-12 21:58 Fabio Miranda Hamburger
  2006-07-13  3:21 ` Glynn Clements
  0 siblings, 1 reply; 2+ messages in thread
From: Fabio Miranda Hamburger @ 2006-07-12 21:58 UTC (permalink / raw)
  To: linux-c-programming

Hello,

What's wrong with the following code:

[fabio@localhost projects]$ cat poc.c ; gcc poc.c ; ./a.out
main()
{
         #define A 100
         #define B 100
         #define C 100
         #define D 100
         int i,j,k,l;
         double ****array;
         //srand( (unsigned) time(0x0));
         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));
         }

         for(i=0;i<A;i++)
                 for(j=0;j<B;j++)
                         for(k=0;k<C;k++)
                                 for(l=0;l<D;l++)
                                         array[i][j][k][l] = (double) 0;

}



Segmentation fault
[fabio@localhost projects]$


Thanks for any helpy,



---
Fabio Andres Miranda
Ingenieria de sistemas informaticos
Universidad Latina - Costa Rica


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: n-dimension dynamic allocated array with malloc()
  2006-07-12 21:58 n-dimension dynamic allocated array with malloc() Fabio Miranda Hamburger
@ 2006-07-13  3:21 ` Glynn Clements
  0 siblings, 0 replies; 2+ messages in thread
From: Glynn Clements @ 2006-07-13  3:21 UTC (permalink / raw)
  To: Fabio Miranda Hamburger; +Cc: linux-c-programming


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@gclements.plus.com>

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2006-07-13  3:21 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-07-12 21:58 n-dimension dynamic allocated array with malloc() Fabio Miranda Hamburger
2006-07-13  3:21 ` Glynn Clements

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).