how to allocate memory using malloc in a 3 D array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jmbn2k
    New Member
    • Oct 2006
    • 14

    how to allocate memory using malloc in a 3 D array

    Hi
    Can anyone pls teme how to allocate memory using malloc in a 3 D diemensional array.......... ...
  • tyreld
    New Member
    • Sep 2006
    • 144

    #2
    Take a look at this thread:

    Comment

    • jmbn2k
      New Member
      • Oct 2006
      • 14

      #3
      Originally posted by tyreld


      HI
      Can yo plz get me d full code coz dis was an intw question which am supposd to answer as soon as possible......a s 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

      Comment

      • vermarajeev
        New Member
        • Aug 2006
        • 180

        #4
        Code:
        int main(void)
        { 
        	int*** arr;
        	arr = new int**[2];
        	for(int i=0; i<5; i++)
        	{
        		arr[i] = new int*[5];
        		for(int j=0; j<3; j++)
        		{
        			arr[i][j] = new int[2];
        		}
        	}
        
        	for(int i=0; i<5; i++)
        	{
        		for(int j=0; j<3; j++)
        		{
        			for(int k=0; k<2; k++)
        			{
        				arr[i][j][k]=2;
                    }			
        		}	
        	}
        
        
        	for(int i=0; i<5; i++)
        	{
        		for(int j=0; j<3; j++)
        		{
        			for(int k=0; k<2; k++)
        			{
        				cout<<arr[i][j][k]<<"\t";
                    }
        			cout<<"\n";
        		}
        		cout<<"\n";
        	}
         return 0;
        }

        Comment

        • jmbn2k
          New Member
          • Oct 2006
          • 14

          #5
          Originally posted by vermarajeev
          Code:
          int main(void)
          { 
          	int*** arr;
          	arr = new int**[2];
          	for(int i=0; i<5; i++)
          	{
          		arr[i] = new int*[5];
          		for(int j=0; j<3; j++)
          		{
          			arr[i][j] = new int[2];
          		}
          	}
          
          	for(int i=0; i<5; i++)
          	{
          		for(int j=0; j<3; j++)
          		{
          			for(int k=0; k<2; k++)
          			{
          				arr[i][j][k]=2;
                      }			
          		}	
          	}
          
          
          	for(int i=0; i<5; i++)
          	{
          		for(int j=0; j<3; j++)
          		{
          			for(int k=0; k<2; k++)
          			{
          				cout<<arr[i][j][k]<<"\t";
                      }
          			cout<<"\n";
          		}
          		cout<<"\n";
          	}
           return 0;
          }
          thank you so much.......

          Comment

          • tyreld
            New Member
            • Sep 2006
            • 144

            #6
            Originally posted by vermarajeev
            Code:
            int main(void)
            { 
            	int*** arr;
            	arr = new int**[2];
            	for(int i=0; i<5; i++)
            	{
            		arr[i] = new int*[5];
            		for(int j=0; j<3; j++)
            		{
            			arr[i][j] = new int[2];
            		}
            	}
            
            	for(int i=0; i<5; i++)
            	{
            		for(int j=0; j<3; j++)
            		{
            			for(int k=0; k<2; k++)
            			{
            				arr[i][j][k]=2;
                        }			
            		}	
            	}
            
            
            	for(int i=0; i<5; i++)
            	{
            		for(int j=0; j<3; j++)
            		{
            			for(int k=0; k<2; k++)
            			{
            				cout<<arr[i][j][k]<<"\t";
                        }
            			cout<<"\n";
            		}
            		cout<<"\n";
            	}
             return 0;
            }
            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.

            Comment

            • tyreld
              New Member
              • Sep 2006
              • 144

              #7
              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.

              Code:
              #include <stdlib.h>
              
              #define X_SIZE 5
              #define Y_SIZE 4
              #define Z_SIZE 3
              
              int main(void)
              {
                 int ***array;
                 int i, j, k;
              
                 array = (int ***)malloc(sizeof(int **) * X_SIZE);
              
                 for (i = 0 ;  i < X_SIZE; i++) {
                    array[i] = (int **)malloc(sizeof(int *) * Y_SIZE);
              
                    for (j = 0; j < Y_SIZE; j++) {
                       array[i][j] = (int *)malloc(sizeof(int) * Z_SIZE);
                       
                        for (k = 0; k < Z_SIZE; k++)
                           array[i][j][k] = i * j * k;
                    }
                 }
              
                 // Do Some Stuff With Your 3D Array
              
                 return 0;
              }
              Last edited by tyreld; Oct 16 '06, 03:32 PM. Reason: fixed code typos

              Comment

              • jmbn2k
                New Member
                • Oct 2006
                • 14

                #8
                Originally posted by tyreld
                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.

                Code:
                #include <stdlib.h>
                
                #define X_SIZE 5
                #define Y_SIZE 4
                #define Z_SIZE 3
                
                int main(void)
                {
                   int ***array;
                   int i, j, k;
                
                   array = (int ***)malloc(sizeof(int **) * X_SIZE);
                
                   for (i = 0 ;  i < X_SIZE; i++) {
                      array[i] = (int **)malloc(sizeof(int *) * Y_SIZE);
                
                      for (j = 0; j < Y_SIZE; j++) {
                         array[i][j] = (int *)malloc(sizeof(int) * Z_SIZE);
                         
                          for (k = 0; k < Z_SIZE; k++)
                             array[i][j][k] = i * j * k;
                      }
                   }
                
                   // Do Some Stuff With Your 3D Array
                
                   return 0;
                }
                thanx a lot........actu ally i dont hav much idea on dis dats y i asked for clear explanation n now its clear........

                Comment

                • vermarajeev
                  New Member
                  • Aug 2006
                  • 180

                  #9
                  Originally posted by tyreld
                  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
                  Code:
                  int main(void)
                  {
                     int ***p;
                     p = new int**[5];
                     for(int i=0; i<5; ++i)
                     {
                  	p[i] = new int*[5];
                  	for(int j=0; j<5; ++j)
                  	{
                  	     p[i][j] = new int[5];
                  	}
                     }
                     return 0;
                  }

                  Comment

                  Working...