c - Pset4(更舒适)- 模糊问题
问题描述
当我收到分段错误错误时,我在编译代码时遇到了麻烦。我相信这与我如何尝试从堆中释放图像并将其重新分配为指向平均值有关。我会认为这会很好,因为图像将再次根据它指向的新地址在 filters.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
提前致谢!
解决方案
推荐阅读
- python-3.x - 为什么这个多项式函数会给出截然不同的结果,具体取决于我绘制的点数?
- azure-log-analytics - 是否可以在 Azure Sentinel 中创建日志源健康警报?
- reactjs - 为什么没有添加 Set-Cookie 标头?
- sql - 每家餐厅的独特客户
- python - 遍历两个数据帧并创建一个字典,一个数据帧是在第二个数据帧(值)中找到的字符串中的子字符串
- stackdriver - 将自定义 jmx 指标添加到谷歌云监控 collectd 配置
- groovy - Groovy replaceAll 替换多个半匹配字符串
- python - 使用 python 搜索谷歌输出
- concurrency - DB2 - 读写锁
- sap-cloud-platform - 如何优化 TMS 上传的大尺寸 mtar?