在遇到分段错误错误时,我在编译代码时遇到麻烦。我认为这与我尝试从堆中释放图像并将其重新分配为指向平均值有关。我本来以为会很好,因为图像会根据指向的新地址再次在filter.c中释放。
我进行了debug50调试并完成了代码。我意识到,当我进行重新分配时,图像指向平均,在堆栈函数模糊之后它不会蒸腾。该地址将还原为图像的原始地址。有什么办法解决吗?
这是我的代码:
// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
// Create a of the image array to store the average of the pixel
RGBTRIPLE (*average)[width] = calloc(height, width * sizeof(RGBTRIPLE));
// Iterate through each row
for (int i = 0; i < height; i++)
{
// Iterate through each column
// Also declare the sub row and col that will be determined based on current i,j
for (int j = 0, x = 0, y = 0; j < width; j++)
{
// Declare variable parameters to determine sub for loop width and height
// Count is used to track division after sum is taken from surrounding pixels
float count = 0.0;
int boxh, boxw;
// ====HORIZONTAL COMPONENT====
// Check if current row at the first row. Set x = 0 and box height i + 1
if ((i - 1) < 0)
{
x = i;
boxh = i + 1;
}
// If current row is not at the boundary, set x to 1 row before and box height + 1
else if ((i + 1) == height)
{
x = i - 1;
boxh = i;
}
// If current row is the last row, set x to 1 row before and box height = i
else
{
x = i - 1;
boxh = i + 1;
}
// ====VERTICAL COMPONENT====
// Check if current col at the first col. Set y = 0 and box height j + 1
if ((j - 1) < 0)
{
y = j;
boxw = j + 1;
}
// If current col is not at the boundary, set y to 1 col before and box height + 1
else if ((j + 1) == width)
{
y = j - 1;
boxw = j;
}
// If current col is the last col, set y to 1 col before and box height = j
else
{
y = j - 1;
boxw = j + 1;
}
// Iterate through subset box based on parameters determined above
for (; x <= boxh; x++)
{
for (; y <= boxw; y++)
{
// Take summation of each RBG value and store it in the current pixel on average array
average[i][j].rgbtRed += image[x][y].rgbtRed;
average[i][j].rgbtGreen += image[x][y].rgbtGreen;
average[i][j].rgbtBlue += image[x][y].rgbtBlue;
// Count number of pixels used
count++;
}
}
// After summation is complete, divide sum of each RBG value and divide by count
average[i][j].rgbtRed = round(average[i][j].rgbtRed / count);
average[i][j].rgbtGreen = round(average[i][j].rgbtGreen / count);
average[i][j].rgbtBlue = round(average[i][j].rgbtBlue / count);
}
}
// Free current image array from heap
free(image);
// Set image pointer to average array
image = average;
return;
}
这是我得到的错误:
~/pset4/filter/ $ ./filter -b images/yard.bmp out.bmp
UndefinedBehaviorSanitizer:DEADLYSIGNAL
==661==ERROR: UndefinedBehaviorSanitizer: SEGV on unknown address 0x7fd256615010 (pc 0x7fd255674c3c bp 0x000000000708 sp 0x7ffc319b62e8 T661)
==661==The signal is caused by a READ memory access.
#0 0x7fd255674c3b (/lib/x86_64-linux-gnu/libc.so.6+0x18ec3b)
#1 0x7fd255571993 (/lib/x86_64-linux-gnu/libc.so.6+0x8b993)
#2 0x7fd255565976 (/lib/x86_64-linux-gnu/libc.so.6+0x7f976)
#3 0x423456 (/home/ubuntu/pset4/filter/filter+0x423456)
#4 0x7fd255507b96 (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
#5 0x402dd9 (/home/ubuntu/pset4/filter/filter+0x402dd9)
UndefinedBehaviorSanitizer can not provide additional info.
==661==ABORTING
谢谢!