Thursday, July 9, 2009

C multidimensional array memory limit?

Hi,


I need to build a 4-dimensional array in C representing N^3


(about 6000) particles equally spaced in all 3 dimensions. So basically, I have a grid of N^3 particles, N for each dimension. I would need the following scheme for my array:


P[X][Y][Z][0 or 1]


X, Y, Z : position of particle in 3-dim space


0: mass of the particle


1: potential at particle location





The problem is that as soon as I start initializing the array I get a weird "Bus" error. If I lower the number of particles, the problem is solved. Is there any memory limit on a multidimensional array ?





Would you have any idea of what is going on and how I could fix this problem ?





Here is the array initialization:





const int N=20;


int initgrid(double grid_array[N-1][N-1][N-1][1]) {


int i,j,k;


for (i=0;i%26lt;N;i++) { // x position


for (j=0;j%26lt;N;j++) {


for (k=0;k%26lt;N;k++) {


grid_array[i][j][k][0]=0; //mass


grid_array[i][j][k][1]=0; //potential


}





}


}


}





Thanks !

C multidimensional array memory limit?
The code above doesn't have an array definition. It should be something similar to:





char myArray[N-1][N-1][N-1][2];





You can then use myArray[1][2][3][0]=whatever.





I'm using char as the datatype because you show that the value is either 0 or 1, although I may be not understanding what you need. This should work without problems.





If it doesn't, then you're going to have to allocate memory for the array dynamically as the program runs rather than using the compiler memory model.
Reply:It is most probably because you are


trying to pass the array to the initgrid function


as an argument but the wrong way. the bus


error is surely happening when you first access


the array within the function, and it tries to


access a bad memory adress at that moment.


There are two ways to pass arguments,


as pointers or as values, double check


your code that calls the function and the


way you receive the data as the parameter.


If you expect a value, pass a value, not a pointer.


No comments:

Post a Comment