List Info

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




n-dimension dynamic allocated array with malloc()
user name
2006-07-12 21:58:59
Hello,

What's wrong with the following code:

[fabiolocalhost 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
[fabiolocalhost projects]$


Thanks for any helpy,



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

-
To unsubscribe from this list: send the line
"unsubscribe linux-c-programming" in
the body of a message to majordomovger.kernel.org
More majordomo info at  http://vge
r.kernel.org/majordomo-info.html
n-dimension dynamic allocated array with malloc()
user name
2006-07-13 03:21:08
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 <glynngclements.plus.com>
-
To unsubscribe from this list: send the line
"unsubscribe linux-c-programming" in
the body of a message to majordomovger.kernel.org
More majordomo info at  http://vge
r.kernel.org/majordomo-info.html
[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )