jeudi 18 mars 2021

Different writing, same thing: one gives segmentation fault, the other doesn't

Is there any reason why this would give segmentation fault:

if(k < 0 || m < 0 || k == height || m == width) {
  original[k][m].rgbtRed = 0;
  original[k][m].rgbtGreen = 0;
  original[k][m].rgbtBlue = 0;
}

as opposed to this?

if(k < 0 || m < 0 || k == height || m == width) {
   original[k][m].rgbtRed = original[k][m].rgbtGreen = original[k][m].rgbtBlue = 0;
}

This is the only difference, and yet former gives segmentation fault while the latter doesn't.

I am using CS50 IDE, if it matters.

I cannot give the full context since the code is hundreds of lines, but essentially this just reiterates over every pixel of a bmp image. This only happens with large images whereas with small images neither returns a segfault.

Aucun commentaire:

Enregistrer un commentaire