468,491 Members | 2,072 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,491 developers. It's quick & easy.

how to allocate memory using malloc in a 3 D array

14
Hi
Can anyone pls teme how to allocate memory using malloc in a 3 D diemensional array.............
Oct 15 '06 #1
8 7319
tyreld
144 100+
Take a look at this thread:

http://www.thescripts.com/forum/thread546378.html
Oct 15 '06 #2
jmbn2k
14
Take a look at this thread:

http://www.thescripts.com/forum/thread546378.html


HI
Can yo plz get me d full code coz dis was an intw question which am supposd to answer as soon as possible......as am a fresher i don hav much knowledge in dis conept plz......help me wit the code n the explanation of d code plz.......thank you so much for your previous reply
Oct 16 '06 #3
vermarajeev
180 100+
Expand|Select|Wrap|Line Numbers
  1. int main(void)
  2.     int*** arr;
  3.     arr = new int**[2];
  4.     for(int i=0; i<5; i++)
  5.     {
  6.         arr[i] = new int*[5];
  7.         for(int j=0; j<3; j++)
  8.         {
  9.             arr[i][j] = new int[2];
  10.         }
  11.     }
  12.  
  13.     for(int i=0; i<5; i++)
  14.     {
  15.         for(int j=0; j<3; j++)
  16.         {
  17.             for(int k=0; k<2; k++)
  18.             {
  19.                 arr[i][j][k]=2;
  20.             }            
  21.         }    
  22.     }
  23.  
  24.  
  25.     for(int i=0; i<5; i++)
  26.     {
  27.         for(int j=0; j<3; j++)
  28.         {
  29.             for(int k=0; k<2; k++)
  30.             {
  31.                 cout<<arr[i][j][k]<<"\t";
  32.             }
  33.             cout<<"\n";
  34.         }
  35.         cout<<"\n";
  36.     }
  37.  return 0;
  38. }
Oct 16 '06 #4
jmbn2k
14
Expand|Select|Wrap|Line Numbers
  1. int main(void)
  2.     int*** arr;
  3.     arr = new int**[2];
  4.     for(int i=0; i<5; i++)
  5.     {
  6.         arr[i] = new int*[5];
  7.         for(int j=0; j<3; j++)
  8.         {
  9.             arr[i][j] = new int[2];
  10.         }
  11.     }
  12.  
  13.     for(int i=0; i<5; i++)
  14.     {
  15.         for(int j=0; j<3; j++)
  16.         {
  17.             for(int k=0; k<2; k++)
  18.             {
  19.                 arr[i][j][k]=2;
  20.             }            
  21.         }    
  22.     }
  23.  
  24.  
  25.     for(int i=0; i<5; i++)
  26.     {
  27.         for(int j=0; j<3; j++)
  28.         {
  29.             for(int k=0; k<2; k++)
  30.             {
  31.                 cout<<arr[i][j][k]<<"\t";
  32.             }
  33.             cout<<"\n";
  34.         }
  35.         cout<<"\n";
  36.     }
  37.  return 0;
  38. }
thank you so much.......
Oct 16 '06 #5
tyreld
144 100+
Expand|Select|Wrap|Line Numbers
  1. int main(void)
  2.     int*** arr;
  3.     arr = new int**[2];
  4.     for(int i=0; i<5; i++)
  5.     {
  6.         arr[i] = new int*[5];
  7.         for(int j=0; j<3; j++)
  8.         {
  9.             arr[i][j] = new int[2];
  10.         }
  11.     }
  12.  
  13.     for(int i=0; i<5; i++)
  14.     {
  15.         for(int j=0; j<3; j++)
  16.         {
  17.             for(int k=0; k<2; k++)
  18.             {
  19.                 arr[i][j][k]=2;
  20.             }            
  21.         }    
  22.     }
  23.  
  24.  
  25.     for(int i=0; i<5; i++)
  26.     {
  27.         for(int j=0; j<3; j++)
  28.         {
  29.             for(int k=0; k<2; k++)
  30.             {
  31.                 cout<<arr[i][j][k]<<"\t";
  32.             }
  33.             cout<<"\n";
  34.         }
  35.         cout<<"\n";
  36.     }
  37.  return 0;
  38. }
