I am taking a beginner programming course, CS50 and we're currently working with C. Current topic is on Memory and taking up a problem on this. The problem requires us to blur an image using a 'box blur' algorithm. I get no errors compiling the source code but get the following error with the executable:
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)
My code:
// 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 Definition:
#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;
Would appreciate any assistance offered as to why I get the error above. Pardon any code inefficiencies (unless they are the problem cause), still in the learning process. A similar question was posted here but the solution was unclear (unclear as to how they reduced memory, if this is indeed the problem).