I’m having trouble passing a big array to a function in C.
I declare:
int image[height][width][3]={};
where height and width can be as big as 1500. And when I call:
foo((void *)image,height,width);
which is declared as follows:
int *foo(const int *inputImage, int h, int w);
I get segmentation fault error. What’s strange is that if my values are:
height=1200;
width=290;
theres no problem, but when they’re:
height=1200;
width=291;
i get the mentioned error. At 4 bytes per integer with both height and width of 1500 (absolute worst case) the array size would be of 27MB which imo isn’t that big and shouldn’t really matter because I’m only passing a pointer to the first element of the array. Any advice?
At 27 MB, your array is probably bigger than the stack, which is usually 1 MB. You are corrupting your process as soon as
foostarts working with the data. Allocate the array on the heap instead: