微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SEGV在未知地址上-查找错误原因

如何解决SEGV在未知地址上-查找错误原因

我正在学习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 举报,一经查实,本站将立刻删除。