473,396 Members | 2,115 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 7691
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

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

Similar topics

4
by: Franklin Lee | last post by:
Hi All, I use new to allocate some memory,even I doesn't use delete to release them. When my Application exit, OS will release them. Am I right? If I'm right, how about Thread especally on...
7
by: Dhirendra Pal Singh | last post by:
Hi all, I have couple of questions, I hope I can find the answers here...(assuming the question are okay with this group) A) what is memory alignment? I have a rough idea but cant clearly...
7
by: bijax | last post by:
hi, i am new to multidimensional array of c programming .pls help me to solve out this how to allocate memory for mutidimensional array? i.e a ;
7
by: Michael | last post by:
Hi, What's the benefit to dynamically allocate memory? using namespace std; int main() { char* ptr; ptr="abc";
4
by: Dmytro Bablinyuk | last post by:
I came across several possible ways of allocating memory for objects, for example: 1. malloc(sizeof(T)*3)/free - raw memory 2. new T/delete - buffer would be initialized to...
0
by: jmbn2k | last post by:
Hii Can anyone pls teme...how to allocate memory using malloc in a 3 D dimensional array........
13
by: shsingh | last post by:
I have a class A containing some map as data variables. I creat an object of class A on heap by allocatiing memory by using "malloc". This will return me the required memory but the object is not...
71
by: desktop | last post by:
I have read in Bjarne Stroustrup that using malloc and free should be avoided in C++ because they deal with uninitialized memory and one should instead use new and delete. But why is that a...
2
by: srivas | last post by:
Hi, Every body, iam new to this website, i want to know how to allocate memory using malloc in c? if any body knows please post your answers ,i will be graceful to you people. thanks & regards,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.