Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > 4D array to malloc

Reply
Thread Tools

4D array to malloc

 
 
Vamsee
Guest
Posts: n/a
 
      10-15-2003
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] "
 
Reply With Quote
 
 
 
 
cody
Guest
Posts: n/a
 
      10-15-2003
"Vamsee" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed) m...
> 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)));
}

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk


 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      10-15-2003
Vamsee wrote:
>
> 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][i][j][k]' to access an array element;
you've got to write `(*T)[h][i][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][i][j][k]' the way people
usually prefer.

--
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      10-15-2003
cody wrote:

> 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[i] = 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[i] = Create1DFloatArray(d1);
if(NULL == new[i])
{
/* 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[i] = Create2DFloatArray(d1, d2);
if(NULL == new[i])
{
/* 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[i] = Create3DFloatArray(d1, d2, d3);
if(NULL == new[i])
{
/* couldn't do it - take appropriate action */
}
}
}
return new;
}

--
Richard Heathfield : (E-Mail Removed)
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      10-16-2003
On 15 Oct 2003 14:42:35 -0700, (E-Mail Removed) (Vamsee) wrote:

>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>>
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      10-16-2003
Vamsee wrote:
>
> 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?

--
Chuck F ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      10-16-2003
Eric Sosman wrote:
>
> 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.

--
(E-Mail Removed)
 
Reply With Quote
 
Peter Shaggy Haywood
Guest
Posts: n/a
 
      10-21-2003
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 wrote:
>>
>> 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"?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
to malloc or not to malloc?? Johs32 C Programming 4 03-30-2006 10:03 AM
porting non-malloc code to malloc micromysore@gmail.com C Programming 3 02-19-2005 05:39 AM
Malloc/Free - freeing memory allocated by malloc Peter C Programming 34 10-22-2004 10:23 AM
free'ing malloc'd structure with malloc'd members John C Programming 13 08-02-2004 11:45 AM
Re: free'ing malloc'd structure with malloc'd members ravi C Programming 0 07-30-2004 12:42 PM



Advertisments