0

The gray, blur and reflection filter went well and can product correct images. For the "edge" filter, it passed check50, but when I tried to input image and run it, it shows below:

UndefinedBehaviorSanitizer:DEADLYSIGNAL
==15033==ERROR: UndefinedBehaviorSanitizer: SEGV on unknown address 0x7ffc06de18f0 (pc 0x00000044797b bp 0x7ffc0762d280 sp 0x7ffc06b226f0 T15033)
==15033==The signal is caused by a WRITE memory access.
    #0 0x44797a  (/home/ubuntu/pset4/filter/filter+0x44797a)
    #1 0x4232ce  (/home/ubuntu/pset4/filter/filter+0x4232ce)
    #2 0x7f57c4738b96  (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
    #3 0x402dd9  (/home/ubuntu/pset4/filter/filter+0x402dd9)

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

Not sure what this means, can someone give me any suggestions? Thanks a lot! :)

Here's my code:

void edges(int height, int width, RGBTRIPLE image[height][width])
{
    RGBTRIPLE2 image2[height+2][width+2], image3[height+2][width+2], Gx[height][width], Gy[height][width];
    for (int i=0; i<height+2; i++)
    {
        for (int j=0; j<width+2; j++)
        {
            if (i==0 || i==height+1 || j==0 || j==width+1)
            {
                image2[i][j].rgbtBlue=0;
                image2[i][j].rgbtRed=0;
                image2[i][j].rgbtGreen=0;
            } else
            {
                image2[i][j].rgbtBlue=image[i-1][j-1].rgbtBlue;
                image2[i][j].rgbtRed=image[i-1][j-1].rgbtRed;
                image2[i][j].rgbtGreen=image[i-1][j-1].rgbtGreen;
            }
            image3[i][j]=image2[i][j];
        }
    }

//it seems like something wrong with this paragraph, when I delete this paragraph the terminal won't show error.
    for (int i=1; i<height+1; i++)
    {
        for (int j=1; j<width+1; j++)
        {
            Gx[i-1][j-1].rgbtBlue=-image2[i-1][j-1].rgbtBlue+image2[i-1][j+1].rgbtBlue-2*image2[i][j-1].rgbtBlue+2*image2[i][j+1].rgbtBlue-image2[i+1][j-1].rgbtBlue+image2[i+1][j+1].rgbtBlue;
            Gx[i-1][j-1].rgbtRed=-image2[i-1][j-1].rgbtRed+image2[i-1][j+1].rgbtRed-2*image2[i][j-1].rgbtRed+2*image2[i][j+1].rgbtRed-image2[i+1][j-1].rgbtRed+image2[i+1][j+1].rgbtRed;
            Gx[i-1][j-1].rgbtGreen=-image2[i-1][j-1].rgbtGreen+image2[i-1][j+1].rgbtGreen-2*image2[i][j-1].rgbtGreen+2*image2[i][j+1].rgbtGreen-image2[i+1][j-1].rgbtGreen+image2[i+1][j+1].rgbtGreen;

            Gy[i-1][j-1].rgbtBlue=-image2[i-1][j-1].rgbtBlue-image2[i-1][j+1].rgbtBlue-2*image2[i-1][j].rgbtBlue+2*image2[i+1][j].rgbtBlue+image2[i+1][j-1].rgbtBlue+image2[i+1][j+1].rgbtBlue;
            Gy[i-1][j-1].rgbtRed=-image2[i-1][j-1].rgbtRed-image2[i-1][j+1].rgbtRed-2*image2[i-1][j].rgbtRed+2*image2[i+1][j].rgbtRed+image2[i+1][j-1].rgbtRed+image2[i+1][j+1].rgbtRed;
            Gy[i-1][j-1].rgbtGreen=-image2[i-1][j-1].rgbtGreen-image2[i-1][j+1].rgbtGreen-2*image2[i-1][j].rgbtGreen+2*image2[i+1][j].rgbtGreen+image2[i+1][j-1].rgbtGreen+image2[i+1][j+1].rgbtGreen;

            image3[i][j].rgbtBlue=sqrt(Gx[i-1][j-1].rgbtBlue*Gx[i-1][j-1].rgbtBlue+Gy[i-1][j-1].rgbtBlue*Gy[i-1][j-1].rgbtBlue)+0.5;
            image3[i][j].rgbtRed=sqrt(Gx[i-1][j-1].rgbtRed*Gx[i-1][j-1].rgbtRed+Gy[i-1][j-1].rgbtRed*Gy[i-1][j-1].rgbtRed)+0.5;
            image3[i][j].rgbtGreen=sqrt(Gx[i-1][j-1].rgbtGreen*Gx[i-1][j-1].rgbtGreen+Gy[i-1][j-1].rgbtGreen*Gy[i-1][j-1].rgbtGreen)+0.5;
        }
    }

    for (int i=1; i<height+1; i++)
    {
        for (int j=1; j<width+1; j++)
        {
            if (image3[i][j].rgbtBlue>255)
            {
                image3[i][j].rgbtBlue=255;
            }
            if (image3[i][j].rgbtRed>255)
            {
                image3[i][j].rgbtRed=255;
            }
            if (image3[i][j].rgbtGreen>255)
            {
                image3[i][j].rgbtGreen=255;
            }
            image[i-1][j-1].rgbtBlue=image3[i][j].rgbtBlue;
            image[i-1][j-1].rgbtRed=image3[i][j].rgbtRed;
            image[i-1][j-1].rgbtGreen=image3[i][j].rgbtGreen;
        }
    }
    return;
}
1
  • What's the definition of RGBTRIPLE2 ?
    – Tritum
    May 22 '20 at 18:03
0

That was an interesting one. I assume that your structure 'RGBTRIPLE2' contains 3 ints, one for each color which is 12 bytes of memory reserved. When you open an image from the 'images' folder they are 400 x 600 pixels so that would be ‭2880000‬ bytes for each RGBTRIPLE2 variable. You're declaring 4 of them so that would be around 12 MB. As CS50 IDE is a web enviroment, which gives service to a lot of people, I suppose they have set the available memory limit below that, and that's what is causing your program to crash.

So if you want your program to run in CS50 IDE, you'll have to change your code reducing the use of memory.

2
  • I think you're right! After I reduce the use of memory, it can produce the right image. That's really helpful, thank you so much! :)
    – Carrie von
    May 23 '20 at 14:44
  • Can you please help me understand how you decreased the use of memory in your program, because I'm having the exact same problem with my grayscale filter. Thanks!
    – Hob
    Jun 10 '20 at 17:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .