问题描述
我正在学习CS50初学者编程课程,并且当前正在使用C。当前主题是“内存”,并解决了一个问题。该问题要求我们使用“盒子模糊”算法对图像进行模糊处理。我在编译源代码时没有错误,但是在可执行文件中出现以下错误:
UndefinedBehaviorSanitizer:DEADLYSIGNAL
==6429==ERROR: UndefinedBehaviorSanitizer: SEGV on unkNown address 0x7fffec49f13a (pc 0x000000428c30
bp 0x7fffec49c670 sp 0x7fffec3ec380 T6429)
==6429==The signal is caused by a WRITE memory access.
#0 0x428c2f (/home/ubuntu/pset4/filter/filter+0x428c2f)
#1 0x4232b1 (/home/ubuntu/pset4/filter/filter+0x4232b1)
我的代码:
// Blur image
void blur(int height,int width,RGBTRIPLE image[height][width])
{
int sum_red = 0;
int sum_green = 0;
int sum_blue = 0;
int counter = 0 ;
//Temporary variable to copy image
RGBTRIPLE temps[height][width] ;
//copying image to temporary variable
for(int i = 0; i < width; i++)
{
for(int j = 0; j < height; j++)
{
temps[i][j].rgbtRed = image[i][j].rgbtRed;
temps[i][j].rgbtGreen = image[i][j].rgbtGreen;
temps[i][j].rgbtBlue = image[i][j].rgbtBlue;
}
}
//Iterating over each pixel
for(int i = 0; i < height; i++)
{
for(int j = 0; j < width; j++)
//Getting the adjacent pixels to current pixel
{
for (int k = i - 1; k < i + 2; k++)
{
for (int l = j - 1 ; l < j + 2 ; L++)
{
//Skipping iteration if neighbouring pixel is outside image boundary
if(l < 0 || k < 0 || l >= height || k >= width)
{
continue;
}
sum_red += temps[k][l].rgbtRed;
sum_green += temps[k][l].rgbtGreen;
sum_blue += temps[k][l].rgbtBlue;
counter ++ ;
}
}
//Getting the average of RGB of each pixel
image[i][j].rgbtRed = (round)((float)sum_red/counter);
image[i][j].rgbtBlue = (round)((float)sum_blue/counter);
image[i][j].rgbtGreen = (round)((float)sum_green/counter);
//Resetting variables to zero for next iteration
sum_red = 0;
sum_green = 0;
sum_blue = 0;
counter = 0;
}
}
return;
}
RGBTRIPLE定义:
#include <stdint.h>
/**
* RGBTRIPLE
*
* This structure describes a color consisting of relative intensities of
* red,green,and blue.
*
* Adapted from http://msdn.microsoft.com/en-us/library/aa922590.aspx.
*/
typedef struct
{
BYTE rgbtBlue;
BYTE rgbtGreen;
BYTE rgbtRed;
} __attribute__((__packed__))
RGBTRIPLE;
感谢您为我得到上述错误的任何帮助。仍在学习过程中,请避免任何代码效率低下(除非它们是造成问题的原因)。 here发表了类似的问题,但解决方案尚不清楚(如果确实存在问题,则不清楚如何减少内存)。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)