Dynamic Memory allocation for 4 Dimensional array

Jun 23, 2009 at 5:55am
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.
Jun 23, 2009 at 6:08am
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 Jun 23, 2009 at 6:08am
Jun 23, 2009 at 6:45am
???
Jun 23, 2009 at 8:02am
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 Jun 23, 2009 at 11:07am
Jun 25, 2009 at 1:43pm
Hi i have to allocate Dynamic memory for 4 Dimensional array like A[i][j][k][l]
Hows --------------------------
Jun 25, 2009 at 8:43pm
@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];
Jun 25, 2009 at 10:41pm
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.
Jun 30, 2009 at 6:38am
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.
Jun 30, 2009 at 9:10am
@Kempo: Why not use boost for a class project?
Jun 30, 2009 at 12:29pm
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.
Jun 30, 2009 at 7:03pm
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.
Jun 30, 2009 at 10:50pm
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.
Jul 1, 2009 at 5:10am
I have to give you a point there hiryuu. (firedraco)
Jul 1, 2009 at 7:33pm
@katyalgju is the problem with loading an array or is it with loading something into the array?
Topic archived. No new replies allowed.