0

I get a SIGSEGV when my code reach a matrix declaration and i do not get much information about it. This is my code :

void mascaraLaPlace(unsigned char Img[1200][1200][3],int Rx,int Ry){
  int x,y,a,b,c,d;
  int valorR,valorG,valorB;
  unsigned char copia[Rx][Ry][3];
  for (y=0;y<Ry;y++){//operations}

The unsigned char copia[Rx][Ry][3] is where the code stops and gives me the SIGSEGV error. I am using Code Blocks on windows witch come with mingw suite.

Youssef Khloufi
  • 685
  • 3
  • 13
  • 24
  • 3
    [This](http://stackoverflow.com/questions/1847789/segmentation-fault-on-large-array-sizes) can be a helpful explanation.... – Recker Nov 03 '13 at 00:25

1 Answers1

4

Depending on the values of Rx and Ry, your array may simply be too large for the stack. If they are both 2000, for example, that's about 12 megabytes, far more than most OSs reserve for the stack by default.

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
  • OK it seems to be the problem because i just check with small values of Rx and Ry and everything works fine. Is there a way i can use the same function with larger values considering that the maximum those two values can be is 1200 each and that the matrix holds values in the range [0,255] ? – Youssef Khloufi Nov 03 '13 at 00:31
  • Look at the question linked in the comment on your question -- the answers there are good. In short --- allocate array on the heap rather than the stack. – Ernest Friedman-Hill Nov 03 '13 at 00:33