4D array to malloc

V

Vamsee

I have a 4D array:

float T[12][12][12][5]; ( have to go atleast upto T[60][60][60][2000]
if system supports)

How to malloc() it? can you please give me a brief illustration
because I haven't found any article dealing with this.

if I say

float (*T)[12][12][12][5];
T=(float *)malloc(60000);

it says "cannot convert float * to float *[12][12][12][5] "
 
C

cody

Vamsee said:
I have a 4D array:

float T[12][12][12][5]; ( have to go atleast upto T[60][60][60][2000]
if system supports)

How to malloc() it? can you please give me a brief illustration
because I haven't found any article dealing with this.

if I say

float (*T)[12][12][12][5];
T=(float *)malloc(60000);

it says "cannot convert float * to float *[12][12][12][5] "

When you already know the exact dimensions of your array why malloc it?
Just declare a static array.
But if you have to make it dynamically sized note that all dimensions except
the last one has to be constant.
if you only have to make the last dimension dynamically try:

int (*T)[12][12][12][ = malloc(100);

But the best solution would certainly be:

typedef struct
{
float * data;
int a, int b, int c, int d;
} ARRAY;


ARRAY AllocArray(int a, int b, int c, int d)
{
ARRAY ar;
ar.a=a;
ar.b=b;
ar.b=b;
ar.b=b;
ar.data = malloc(sizeof float * a*b*c*d);
return ar;
}

float * GetAt(Array * ar, int a, int b, int c, int d)
{
return ar->data + a + (ar->b * b + (ar->c * c + (ar->d * d)));
}
 
E

Eric Sosman

Vamsee said:
I have a 4D array:

float T[12][12][12][5]; ( have to go atleast upto T[60][60][60][2000]
if system supports)

How to malloc() it? can you please give me a brief illustration
because I haven't found any article dealing with this.

if I say

float (*T)[12][12][12][5];
T=(float *)malloc(60000);

it says "cannot convert float * to float *[12][12][12][5] "

The usual advice on comp.lang.c is "Don't cast the
result of malloc()," but here's a case where the cast
was helpful: it caused the compiler to complain, and
that drew attention to the malloc() call. Without the
cast the compiler would not have complained -- but the
malloc() call would have been incorrect anyhow, and the
error might have been harder to find.

The immediate error is that `T' is not a `float*', but
a pointer to a big 4D array of `float' objects. Removing
the cast "fixes" that problem, because malloc() returns a
`void*', which can be converted to any kind of data pointer
without explicit conversion.

The next problem is the size of the allocation. You've
asked for 60000 bytes to hold an array of 25920 elements;
that's about 2.31 bytes per element. There may be machines
somewhere where sizeof(float) is 2, but I've never encountered
one. If sizeof(float) on your machine is 4 (as is much more
common), the array needs 106380 bytes.

You can (and should) solve both of these problems with
the comp.lang.c Official Allocation Recipe:

T = malloc(sizeof *T);

However, one possible problem remains. `T' is a pointer
to a 4D array, so `*T' is the 4D array itself. That means
you can't write `T[h][j][k]' to access an array element;
you've got to write `(*T)[h][j][k]' instead. This may
be what you intended, but it seems unlikely.

A better way to proceed might be to make `T' be a pointer
to a 3D array, and then to allocate enough room for twelve
such arrays:

float (*T)[12][12][15];
T = malloc(12 * sizeof *T);

.... and now you can write `T[h][j][k]' the way people
usually prefer.
 
R

Richard Heathfield

cody said:
But if you have to make it dynamically sized note that all dimensions
except the last one has to be constant.

Sure about that, are you?

#include <stdlib.h>

double **Create1DFloatArray(size_t d0)
{
double *new = malloc(d0 * sizeof *new);
if(new != NULL)
{
size_t i;
for(i = 0; i < d0; i++)
{
new = 0.0;
}
}
return new;
}

double **Create2DFloatArray(size_t d0, size_t d1)
{
double **new = malloc(d0 * sizeof *new);
if(new != NULL)
{
size_t i;
for(i = 0; i < d0; i++)
{
new = Create1DFloatArray(d1);
if(NULL == new)
{
/* couldn't do it - take appropriate action */
}
}
}
return new;
}

double ***Create3DFloatArray(size_t d0, size_t d1, size_t d2)
{
double ***new = malloc(d0 * sizeof *new);
if(new != NULL)
{
size_t i;
for(i = 0; i < d0; i++)
{
new = Create2DFloatArray(d1, d2);
if(NULL == new)
{
/* couldn't do it - take appropriate action */
}
}
}
return new;
}

double ****Create4DFloatArray(size_t d0, size_t d1, size_t d2, size_t d3)
{
double ****new = malloc(d0 * sizeof *new);
if(new != NULL)
{
size_t i;
for(i = 0; i < d0; i++)
{
new = Create3DFloatArray(d1, d2, d3);
if(NULL == new)
{
/* couldn't do it - take appropriate action */
}
}
}
return new;
}
 
B

Barry Schwarz

I have a 4D array:

float T[12][12][12][5]; ( have to go atleast upto T[60][60][60][2000]
if system supports)

How to malloc() it? can you please give me a brief illustration
because I haven't found any article dealing with this.

if I say

float (*T)[12][12][12][5];
T=(float *)malloc(60000);

it says "cannot convert float * to float *[12][12][12][5] "

See the faq ( http://www.eskimo.com/~scs/C-faq/top.html), particularly
6.16.


<<Remove the del for email>>
 
C

CBFalconer

Vamsee said:
I have a 4D array:

float T[12][12][12][5]; ( have to go atleast upto T[60][60][60][2000]
if system supports)

How to malloc() it? can you please give me a brief illustration
because I haven't found any article dealing with this.

if I say

float (*T)[12][12][12][5];
T=(float *)malloc(60000);

it says "cannot convert float * to float *[12][12][12][5] "

sizeof float is usually 4. If so, your array requires 1728
megabytes. Do you have that much memory?
 
E

Eric Sosman

Eric said:
The next problem is the size of the allocation. You've
asked for 60000 bytes to hold an array of 25920 elements;
that's about 2.31 bytes per element. There may be machines
somewhere where sizeof(float) is 2, but I've never encountered
one. If sizeof(float) on your machine is 4 (as is much more
common), the array needs 106380 bytes.

Woops! Sorry about that; I somehow misread the array
dimensions [12][12][12][5] as [12][12][12][15]. Still, the
requested size bears no obvious relation to the actual
required size, and the moral is to let the compiler do the
size calculations for you instead of attempting them yourself.
 
P

Peter Shaggy Haywood

Groovy hepcat CBFalconer was jivin' on Thu, 16 Oct 2003 06:07:09 GMT
in comp.lang.c.
Re: 4D array to malloc's a cool scene! Dig it!
Vamsee said:
I have a 4D array:

float T[12][12][12][5]; ( have to go atleast upto T[60][60][60][2000]
if system supports)

How to malloc() it? can you please give me a brief illustration
because I haven't found any article dealing with this.

if I say

float (*T)[12][12][12][5];
T=(float *)malloc(60000);

it says "cannot convert float * to float *[12][12][12][5] "

sizeof float is usually 4. If so, your array requires 1728
megabytes. Do you have that much memory?

I make it 34,560 bytes. You didn't make the same mistake as Eric,
thinking the last dimension was 15 instead of 5, did you? Nope: even
then it only requires 103,680 bytes. Ah, I see what you've done. You
calculated the 60 * 60 * 60 * 2000 array (the one he has to "go
atleast upto"(sic.))instead of the 12 * 12 * 12 * 5 one he actually
used in the given code snippet.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top