473,761 Members | 9,480 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to allocate memory using malloc in a 3 D array

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

http://www.thescripts.com/forum/thread546378.html
Oct 15 '06 #2
jmbn2k
14 New Member
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......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
Oct 16 '06 #3
vermarajeev
180 New Member
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 New Member
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 New Member
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 New Member
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 New Member
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........actu ally i dont hav much idea on dis dats y i asked for clear explanation n now its clear........
Oct 16 '06 #8
vermarajeev
180 New Member
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
2590
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 Solaries OS? This means that I use new to allocate memory in one Thread and doesn't use delete to release them.
7
5059
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 understand it clearly...? If anyone has a pointer on net please tell me.. I would be grateful... Some told me that for memory to be aligned, the last 6 digits of the address should be zero? is this correct?
7
7022
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
2805
by: Michael | last post by:
Hi, What's the benefit to dynamically allocate memory? using namespace std; int main() { char* ptr; ptr="abc";
4
2353
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 default-constructed T objects. 3. operator new(sizeof(T)*3)/operator delete - raw memory
0
1311
by: jmbn2k | last post by:
Hii Can anyone pls teme...how to allocate memory using malloc in a 3 D dimensional array........
13
9724
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 initialized properly as constructor same is not get called ( as per the behavior). How to call a constructor explicitly if we want to allocate memory using malloc ?
71
19121
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 problem? I cannot see why using malloc instead of new does not give the same result.
2
1777
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, srinivas
0
9531
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9345
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10115
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9905
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9775
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7332
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5229
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5373
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3881
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.