4D array to malloc

Discussion in 'C Programming' started by Vamsee, Oct 15, 2003.

  1. Vamsee

    Vamsee Guest

    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] "
     
    Vamsee, Oct 15, 2003
    #1
    1. Advertising

  2. Vamsee

    cody Guest

    "Vamsee" <> schrieb im Newsbeitrag
    news:...
    > 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
     
    cody, Oct 15, 2003
    #2
    1. Advertising

  3. Vamsee

    Eric Sosman Guest

    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][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.

    --
     
    Eric Sosman, Oct 15, 2003
    #3
  4. 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 = 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;
    }

    --
    Richard Heathfield :
    "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
     
    Richard Heathfield, Oct 16, 2003
    #4
  5. On 15 Oct 2003 14:42:35 -0700, (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>>
     
    Barry Schwarz, Oct 16, 2003
    #5
  6. Vamsee

    CBFalconer Guest

    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 () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
     
    CBFalconer, Oct 16, 2003
    #6
  7. Vamsee

    Eric Sosman Guest

    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.

    --
     
    Eric Sosman, Oct 16, 2003
    #7
  8. 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"?
     
    Peter Shaggy Haywood, Oct 21, 2003
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John
    Replies:
    13
    Views:
    733
  2. ravi
    Replies:
    0
    Views:
    481
  3. Peter
    Replies:
    34
    Views:
    2,054
    Richard Tobin
    Oct 22, 2004
  4. porting non-malloc code to malloc

    , Feb 18, 2005, in forum: C Programming
    Replies:
    3
    Views:
    502
    Walter Roberson
    Feb 19, 2005
  5. Johs32

    to malloc or not to malloc??

    Johs32, Mar 30, 2006, in forum: C Programming
    Replies:
    4
    Views:
    340
    Captain Winston
    Mar 30, 2006
Loading...

Share This Page