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

I want to allocate memory for a 3D array...

momotaro
357 100+
I want to allocate memoru for a 3D array...and this is my code:

Expand|Select|Wrap|Line Numbers
  1. int*** build_3D_arr(int n, int ***arr){
  2. arr = (int ***) calloc (sizeof(int**));
  3. for(int i = 0; i < n; i++)
  4. for(int j = 0; j < n; j++)
  5.    arr[i][j] = (int*)calloc(sizeof(int));
  6. }
Jun 26 '07 #1
6 6131
hi,
If you want to allocate for a 3D array of size n x n x n then every time you allocate you need to allocate for n number of intergers, that is the parameter for caloc function should be like, n*sizeof(int).

Your first allocation is OK, just chage the parameter as n*sizeof(int). But inside the first for loop you need to allocate one more time like,
arr[i]= (int**) calloc(n*sizeof(int));

And also, chage the parameter of the third allcation as n*sizeof(int). Thats it.

To summarize,
first you need to allocate for ***arr to hold n number of **arr[i],
then for each of them you need to allocate to hold n number of *arr[i][j]
and only then you can allocate for each of them to hold n number of a[i][j][k], that is integers.

Hope it helps :) and you probably should be able t write yourself now.

Thanks,
Sorower
Jun 26 '07 #2
rsteph
71
Sorry that this is a little off topic from your post, but I noticed in your code that you have instances with three *'s by the variable names, and type declarations. Can I ask why you use 3 *'s instead of just 1? I'm just getting back into programming, and into C++ programming, and that just struck me as something I hadn't seen before. Again sorry to get off topic for a moment.
Jun 26 '07 #3
Silent1Mezzo
208 100+
Sorry that this is a little off topic from your post, but I noticed in your code that you have instances with three *'s by the variable names, and type declarations. Can I ask why you use 3 *'s instead of just 1? I'm just getting back into programming, and into C++ programming, and that just struck me as something I hadn't seen before. Again sorry to get off topic for a moment.
int* var; <---a pointer to an int
int*** var; <---a pointer to a pointer to a pointer of an int

Kind of like var[] vs var[][][]
Jun 26 '07 #4
rsteph
71
Ahh thank you. I'm still a little hazy on the advantages of that, but I can see what you're getting at as far as how it works. Thanks for the information.
Jun 26 '07 #5
Silent1Mezzo
208 100+
Ahh thank you. I'm still a little hazy on the advantages of that, but I can see what you're getting at as far as how it works. Thanks for the information.
The main advantage is that with arrays you need to know the number of elements when you declare them but with pointers you can just allocate more memory and dynamically grow (or shrink) them.
Jun 26 '07 #6
weaknessforcats
9,208 Expert Mod 8TB
Everyone is making this too hard. There are no 3D arrays in C or C++. All there are are one dimensional arrays. The first index specifies the number of elements:
Expand|Select|Wrap|Line Numbers
  1. int arr[3];       //has 3 elements. Each element an int
  2. int arr[3][7];   //has 3 elements. Each element an array of 7 int
  3. int arr[3][4][5];  //has 3 elements. Each element an array 4 arrays of 5 ints.
  4.  
Further, arrays must be contiguous in memory. Element 0 then element 1, etc.

So you allocate int arr[3][4[5] as:

Expand|Select|Wrap|Line Numbers
  1. int* arr = malloc(3 * 4 * 5 * sizeof(int));
  2.  
You now have 60 ints. If you want to use this as a 3D array then tou need to cast the return from malloc to the correct pointer. In this case, a pointer to a 4x5 array of int:

Expand|Select|Wrap|Line Numbers
  1. int (* arr)[4][5] =(int (*)[4][5] )malloc(3 * 4 * 5 * sizeof(int));
  2.  
Remember, the name of an array is always the address of element 0. In this case the elements are 4x5 arrays. Using an int*** is a pointer to a pointer to a pointer to a single int. Trying to use this as a 3D array will produce the wrong address calculations.
Jun 26 '07 #7

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

Similar topics

1
by: OlgaM | last post by:
Hello, i'm trying to initialize an array. The class List contains this in its private data members: ListNode<DATATYPE> *dataItems; In the constructor, i'm trying to allocate space for...
6
by: bluekite2000 | last post by:
I was able to allocate a single array having 2.6GB (ie. w/ 700200000 elements of type float) yet when I did a cpuinfo on my computer, it showed a cache size of 256KB and 1GB of memory. What...
12
by: gc | last post by:
I am writing a function that given nx1 vector a and a nx1 b solves a system of equations f(a,c)=b for a nx1 c. While writing the function: 1] Should I allocate the memory for c within the...
8
by: M. Moennigmann | last post by:
Dear all: I would like to write a function that opens a file, reads and stores data into an 2d array, and passes that array back to the caller (=main). The size of the array is not known before...
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 ;
2
by: xhunga | last post by:
I have try a new version of my work. I have put the sizes of the matrix into the matrix. A = number of rows A = number of columns The first element of the matrix is A instead of A. You...
1
by: trevor.farchild | last post by:
Hi, long time reader, first time poster I have an application that will be doing this 5 times a second: Get a bunch of data from a NetworkStream and convert it into a Bitmap Therefore, the...
7
by: emre esirik(hacettepe computer science and enginee | last post by:
I used a structer in this program. typedef struct _guitar { int serial; :serial of item at the guitar store int price; :price of item at the guitar...
1
by: parvtb | last post by:
I know STL vector works. But in case STL is not available, what can one do to allocate large memory size in c++ through operator "new"? For instance, I am writing a sort algorithm, and here's...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
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...

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.