Dynamic Memory allocation for 4 Dimensional array

Hi i have to make a function to Dynamically Allocate memory for 4 dimensional array like A[i][j][k][l]
Plz. suggest me the programme for particular problem.
so Easy!

Dynamic Memory allocation!
u need a ponter, and 4
1
2
type *A;
A = new type[i][j][k][l];

type = your data type, (i.e, int, char, ...);

and for delete this allocation:
 
delete [] A;


more information: Dynamic Memory: http://www.cplusplus.com/doc/tutorial/dynamic/
Last edited on
???
you have to allocate and deallocate memory for each dimension by using for loop.
So , you have to use 4 nested for loop. Like
1
2
3
4
5
6
7
8
9
10
11
12
13
int ****arr ;
arr = new ***int[size1];

for( int i = 0 ; i < size1 ; i++ )
{
	arr[i] = new **int[size2] ;
	for( int j = 0 ; j < size2 ; j++ )
	{
		arr[i][j] = new *int[size3] ;
		for( int k = 0 ; k < size3 ; k++ )
			arr[i][j][k] = new int[ size4 ] ;
	}
}
Last edited on
Hi i have to allocate Dynamic memory for 4 Dimensional array like A[i][j][k][l]
Hows --------------------------
@kat what is this for?
@kaveh, your method works for one dimension only, not four.

there are two ways to do this, Dufresne's method or you can

type *A = new type[i*j*k*l];
Another is to use boost if you are able. If this is an exercise for a class, then forget it. check out boost.org if you are able as there are quite a few free tools such as an abstraction for a multi-dimensional array.
I have dynamically allocated the Array but it gives an error that the image size crosses the limits so it can not be build.
plz suggest me how i can manage with this problem.
@Kempo: Why not use boost for a class project?
It is possible that the point of the project is to demonstrate the student's proficiency with dynamically allocating arrays and working with them. Using a higher-level construct such as boost would defeat that purpose.
But the test SHOULD be there for testing the students skill in programming. So if he can move out of the textbook and use more efficient and advanced coding, that should be better.
Yeah, but then why learn arrays at all? And besides, what happens if boost isn't available? If you don't know how to make it work by yourself are screwed. The point of most projects IS to reinvent to wheel so you can gain experience figuring out how to code.
I have to give you a point there hiryuu. (firedraco)
@katyalgju is the problem with loading an array or is it with loading something into the array?
Topic archived. No new replies allowed.