Я работал над набором задач 4 в CS50, этот код хорошо скомпилирован; однако, как только я запустил его, он полностью сломался и выдал мне следующее сообщение об ошибке:
UndefinedBehaviorSanitizer:DEADLYSIGNAL
==13838==ERROR: UndefinedBehaviorSanitizer: SEGV on unknown address 0x7fae6282690a (pc 0x0000004292c7 bp 0x7fff28e4e760 sp 0x7fff28e49530 T13838)
==13838==The signal is caused by a READ memory access.
#0 0x4292c6 (/home/ubuntu/pset4/filter/filter+0x4292c6)
#1 0x4232b1 (/home/ubuntu/pset4/filter/filter+0x4232b1)
#2 0x7fae61719b96 (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
#3 0x402dd9 (/home/ubuntu/pset4/filter/filter+0x402dd9)
UndefinedBehaviorSanitizer can not provide additional info.
==13838==ABORTING
Во время моей работы с предыдущими фильтрами ошибки не появлялись, и все работало хорошо, так же, как я реализовал этот новый код, это дало мне эту ошибку. Я попытался заменить 0 на 1, чтобы автоматически получить доступ к первому пикселю и т. Д .; однако это не сработало, и я полагаю, что это не правильно; поэтому я возвращаю его к 0. Я был бы благодарен за любую помощь. Большое спасибо.
#include "helpers.h"
#include <math.h>
#include <stdlib.h>
// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
int sumred;
int sumgreen;
int sumblue;
for (int i = 0; i < height; i++)
{
for (int n = 0; n < width / 2; n++)
{
if (i == 0 && n == 0)
{
sumred = ((image[i][n].rgbtRed + image[i - 1][n].rgbtRed + image[i][n + 1].rgbtRed + image[i - 1][n + 1].rgbtRed)/4);
sumgreen = ((image[i][n].rgbtGreen + image[i - 1][n].rgbtGreen + image[i][n + 1].rgbtGreen + image[i - 1][n + 1].rgbtGreen)/4);
sumblue = ((image[i][n].rgbtBlue + image[i - 1][n].rgbtBlue + image[i][n + 1].rgbtBlue + image[i - 1][n + 1].rgbtBlue)/4);
image[i][n].rgbtRed = sumred;
image[i][n].rgbtGreen = sumgreen;
image[i][n].rgbtBlue = sumblue;
}
else if (i == (height - 1) && n == (width -1))
{
sumred = ((image[i][n].rgbtRed + image[i + 1][n].rgbtRed + image[i][n - 1].rgbtRed + image[i + 1][n - 1].rgbtRed)/4);
sumgreen = ((image[i][n].rgbtGreen + image[i - 1][n].rgbtGreen + image[i][n - 1].rgbtGreen + image[i - 1][n - 1].rgbtGreen)/4);
sumblue = ((image[i][n].rgbtBlue + image[i - 1][n].rgbtBlue + image[i][n - 1].rgbtBlue + image[i - 1][n - 1].rgbtBlue)/4);
image[i][n].rgbtRed = sumred;
image[i][n].rgbtGreen = sumgreen;
image[i][n].rgbtBlue = sumblue;
}
else if (i == 0 && n == (width - 1))
{
sumred = ((image[i][n].rgbtRed + image[i - 1][n].rgbtRed + image[i][n - 1].rgbtRed + image[i - 1][n - 1].rgbtRed)/4);
sumgreen = ((image[i][n].rgbtGreen + image[i - 1][n].rgbtGreen + image[i][n - 1].rgbtGreen + image[i - 1][n - 1].rgbtGreen)/4);
sumblue = ((image[i][n].rgbtBlue + image[i - 1][n].rgbtBlue + image[i][n - 1].rgbtBlue + image[i - 1][n - 1].rgbtBlue)/4);
image[i][n].rgbtRed = sumred;
image[i][n].rgbtGreen = sumgreen;
image[i][n].rgbtBlue = sumblue;
}
else if (i == (height - 1) && n == 0)
{
sumred = ((image[i][n].rgbtRed + image[i + 1][n].rgbtRed + image[i][n + 1].rgbtRed + image[i + 1][n + 1].rgbtRed)/4);
sumgreen = ((image[i][n].rgbtGreen + image[i + 1][n].rgbtGreen + image[i][n + 1].rgbtGreen + image[i + 1][n + 1].rgbtGreen)/4);
sumblue = ((image[i][n].rgbtBlue + image[i + 1][n].rgbtBlue + image[i][n + 1].rgbtBlue + image[i + 1][n + 1].rgbtBlue)/4);
image[i][n].rgbtRed = sumred;
image[i][n].rgbtGreen = sumgreen;
image[i][n].rgbtBlue = sumblue;
}
else if (i == 0 && n != 0 && n != (width -1))
{
sumred = ((image[i][n].rgbtRed + image[i - 1][n].rgbtRed + image[i][n + 1].rgbtRed + image[i - 1][n + 1].rgbtRed + image[i - 1][n - 1].rgbtRed + image[i][n - 1].rgbtRed)/6);
sumgreen = ((image[i][n].rgbtGreen + image[i - 1][n].rgbtGreen + image[i][n + 1].rgbtGreen + image[i - 1][n + 1].rgbtGreen + image[i - 1][n - 1].rgbtGreen + image[i][n - 1].rgbtGreen)/6);
sumblue = ((image[i][n].rgbtBlue + image[i - 1][n].rgbtBlue + image[i][n + 1].rgbtBlue + image[i - 1][n + 1].rgbtBlue + image[i - 1][n - 1].rgbtBlue + image[i][n - 1].rgbtBlue)/6);
image[i][n].rgbtRed = sumred;
image[i][n].rgbtGreen = sumgreen;
image[i][n].rgbtBlue = sumblue;
}
else if (i != 0 && i != (height - 1) && n == 0)
{
sumred = ((image[i][n].rgbtRed + image[i - 1][n].rgbtRed + image[i][n + 1].rgbtRed + image[i - 1][n + 1].rgbtRed + image[i + 1][n + 1].rgbtRed + image[i + 1][n].rgbtRed)/6);
sumgreen = ((image[i][n].rgbtGreen + image[i - 1][n].rgbtGreen + image[i][n + 1].rgbtGreen + image[i - 1][n + 1].rgbtGreen + image[i + 1][n + 1].rgbtGreen + image[i + 1][n].rgbtGreen)/6);
sumblue = ((image[i][n].rgbtBlue + image[i - 1][n].rgbtBlue + image[i][n + 1].rgbtBlue + image[i - 1][n + 1].rgbtBlue + image[i + 1][n + 1].rgbtBlue + image[i + 1][n].rgbtBlue)/6);
image[i][n].rgbtRed = sumred;
image[i][n].rgbtGreen = sumgreen;
image[i][n].rgbtBlue = sumblue;
}
else if (i == (height - 1) && n != 0 && n != (width - 1))
{
sumred = ((image[i][n].rgbtRed + image[i + 1][n].rgbtRed + image[i][n + 1].rgbtRed + image[i + 1][n + 1].rgbtRed + image[i][n - 1].rgbtRed + image[i + 1][n - 1].rgbtRed)/6);
sumgreen = ((image[i][n].rgbtGreen + image[i + 1][n].rgbtGreen + image[i][n + 1].rgbtGreen + image[i + 1][n + 1].rgbtGreen + image[i][n - 1].rgbtGreen + image[i + 1][n - 1].rgbtGreen)/6);
sumblue = ((image[i][n].rgbtBlue + image[i + 1][n].rgbtBlue + image[i][n + 1].rgbtBlue + image[i + 1][n + 1].rgbtBlue + image[i][n - 1].rgbtBlue + image[i + 1][n - 1].rgbtBlue)/6);
image[i][n].rgbtRed = sumred;
image[i][n].rgbtGreen = sumgreen;
image[i][n].rgbtBlue = sumblue;
}
else if (i != 0 && i != (height - 1) && n == (width - 1))
{
sumred = ((image[i][n].rgbtRed + image[i - 1][n].rgbtRed + image[i][n - 1].rgbtRed + image[i - 1][n - 1].rgbtRed + image[i + 1][n - 1].rgbtRed + image[i + 1][n].rgbtRed)/6);
sumgreen = ((image[i][n].rgbtGreen + image[i - 1][n].rgbtGreen + image[i][n - 1].rgbtGreen + image[i - 1][n - 1].rgbtGreen + image[i + 1][n - 1].rgbtGreen + image[i + 1][n].rgbtGreen)/6);
sumblue = ((image[i][n].rgbtBlue + image[i - 1][n].rgbtBlue + image[i][n - 1].rgbtBlue + image[i - 1][n - 1].rgbtBlue + image[i + 1][n - 1].rgbtBlue + image[i + 1][n].rgbtBlue)/6);
image[i][n].rgbtRed = sumred;
image[i][n].rgbtGreen = sumgreen;
image[i][n].rgbtBlue = sumblue;
}
else
{
sumred = ((image[i][n].rgbtRed + image[i - 1][n].rgbtRed + image[i][n - 1].rgbtRed + image[i - 1][n - 1].rgbtRed + image[i + 1][n - 1].rgbtRed + image[i + 1][n].rgbtRed + image[i + 1][n + 1].rgbtRed + image[i - 1][n + 1].rgbtRed + image[i][n + 1].rgbtRed)/9);
sumgreen = ((image[i][n].rgbtGreen + image[i - 1][n].rgbtGreen + image[i][n - 1].rgbtGreen + image[i - 1][n - 1].rgbtGreen + image[i + 1][n - 1].rgbtGreen + image[i + 1][n].rgbtGreen + image[i + 1][n + 1].rgbtGreen + image[i - 1][n + 1].rgbtGreen + image[i][n + 1].rgbtGreen)/9);
sumblue = ((image[i][n].rgbtBlue + image[i - 1][n].rgbtBlue + image[i][n - 1].rgbtBlue + image[i - 1][n - 1].rgbtBlue + image[i + 1][n - 1].rgbtBlue + image[i + 1][n].rgbtBlue + image[i + 1][n + 1].rgbtBlue + image[i - 1][n + 1].rgbtBlue + image[i][n + 1].rgbtBlue)/9);
image[i][n].rgbtRed = sumred;
image[i][n].rgbtGreen = sumgreen;
image[i][n].rgbtBlue = sumblue;
}
}
}
return;
}