简体   繁体   English

C-浮点异常(核心已转储)

[英]C - Floating Point Exception (core dumped)

This function is supposed to, pixel by pixel, blur the image by turning the color of each pixel on the mean of the colors around it in a 2n+1 "radius". 该功能应该通过将每个像素的颜色按2n + 1个“半径”的平均值转换为每个像素的颜色来使图像模糊。

(The part where it skips to the next pixel is already implemented, don't worry). (不必担心跳到下一个像素的部分)。

I successfully compiled this code: 我成功地编译了这段代码:

void
blur_pixels(image *img, pixel *p, size_t i, size_t j)
{
  //i = current height of pixel, j = current width of pixel
  int side = 2*blurRate+1;
  int total = 0;
  int leftRight = i-blurRate;
  int upDown = j-blurRate;
  int tmpHr = 0, tmpHg = 0, tmpHb = 0;

  for(; upDown < j+blurRate; upDown++) {
    if(upDown >= 0 && upDown < img->height) {
      for(; leftRight < i+blurRate; leftRight++) {
        if(leftRight >= 0 && leftRight < img->width) {
          tmpHr += (p+leftRight)->r;
          tmpHg += (p+leftRight)->g;
          tmpHb += (p+leftRight)->b;
          total++;
        }
      }
    }
  }
  p->r=tmpHr/total;
  p->g=tmpHg/total;
  p->b=tmpHb/total;
}

But when I run the code I get the following exception: 但是,当我运行代码时,出现以下异常:

Floating point exception

Does anyone know why? 有人知道为什么吗?

Code is preforming division by 0 with p->r=tmpHr/total; 代码用p->r=tmpHr/total;进行0 p->r=tmpHr/total;

total is likely zero because compiler warnings are not turned on showing the mixed signed/unsigned math of the for() loop. total可能为零,因为未打开编译器警告以显示for()循环的混合有符号/无符号数学。 Turn on all compiler warnings. 打开所有编译器警告。

The compare upDown < j+blurRate and other code is done using unsigned math, likely not as OP expects and the inner total++; 比较upDown < j+blurRate和其他代码是使用无符号数学完成的,可能不像OP期望的那样,也不是内部total++; never happens. 永远不会发生。 If upDown < 0 , then upDown in upDown < j+blurRate becomes a large unsigned value. 如果upDown < 0 ,则upDown upDown < j+blurRate变为一个大的无符号值。 Then compare is false. 然后比较是假的。

size_t j  // an unsigned type
...
int upDown = j-blurRate;
...
for(; upDown < j+blurRate; upDown++) {  // not firing

One solution would be to use only int variables. 一种解决方案是仅使用int变量。 A more robust solution would use unsigned math, yet more of the higher level code would be needed for a good answer. 一个更健壮的解决方案将使用无符号数学,但要获得一个好的答案,还需要更多的高级代码。

Something like: 就像是:

blur_pixels(image *img, pixel *p, size_t i, size_t j) {
  //i = current height of pixel, j = current width of pixel
  size_t side = 2u*blurRate+1;
  size_t total = 0;
  size_t leftRight = (i > blurRate) ? i-blurRate : 0;
  size_t upDown = (j > blurRate) ? j-blurRate : 0;
  int tmpHr = 0, tmpHg = 0, tmpHb = 0;

  for(; upDown < j+blurRate; upDown++) {
    if (upDown < img->height) {
      // I suspect leftRight needs to be set here each iteration
      size_t leftRight = (i > blurRate) ? i-blurRate : 0;
      for(; leftRight < i+blurRate; leftRight++) {
        if (leftRight < img->width) {
          tmpHr += (p+leftRight)->r;
          tmpHg += (p+leftRight)->g;
          tmpHb += (p+leftRight)->b;
          total++;
        }
      }
    }
  }
  if (total) {
    p->r = tmpHr/total;
    p->g = tmpHg/total;
    p->b = tmpHb/total;
  } else {
    p->r = p->g = p->b = 0;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM