CS50 过滤器:我收到一个我不熟悉的新错误
CS50 filter: I'm getting a new error which I'm not familiar with
我已经研究 cs50 的边缘过滤功能几个小时了。它看起来不错,但我收到了一个看起来很可怕的错误,我不明白这是我的代码:
// Detect edges
void edges(int height, int width, RGBTRIPLE image[height][width])
{
RGBTRIPLE copy[height][width];
for (int h = 0; h < height; h++)
{
for (int w = 0; w < width; w++)
{
copy[h][w] = image[h][w];
}
}
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
float gxred = 0;
float gxgreen = 0;
float gxblue = 0;
float gyred = 0;
float gygreen = 0;
float gyblue = 0;
for (int r = -1; r < 2; r++)
{
for (int s = -1; s < 2; s++)
{
if ((y + r < height && y + r >= 0) && (x + s < width && x + s >= 0))
{
if (r == -1)
{
gxred = copy[y + r][x + s].rgbtRed * s;
gxgreen = copy[y + r][x + s].rgbtGreen * s;
gxblue = copy[y + r][x + s].rgbtBlue * s;
gyred = copy[x + s][y + r].rgbtRed * s;
gygreen = copy[x + s][y + r].rgbtGreen * s;
gyblue = copy[x + s][y + r].rgbtBlue * s;
}
if (r == 0)
{
gxred = copy[y + r][x + s].rgbtRed * (s * 2);
gxgreen = copy[y + r][x + s].rgbtGreen * (s * 2);
gxblue = copy[y + r][x + s].rgbtBlue * (s * 2);
gyred = copy[x + s][y + r].rgbtRed * (s * 2);
gygreen = copy[x + s][y + r].rgbtGreen * (s * 2);
gyblue = copy[x + s][y + r].rgbtBlue * (s * 2);
}
if (r == 1)
{
if (s * s == 1)
{
gxred = copy[y + r][x + s].rgbtRed * (s * s);
gxgreen = copy[y + r][x + s].rgbtGreen * (s * s);
gxblue = copy[y + r][x + s].rgbtBlue * (s * s);
gyred = copy[x + s][y + r].rgbtRed * (s * s);
gygreen = copy[x + s][y + r].rgbtGreen * (s * s);
gyblue = copy[x + s][y + r].rgbtBlue * (s * s);
}
else
{
gxred = copy[y + r][x + s].rgbtRed * (s + 1);
gxgreen = copy[y + r][x + s].rgbtGreen * (s + 1);
gxblue = copy[y + r][x + s].rgbtBlue * (s + 1);
gyred = copy[x + s][y + r].rgbtRed * (s + 1);
gygreen = copy[x + s][y + r].rgbtGreen * (s + 1);
gyblue = copy[x + s][y + r].rgbtBlue * (s + 1);
}
}
}
}
}
image[y][x].rgbtRed = round(sqrt((gxred * gxred) + (gyred * gyred)));
image[y][x].rgbtGreen = round(sqrt((gxgreen * gxgreen) + (gygreen * gygreen)));
image[y][x].rgbtBlue = round(sqrt((gxblue * gxblue) + (gyblue * gyblue)));
image[y][x].rgbtRed = image[y][x].rgbtRed % 256;
image[y][x].rgbtGreen = image[y][x].rgbtGreen % 256;
image[y][x].rgbtBlue = image[y][x].rgbtBlue % 256;
}
}
return;
}
这是我的错误
helpers.c:169:37: runtime error: 261 is outside the range of representable values of type
'unsigned char'
UndefinedBehaviorSanitizer:DEADLYSIGNAL
==5405==ERROR: UndefinedBehaviorSanitizer: SEGV on unknown address 0x7ffea18b01fa (pc
0x000000429bb3 bp 0x7ffea18af1a0 sp 0x7ffea17fe250 T5405)
==5405==The signal is caused by a READ memory access.
#0 0x429bb2 (/home/ubuntu/pset4/filter/filter+0x429bb2)
#1 0x42330e (/home/ubuntu/pset4/filter/filter+0x42330e)
#2 0x7f7c0b22eb96 (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
#3 0x402e19 (/home/ubuntu/pset4/filter/filter+0x402e19)
UndefinedBehaviorSanitizer can not provide additional info.
==5405==ABORTING
这会将我带到最后这一行:
image[y][x].rgbtGreen = round(sqrt((gxgreen * gxgreen) + (gygreen * gygreen)));
如果有人能解释这种错误的含义,我将不胜感激。而且,如果对我的代码提出任何批评,我将不胜感激,我正在努力做到这一点。
RGB 值可以是 0 到 255 之间的任何值。
您以某种方式将该行中的 RGB 值分配给了超出范围的 261。
在分配给 rgb 值之前,我会在下面的代码中打印值。
round(sqrt((gxgreen * gxgreen) + (gygreen * gygreen)))
然后检查为什么 round
内部给出 261。一旦你解决了这个问题,你就可以开始了。
我已经研究 cs50 的边缘过滤功能几个小时了。它看起来不错,但我收到了一个看起来很可怕的错误,我不明白这是我的代码:
// Detect edges
void edges(int height, int width, RGBTRIPLE image[height][width])
{
RGBTRIPLE copy[height][width];
for (int h = 0; h < height; h++)
{
for (int w = 0; w < width; w++)
{
copy[h][w] = image[h][w];
}
}
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
float gxred = 0;
float gxgreen = 0;
float gxblue = 0;
float gyred = 0;
float gygreen = 0;
float gyblue = 0;
for (int r = -1; r < 2; r++)
{
for (int s = -1; s < 2; s++)
{
if ((y + r < height && y + r >= 0) && (x + s < width && x + s >= 0))
{
if (r == -1)
{
gxred = copy[y + r][x + s].rgbtRed * s;
gxgreen = copy[y + r][x + s].rgbtGreen * s;
gxblue = copy[y + r][x + s].rgbtBlue * s;
gyred = copy[x + s][y + r].rgbtRed * s;
gygreen = copy[x + s][y + r].rgbtGreen * s;
gyblue = copy[x + s][y + r].rgbtBlue * s;
}
if (r == 0)
{
gxred = copy[y + r][x + s].rgbtRed * (s * 2);
gxgreen = copy[y + r][x + s].rgbtGreen * (s * 2);
gxblue = copy[y + r][x + s].rgbtBlue * (s * 2);
gyred = copy[x + s][y + r].rgbtRed * (s * 2);
gygreen = copy[x + s][y + r].rgbtGreen * (s * 2);
gyblue = copy[x + s][y + r].rgbtBlue * (s * 2);
}
if (r == 1)
{
if (s * s == 1)
{
gxred = copy[y + r][x + s].rgbtRed * (s * s);
gxgreen = copy[y + r][x + s].rgbtGreen * (s * s);
gxblue = copy[y + r][x + s].rgbtBlue * (s * s);
gyred = copy[x + s][y + r].rgbtRed * (s * s);
gygreen = copy[x + s][y + r].rgbtGreen * (s * s);
gyblue = copy[x + s][y + r].rgbtBlue * (s * s);
}
else
{
gxred = copy[y + r][x + s].rgbtRed * (s + 1);
gxgreen = copy[y + r][x + s].rgbtGreen * (s + 1);
gxblue = copy[y + r][x + s].rgbtBlue * (s + 1);
gyred = copy[x + s][y + r].rgbtRed * (s + 1);
gygreen = copy[x + s][y + r].rgbtGreen * (s + 1);
gyblue = copy[x + s][y + r].rgbtBlue * (s + 1);
}
}
}
}
}
image[y][x].rgbtRed = round(sqrt((gxred * gxred) + (gyred * gyred)));
image[y][x].rgbtGreen = round(sqrt((gxgreen * gxgreen) + (gygreen * gygreen)));
image[y][x].rgbtBlue = round(sqrt((gxblue * gxblue) + (gyblue * gyblue)));
image[y][x].rgbtRed = image[y][x].rgbtRed % 256;
image[y][x].rgbtGreen = image[y][x].rgbtGreen % 256;
image[y][x].rgbtBlue = image[y][x].rgbtBlue % 256;
}
}
return;
}
这是我的错误
helpers.c:169:37: runtime error: 261 is outside the range of representable values of type
'unsigned char'
UndefinedBehaviorSanitizer:DEADLYSIGNAL
==5405==ERROR: UndefinedBehaviorSanitizer: SEGV on unknown address 0x7ffea18b01fa (pc
0x000000429bb3 bp 0x7ffea18af1a0 sp 0x7ffea17fe250 T5405)
==5405==The signal is caused by a READ memory access.
#0 0x429bb2 (/home/ubuntu/pset4/filter/filter+0x429bb2)
#1 0x42330e (/home/ubuntu/pset4/filter/filter+0x42330e)
#2 0x7f7c0b22eb96 (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
#3 0x402e19 (/home/ubuntu/pset4/filter/filter+0x402e19)
UndefinedBehaviorSanitizer can not provide additional info.
==5405==ABORTING
这会将我带到最后这一行:
image[y][x].rgbtGreen = round(sqrt((gxgreen * gxgreen) + (gygreen * gygreen)));
如果有人能解释这种错误的含义,我将不胜感激。而且,如果对我的代码提出任何批评,我将不胜感激,我正在努力做到这一点。
RGB 值可以是 0 到 255 之间的任何值。
您以某种方式将该行中的 RGB 值分配给了超出范围的 261。
在分配给 rgb 值之前,我会在下面的代码中打印值。
round(sqrt((gxgreen * gxgreen) + (gygreen * gygreen)))
然后检查为什么 round
内部给出 261。一旦你解决了这个问题,你就可以开始了。