1
$\begingroup$

I am trying to figure out how to apply the Sobel edge detection for a BMP image, which is a 2D array of BYTE values. When multiplying a pixel by the GX or GY kernel, if the pixel is on the edge of the image, I'm supposed to assume the missing pixels have the RGB values of 0.

Wanting to test my method of how to know if a pixel is outside the 2D array. This is what I came up with, but it seems like I am accessing indexes that are out of range.

int array [3][3]={{1,2,3},{4,5,6},{7,8,9}};


printf ("\n");

for (int i=0;i<3;i++){
    for (int j=0;j<3;j++){

        int yAxis[3]={i-1,i,i+1};
        int xAxis[3]={j-1,j,j+1};

        int y = yAxis[i];
        int x = xAxis[j];


        printf(" y: %i x: %i v: %i",y,x,array[y][x]);

    }
    printf("\n");
}

If I try to print array[-1][-1] I get a compiling error:

arrayTest.c:27:14: error: array index -1 is before the beginning of the array [-Werror,-Warray-bounds]

But in the for loops, it seems to be printing the array value at index[-1][-1].

filter-more/ $ ./arrayTest

| y: -1 x: -1 v: 0 | y: -1 x: 1 v: 1 | y: -1 x: 3 v: 1 
| y: 1 x: -1 v: 3 | y: 1 x: 1 v: 5 | y: 1 x: 3 v: 7 
| y: 3 x: -1 v: 9 | y: 3 x: 1 v: -576368544 | y: 3 x: 3 v: 1 

To be clear, all those values (v:) will consistently show up except for y:3 x:1 that is the only value which will change every time the program is run.

So what am I missing that's going on here?

$\endgroup$
4
  • $\begingroup$ I don't really understand what you're asking. y: -1 x: -1 means you're accessing array[-1][-1], as you said. Do you want to know how x and y ended up with the value -1, or why the results of some out-of-bounds accesses change across runs and others don't, or why GCC doesn't catch the bug with -Warray-bounds, or something else? $\endgroup$
    – benrg
    Commented Jul 30 at 2:56
  • $\begingroup$ Why the out-of-bounds access is pulling junk values but only one is consistently junk and the others not also some massive "random" integer. I do also find it odd that the compiler basically lets this happen uncontested but I do not know enough about compiling to even have a confident starting point. $\endgroup$
    – Magnio
    Commented Jul 30 at 3:40
  • 2
    $\begingroup$ 1) Undefined behavior. Nothing is guaranteed, anything can happen. It's easy to explain y:3 x:-1 v:9: most likely, the values in array are stored in the memory sequentially, and this accesses the last element of the array. 2) The compiler doesn't catch it because at the moment of compilation it doesn't know that there may be out of bounds access. While it is possible to infer "out of bounds" for this particular example, due to certain reasons the compiler must stop the analysis at some point. $\endgroup$
    – Dmitry
    Commented Jul 30 at 4:57
  • $\begingroup$ @YvesDaoust Thanks buddy! I'm troubleshooting with a 3x3 bmp. $\endgroup$
    – Magnio
    Commented Jul 30 at 17:44

1 Answer 1

2
$\begingroup$

You have arrays of size 3. In the C language and related languages there are two kinds of legal pointers: First, pointers to array elements, that is &c[0], &c[1], &c[2], and pointers past the end of an array, that is &c[3]. You are allowed to read or write an item using a pointer to an array element but not using a pointer past the end of an array. You may compare any valid pointers using == or !=, and you may compare pointers using the same array using >=, >, <= and <.

Everything else is undefined behaviour. With undefined behaviour anything whatsoever can happen. That includes crashing, giving rubbish results, changing seemingly completely unrelated data, giving any kinds of unexpected results, and importantly doing exactly what you expected. And more importantly, doing exactly what you expect when you are testing your code, and malfunctioning and/or causing damage when you hand the software to a paying customer.

So what you are missing: Once you produce undefined behaviour just about anything whatsoever can happen.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.