473,499 Members | 1,593 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

dynamic 3D array memory alllocation

1 New Member
can u plz giv a code in c for allocating a memory for 3D array.
Oct 8 '06 #1
3 14815
tyreld
144 New Member
[code]

int ***3d_array;
int i;

3d_array = (int ***)malloc(sizeof(int **) * DEPTH_SIZE);

for (i = 0; i < DEPTH_SIZE; i++)
Oct 9 '06 #2
tyreld
144 New Member
Here is one way to do it. Keep in mind that this doesn't initialize any of the array to useful values, and that X_SIZE, Y_SIZE, and Z_SIZE are defined else where.

Expand|Select|Wrap|Line Numbers
  1.  
  2. int ***3d_array;
  3. int i, j;
  4.  
  5. 3d_array = (int ***)malloc(sizeof(int **) * X_SIZE);
  6.  
  7. for (i = 0 ;  i < X_SIZE; i++) {
  8.    3d_array[i] = (int **)malloc(sizeof(int *) * Y_SIZE);
  9.  
  10.    for (j = 0; j < Y_YSIZE; j++)
  11.       3d_array[i][j] = (int *)malloc(sizeof(int) * Z_SIZE);
  12. }
  13.  
  14.  
Oct 9 '06 #3
tyreld
144 New Member
The c99 standard also supports dynamic allocations using the standard array allocation scheme. If you have c99 compliant compiler the following should do the same thing simpler.

Expand|Select|Wrap|Line Numbers
  1.  
  2. int 3d_array[X_SIZE][Y_SIZE][Z_SIZE];
  3.  
Oct 9 '06 #4

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

Similar topics

5
10631
by: Nils | last post by:
Hi, I want to create a dynamic array with pointer, without allocation of the memory. I tried it so: objekt **ob= new objekt; It is not working, because he will parameter for the Konstructor...
8
3662
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was...
11
12006
by: D | last post by:
hi, i would like to know how to calculate the size of a dynamic array created using a dereference declaration like int *numbers and allocating via malloc or calloc: numbers=(int...
14
476
by: chai | last post by:
Can anyone help me in finding elements stored in a dynamic array in.
1
4444
by: lemonade | last post by:
Hello! Can someone explain to me the difference between dynamic array of pointers vs dynamic array of objects by giving a real life example. Following is the code that I am using for dynamic...
13
3577
by: lovecreatesbeauty | last post by:
/* How do free() know how many elements should be freed in a dynamic array? When free a single variable, the amount of byte of memory can be retrieved from the type of variable itself. ...
14
8308
by: romayankin | last post by:
Hello All, I'm writing cross-platform code so i'm bound to standards. Here is the code I have: ~~~~~~~~~~~~~~~~~~ double **mx = new double*; for(int i = 0; i < col - 1; i++) { mx = new...
0
2304
by: skm376 | last post by:
I am a little confused as to how I need to allocate memory for a dynamic array inside a struct. Here is the struct that I am using: typedef struct command_t *commandPtr; typedef struct...
6
7969
by: Ananas | last post by:
Hi, My native C++ function creates a dynamic array. I'm marshalling it to managed code and got to delete after. How to make it: c++ code: void CreateArrayInside( pTestStruct &TestStruct,...
0
7132
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
7009
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...
1
6899
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...
0
7390
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...
0
5475
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4919
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...
0
3094
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1427
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 ...
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.