Thread: Looked Everywhere (Malloc)

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    12

    Looked Everywhere (Malloc)

    I have been desperately scouring the net for information on how to use malloc, and I seriously still don't even understand the concept behind it. I just want to create a 3D array with dimensions [4][11][5], and my assignment requires dynamic mem allocation.

    I found this code that I adapted, and I guess that allocates space for my theoretical array. Now I need to define the values for it, and I don't know how to do that without doing the other way of defining values for an array, which would defeat the purpose of malloc.


    here's what I have...


    Code:
    int dim1 = 4, dim2 = 11, dim3 = 5;
     int i,j,k;
     
     double *** grades = (double ***)malloc(dim1*sizeof(double**));
    
            for (i = 0; i< dim1; i++) {
    
             grades[i] = (double **) malloc(dim2*sizeof(double *));
    
              for (j = 0; j < dim2; j++) {
    
                  grades[i][j] = (double *)malloc(dim3*sizeof(double));
              }
    
            }

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    If you don't understand something, try simplifying it.

    Do you understand the 2D case?

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    12
    No, not with malloc.

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    12
    I know it has something to do with a pointer to an array. and then for a 3D array it's a pointer to a pointer to an array...or something like that

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    you are creating an array of pointers to an array of pointers to an array of elements. that's one way to do it but complicated. if the problem allows it, a simpler way to do it is to allocate a single block of memory for the array, and then create a pointer variable of the correct type to let you do the 3d indexing into it. something like this
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc,char *argv[])
    {
            // create a POINTER to the 3d array
            // the parens are necessary to tell the compiler your are creating a POINTER to a 3d array of doubles. without
            // the parens you would be creating a 3d array of pointers to double
            double (*p)[4][3][2];
    
            // allocate space for the entire array
            p = malloc(sizeof(double) * 4 * 3 * 2);
    
            // point at the array and index into it
            // again you need the parens to dereference
            (*p)[0][0][0] = 1;
    
            return 0;
    }

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Your malloc loops seem fine (except for the redundant casts on the return result of malloc).

    All you do now is say
    grades[i][j][0] = 12.34;

    Nothing special here, it works like a regular 3D array.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. looked at tutorial and still confused
    By rafacnsoccer in forum C++ Programming
    Replies: 19
    Last Post: 06-03-2010, 02:10 PM
  2. Replies: 7
    Last Post: 05-19-2010, 02:12 AM
  3. Linker error with win16? ive looked on internet nothing worked.
    By Nathan the noob in forum C++ Programming
    Replies: 2
    Last Post: 02-21-2009, 07:24 PM
  4. Replies: 7
    Last Post: 10-01-2008, 07:45 PM
  5. I need help with Malloc()
    By electrolove in forum C Programming
    Replies: 3
    Last Post: 02-03-2003, 12:07 AM