This code has failed to answer the question. First, the question asked about using "malloc" not the "new" operator. Second, you have provided faulty code demonstrating that you yourself do not understand the concept. The code you have provided exceeds the bounds of the array you define. The first example being that your first dimension is intialized to hold 2 elements yet you use a bounds of 5 when trying to allocate the second dimension.
Oct 16 '06 #6
tyreld
144 100+
The original thread I referenced should have contained enough information to answer your question. Here is the code again including an extra for loop that initializes each value of the array to the product of its indices.

Expand|Select|Wrap|Line Numbers
  1. #include <stdlib.h>
  2.  
  3. #define X_SIZE 5
  4. #define Y_SIZE 4
  5. #define Z_SIZE 3
  6.  
  7. int main(void)
  8. {
  9.    int ***array;
  10.    int i, j, k;
  11.  
  12.    array = (int ***)malloc(sizeof(int **) * X_SIZE);
  13.  
  14.    for (i = 0 ;  i < X_SIZE; i++) {
  15.       array[i] = (int **)malloc(sizeof(int *) * Y_SIZE);
  16.  
  17.       for (j = 0; j < Y_SIZE; j++) {
  18.          array[i][j] = (int *)malloc(sizeof(int) * Z_SIZE);
  19.  
  20.           for (k = 0; k < Z_SIZE; k++)
  21.              array[i][j][k] = i * j * k;
  22.       }
  23.    }
  24.  
  25.    // Do Some Stuff With Your 3D Array
  26.  
  27.    return 0;
  28. }
  29.  
Oct 16 '06 #7
jmbn2k
14
The original thread I referenced should have contained enough information to answer your question. Here is the code again including an extra for loop that initializes each value of the array to the product of its indices.

Expand|Select|Wrap|Line Numbers
  1. #include <stdlib.h>
  2.  
  3. #define X_SIZE 5
  4. #define Y_SIZE 4
  5. #define Z_SIZE 3
  6.  
  7. int main(void)
  8. {
  9.    int ***array;
  10.    int i, j, k;
  11.  
  12.    array = (int ***)malloc(sizeof(int **) * X_SIZE);
  13.  
  14.    for (i = 0 ;  i < X_SIZE; i++) {
  15.       array[i] = (int **)malloc(sizeof(int *) * Y_SIZE);
  16.  
  17.       for (j = 0; j < Y_SIZE; j++) {
  18.          array[i][j] = (int *)malloc(sizeof(int) * Z_SIZE);
  19.  
  20.           for (k = 0; k < Z_SIZE; k++)
  21.              array[i][j][k] = i * j * k;
  22.       }
  23.    }
  24.  
  25.    // Do Some Stuff With Your 3D Array
  26.  
  27.    return 0;
  28. }
  29.  
thanx a lot........actually i dont hav much idea on dis dats y i asked for clear explanation n now its clear........
Oct 16 '06 #8
vermarajeev
180 100+
This code has failed to answer the question. First, the question asked about using "malloc" not the "new" operator. Second, you have provided faulty code demonstrating that you yourself do not understand the concept. The code you have provided exceeds the bounds of the array you define. The first example being that your first dimension is intialized to hold 2 elements yet you use a bounds of 5 when trying to allocate the second dimension.
Hi tyreId,
Thankx for your comments,
I'm accept my mistake....
I think I was unsure about what I wrote

Also I just provided solution using new coz the thread link already contains solution using malloc and I thought to implement using new....

Never mind thankx for your comments and I think here is the solution you were looking for
Expand|Select|Wrap|Line Numbers
  1. int main(void)
  2. {
  3.    int ***p;
  4.    p = new int**[5];
  5.    for(int i=0; i<5; ++i)
  6.    {
  7.     p[i] = new int*[5];
  8.     for(int j=0; j<5; ++j)
  9.     {
  10.          p[i][j] = new int[5];
  11.     }
  12.    }
  13.    return 0;
  14. }
Oct 17 '06 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Franklin Lee | last post: by
7 posts views Thread by Dhirendra Pal Singh | last post: by
4 posts views Thread by Dmytro Bablinyuk | last post: by
71 posts views Thread by desktop | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by theflame83 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.