473,395 Members | 1,756 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,395 software developers and data experts.

dynamic 3D array memory alllocation

can u plz giv a code in c for allocating a memory for 3D array.
Oct 8 '06 #1
3 14813
tyreld
144 100+
[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 100+
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 100+
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
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
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
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
by: chai | last post by:
Can anyone help me in finding elements stored in a dynamic array in.
1
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
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
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
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
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
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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.