1

I send array int Vetor[33]; by parameter for the function MontaVetorVerticalOtimizado(x, y, Vetor), inside this the array is filled, the problem is that after filling the array all the variables of the function OtimizaVerticalDentina() are signed with the value of the array, it seems confusing, so I added images while debugging to make it more understandable:

First function:

void OtimizaVerticalDentina() {
    int Vetor[33];
    int x, y;
    for (x = 1; x < NewImage.SizeX() - 1; x++)
    {
        for (y = 10; y < NewImage.SizeY() - 10; y++)
        {
            MontaVetorVerticalOtimizado(x, y, Vetor);
            VerificaIntensidadeVetorVerticalOtimizado(Vetor);
            if (bPreenche) {
                NewImage.DrawPixel(x, y, 255, 255, 255);
            } else {
                NewImage.DrawPixel(x, y, 0, 0, 0);
                bPreenche = true;
            }
        }

    }
}

Second function:

void MontaVetorVerticalOtimizado(int Px, int Py, int Vetor[33])
{
    int x, y;
    int i = 0;
    unsigned char r, g, b;
    for(x = Px - 1; x <= Px + 1; x++)
    {
        for(y = Py - 10; y <= Py + 10; y++)
        {
           NewImage.ReadPixel(x, y, r, g, b);
           Vetor[i] = r;
           i++;
        }
    }
}

Note:

ImageClass NewImage; // global

Before filling the array variables are with their normal value enter image description here

After populating the array the variables are with another value (the value that was added in the vector)enter image description here

*I have created other variables in the first method to test and they have also changed, does anyone have any idea what might be happening?

Community
  • 1
  • 1
Mathiasfc
  • 1,627
  • 1
  • 16
  • 24
  • Which compiler? Have you added print statements to confirm (in case the debugger is lying to you)? – Borgleader Jun 16 '17 at 15:54
  • ah, my eye, too many white ! – Stargateur Jun 16 '17 at 15:55
  • @Borgleader GNU GCC Compiler, I printed the value of the variable on the console before and after filling the array and showed that the compiler is not lying. – Mathiasfc Jun 16 '17 at 15:56
  • @Stargateur Sorry but this was necessary... – Mathiasfc Jun 16 '17 at 15:57
  • From the `<=` in your for loop I have the sneaking suspicion this is an array overrun, can you confirm you never index outside of your array size? (`< n - 10`, and then `<= n + 10`, seems like off by one). P.S: you can also try moving the x, y variables before the array declaration and see if the problem persists. – Borgleader Jun 16 '17 at 16:00
  • @Stargateur I'm manipulating images and applying filters on them, in my application I have two images (`ImageClass Image` and `ImageClass NewImage`) – Mathiasfc Jun 16 '17 at 16:05
  • @Borgleader I confirm you that it is not a problem in the for, testing without the method that fills the array, the index wont go outside of array size. – Mathiasfc Jun 16 '17 at 16:08
  • @Stargateur Yes. – Mathiasfc Jun 16 '17 at 16:08
  • Well, I agree with @Borgleader, try to print `if (i < 0 || i > 32) cout << "Problem: " << i << std::endl;` – Stargateur Jun 16 '17 at 16:12

1 Answers1

1

The only explanation I can find is that you have a buffer overrun. That is is you are writing to this array (Vetor) which is not big enough and happen to overwrite unrelated memory in the process. In this case specifically you are overwriting the value of the variables x and y of the calling function.

I have demo here:

#include <iostream>

void bar(int* arr)
{
    for (int i = 0; i <= 35; i++) arr[i] = 255;
}

void foo()
{
    int arr[33];
    int x;
    for (x = 0; x < 5; x++)
    {
        std::cout << x << '\n';
        bar(arr);
        std::cout << x << '\n';
    }
}

int main()
{
    foo();
    return 0;
}

This produces: 0 255 and immediately terminates because the loop variable got overwritten and the subsequent x < 5 check fails. You either have to increase the size of the array (if it turns out it was too small) or make sure you index within its bounds.

Borgleader
  • 15,826
  • 5
  • 46
  • 62