Lou
Lou

Reputation: 103

CS50 filter: I'm getting a new error which I'm not familiar with

I've been working on the edge filter function for cs50 for a few hours now. It looks fine to me but I'm receiving a pretty scary looking error which I don't understand here is my code:

// Detect edges
void edges(int height, int width, RGBTRIPLE image[height][width])
{
RGBTRIPLE copy[height][width];

for (int h = 0; h < height; h++)
{
    for (int w = 0; w < width; w++)
    {
        copy[h][w] = image[h][w];
    }
}

for (int y = 0; y < height; y++)
{
    for (int x = 0; x < width; x++)
    {
        float gxred = 0;
        float gxgreen = 0;
        float gxblue = 0;
        float gyred = 0;
        float gygreen = 0;
        float gyblue = 0;
        for (int r = -1; r < 2; r++)
        {
            for (int s = -1; s < 2; s++)
            {
                if ((y + r < height && y + r >= 0) && (x + s < width && x + s >= 0))
                {
                    if (r == -1)
                    {
                        gxred = copy[y + r][x + s].rgbtRed * s;
                        gxgreen = copy[y + r][x + s].rgbtGreen * s;
                        gxblue = copy[y + r][x + s].rgbtBlue * s;
                        gyred = copy[x + s][y + r].rgbtRed * s;
                        gygreen = copy[x + s][y + r].rgbtGreen * s;
                        gyblue = copy[x + s][y + r].rgbtBlue * s;
                    }
                    if (r == 0)
                    {
                        gxred = copy[y + r][x + s].rgbtRed * (s * 2);
                        gxgreen = copy[y + r][x + s].rgbtGreen * (s * 2);
                        gxblue = copy[y + r][x + s].rgbtBlue * (s * 2);
                        gyred = copy[x + s][y + r].rgbtRed * (s * 2);
                        gygreen = copy[x + s][y + r].rgbtGreen * (s * 2);
                        gyblue = copy[x + s][y + r].rgbtBlue * (s * 2);
                    }
                    if (r == 1)
                    {
                        if (s * s == 1)
                        {
                            gxred = copy[y + r][x + s].rgbtRed * (s * s);
                            gxgreen = copy[y + r][x + s].rgbtGreen * (s * s);
                            gxblue = copy[y + r][x + s].rgbtBlue * (s * s);
                            gyred = copy[x + s][y + r].rgbtRed * (s * s);
                            gygreen = copy[x + s][y + r].rgbtGreen * (s * s);
                            gyblue = copy[x + s][y + r].rgbtBlue * (s * s);
                        }
                        else
                        {
                            gxred = copy[y + r][x + s].rgbtRed * (s + 1);
                            gxgreen = copy[y + r][x + s].rgbtGreen * (s + 1);
                            gxblue = copy[y + r][x + s].rgbtBlue * (s + 1);
                            gyred = copy[x + s][y + r].rgbtRed * (s + 1);
                            gygreen = copy[x + s][y + r].rgbtGreen * (s + 1);
                            gyblue = copy[x + s][y + r].rgbtBlue * (s + 1);    
                        }

                    }

                }
            }
        }
        image[y][x].rgbtRed = round(sqrt((gxred * gxred) + (gyred * gyred)));
        image[y][x].rgbtGreen = round(sqrt((gxgreen * gxgreen) + (gygreen * gygreen)));
        image[y][x].rgbtBlue = round(sqrt((gxblue * gxblue) + (gyblue * gyblue)));

        image[y][x].rgbtRed = image[y][x].rgbtRed % 256;
        image[y][x].rgbtGreen = image[y][x].rgbtGreen % 256;
        image[y][x].rgbtBlue = image[y][x].rgbtBlue % 256;
    }
}
return;
}

And here is my error

helpers.c:169:37: runtime error: 261 is outside the range of representable values of type 
'unsigned char'
UndefinedBehaviorSanitizer:DEADLYSIGNAL
==5405==ERROR: UndefinedBehaviorSanitizer: SEGV on unknown address 0x7ffea18b01fa (pc 
0x000000429bb3 bp 0x7ffea18af1a0 sp 0x7ffea17fe250 T5405)
==5405==The signal is caused by a READ memory access.
#0 0x429bb2  (/home/ubuntu/pset4/filter/filter+0x429bb2)
#1 0x42330e  (/home/ubuntu/pset4/filter/filter+0x42330e)
#2 0x7f7c0b22eb96  (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
#3 0x402e19  (/home/ubuntu/pset4/filter/filter+0x402e19)

UndefinedBehaviorSanitizer can not provide additional info.
==5405==ABORTING

This directs me to this line at the end:

image[y][x].rgbtGreen = round(sqrt((gxgreen * gxgreen) + (gygreen * gygreen)));

I'd be really grateful if someone could explain what this kind of error means. And also, any critique on my code would be much appreciated, I'm trying to get good at this.

Upvotes: 1

Views: 469

Answers (1)

Enis Arik
Enis Arik

Reputation: 677

RGB values can be any value between 0 and 255.

You are somehow assigning your RGB value in that line to 261 which is outside of the range.

I would print the value in the following code before assigning to rgb value.

round(sqrt((gxgreen * gxgreen) + (gygreen * gygreen)))

Then check why inside of round gives 261. Once you fix this you are good to go.

Upvotes: 1

Related Questions