I keep getting a segmentation with the following code. Changing the 4000 to 1000 makes the code run fine. I would think that I have enough memory here… How can I fix this?
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#define MAXLEN 4000
void initialize_mx(float mx[][MAXLEN])
{
int i, j;
float c=0;
for(i=0;i<MAXLEN;i++){
for(j=0;j<MAXLEN;j++) mx[i][j]=c;
}
}
int main(int ac, char *av[])
{
int i, j;
float confmx[MAXLEN][MAXLEN];
initialize_mx(confmx);
return 0;
}
The problem is you’re overflowing the stack.
When you call
initialize_mx()it allocates stack space for it’s local variables (confmx in your case). This space, which is limited by your OS (check ulimit if you’re on linux), can get overflowed if local variables are too big.Basically you can:
initialize_mx()EDIT: Just realized you must still allocate memory space if you pass a pointer so you have those two options 🙂