multidimensional array function - simple question

This is a discussion on multidimensional array function - simple question within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm not an expert programmer and I'm having trouble with a function I am trying to create. I have ...

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    2

    multidimensional array function - simple question

    Hi, I'm not an expert programmer and I'm having trouble with a function I am trying to create. I have the following code for a multidimensional array.

    Code:
     int memory_4D(){
      double**** array_4D;
      int i, j, k;
      
      array_4D = (double****)malloc(NZ*sizeof(double***));	
      for(k=0; k<NZ; k++) {
        array_4D[k] = (double***)malloc(NY*sizeof(double**));
      }
      for(k=0; k<NZ; k++) {
        for(j=0; j<NY; j++) {
          array_4D[k][j] = (double**)malloc(NX*sizeof(double*));
        }
      }
      for(k=0; k<NZ; k++) {
        for(j=0; j<NY; j++) {
          for(i=0; i<NX; i++) {
    	array_4D[k][j][i] = (double*)malloc(N*sizeof(double));
          }
        }
      }
      return 0;
    }
    I am trying to create the function memory_4D that I can call to allocate memory to all of my 4D arrays. For eaxmple, I have the arrays double**** f, double**** g, and double h**** that I want to allocate memory for. I currently have the above code typed three different times with f, g, and h replacing array_4D. I know I can create the function memory_4D that I call and assign f, g, and h with the appropriate memeory. I have tried several ways of initializing and calling memory _4D and none seem to work. I'm sure this is an easy questions. Does anyone have a suggestion?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    As this is C++ and not C I would strongly recommend using new vs malloc:

    Code:
    double**** memory_4D(int NZ, int NY, int NX, int N)
    {
        double**** temp;
    
        temp = new double***[NZ];
        for( int i = 0; i < NZ; ++i )
        {
            temp[i] = new double**[NY];
            for( int j = 0; j < NY; ++j )
            {
                temp[i][j] = new double*[NX];
                for( int k = 0; k < NX; ++k )
                {
                    temp[i][j][k] = new double[N];
                }
            }
        }
    
        return temp;
    }
    
    int main()
    {
        double**** array_4D;
    
        array_4D = memory_4D(2,2,2,2);  // Create a 2x2x2x2 array for example
    
        // remember to also delete the memory allocated by new when you're done with it
    
        ...
    
        return 0;
    }
    [edit]You should also add in some checks to make sure the memory allocations are successfull.[/edit]
    Last edited by hk_mp5kpdw; 05-23-2005 at 01:23 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    If it is C++, vectors should be used anyway. Otherwise, maybe it should be moved to the C forum.

  4. #4
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    As this is C++ and not C as stated, I think this also applies:
    Why shouldn't my Matrix class's interface look like an array-of-array?
    since arrays are evil

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    If it's C you're after, this may be of interest.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >You should also add in some checks to make sure the memory allocations are successfull.
    Won't the runtime throw an exception if new is unsuccessful?

  7. #7
    Registered User
    Join Date
    May 2005
    Posts
    2
    Thanks for the advice. sorry about the post. I'm new to this. The advice helped and things are working now. Once again thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 05:23 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  3. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  4. Replies: 28
    Last Post: 07-17-2006, 12:35 AM
  5. simple char array question
    By yahn in forum C++ Programming
    Replies: 4
    Last Post: 02-08-2006, 09:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21