Reputation: 155
I am writing a program for my research that requires a non-trivial indexing scheme to deal with a spin ice system. To help with the indexing I use a combination of structs and arrays. Every struct holds the 16 points contained within a cubic cell [I tried to post a picture of the cubic cell but stackoverflow said I needed at least 10 reputation points to do so, my apologies], however for numerical reasons later these need to be store in one matrix.
The values that determine the size of the system (i.e. how big the simulation cube is) work fine for L=1, L=2, L=3. However when I try L=4 I have a seg fault. The relevant part of the code is as follows:
/* The indexing of this program is as
* (i,j,k) refers to which cubic cell you are in
* (l,m) refers to which particle in the cubic cell you are in
* the order is as follows
* (l,m) = (1,1) (1,3) (3,1) (3,3)
* (1,0) (1,2) (3,0) (3,2)
*
* (0,1) (0,3) (2,1) (2,3)
* (0,0) (0,2) (2,0) (2,2)
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define FOR_IJKLM for (i=0; i<L; i++) \
for (j=0; j<L; j++) \
for (k=0; k<L; k++) \
for (l=0; l<4; l++) \
for (m=0; m<4; m++)
// L := integer length of convential cubic cell
// Np := Number of total particles in the system
#define L 4
#define Np 16*L*L*L
struct ConventialCube{
double p[4][4][3]; // Position of particle
double mu[4][4][3]; // Magnetic Moment of particle
};
void initialize(struct ConventialCube cc[][L][L]);
int main(void){
struct ConventialCube cc[L][L][L];
initialize(cc);
double ewaldMatrix[Np][Np];
return 0;
}
void initialize(struct ConventialCube cc[][L][L]){
int i, j, k, l, m, d;
double s = 1.0/sqrt(3);
double sv[4][3] = {
{-s,-s,-s},
{-s, s, s},
{ s,-s, s},
{ s, s,-s}
};
double O[4][3] = {
{0.0, 0.0, 0.0},
{0.0, 0.5, 0.5},
{0.5, 0.0, 0.5},
{0.5, 0.5, 0.0}
};
FOR_IJKLM{
double CO[] = {i,j,k};
for (d=0; d<3; d++){
cc[i][j][k].mu[l][m][d] = sv[m][d];
cc[i][j][k].p[l][m][d] = CO[d] + O[l][d] + O[m][d]/2.0;
}
}
}
As mentioned before the code runs for L=1, L=2, L=3, however at L=4 it breaks. Some peculiarities I have found are the following:
I would greatly appreciate any input or advice as the L=4 case is absolutely necessary (I don't actually need anything higher than L=4 just L=4 - Murphy's Law I guess).
Upvotes: 2
Views: 63
Reputation: 81936
You're overflowing the stack. Declare your two arrays as static:
int main(void){
static struct ConventialCube cc[L][L][L];
initialize(cc);
static double ewaldMatrix[Np][Np];
return 0;
}
or as globals:
static struct ConventialCube cc[L][L][L];
static double ewaldMatrix[Np][Np];
int main(void){
initialize(cc);
return 0;
}
Alternatively, you could also declare these objects on the heap using malloc()
.
Upvotes: 2