bubble sort crashing out of array

Hello everyone,

I’m doing a simple bubble sort on the pixel array, however somewhat randomly, maybe once out of every 10 times I compile, my app crashes out of the array on launch or crashes within a couple minutes of being opened. Otherwise it seems to work just fine.

First off I have a simple swap function that takes an array and a spot for r,g, and b bytes and the spot it should swap with.

  
  
void testApp::swap(unsigned char a[], int rx, int ry, int gx, int gy, int bx, int by ) {  
//swap reds  
    int tempR = a[rx];  
    a[rx] = a[ry];  
    a[ry] = tempR;  
  
//swap greens  
    int tempG = a[gx];  
    a[gx] = a[gy];  
    a[gy] = tempG;  
  
 //swap blues  
    int tempB = a[bx];  
    a[bx] = a[by];  
    a[by] = tempB;  
  
}  
  

Next I gather the pixels with getPixels and loop through the array

This one would swap left

  
  
unsigned char * pix = screen.getPixels();  
  
    for(int y = 0; y<h; y++){  
        for(int x = 0; x<w; x++){  
            int loc = (x+y*w)*3;  
            int r = loc;  
            int g = loc+1;  
            int b = loc+2;  
              
            if(pix[loc-3] > pix[loc] ){  
                swap(pix, r, r-3, g, g-3, b, b-3);  
            }  
            
        }  
    }  
  

This one swaps vertically

  
  
        for(int y = 0; y<w*h; y++){  
            int loc = y*3;  
            int r = loc;  
            int g = loc+1;  
            int b = loc+2;  
  
            if(pix[loc-w*3] > pix[loc] ){  
               swap(pix, r, r-w*3, g, g-w*3, b, b-w*3);  
            }  
        }  
  

They both seem to cause errors. Is there something I am doing wrong with my bubble sort? I’m not interested in getting to a sorted state efficiently, I’m more interested in seeing the pixels flying around swapping with each other.

So the core of my question is, how can I prevent the array from crashing, and why does it happen only sometimes? What can I use to check that I am not making calculations outside of the array?

I tried doing something like:

  
  
            if(pix[loc-w*3] > pix[loc] && pix[loc-w*3] > pix[0]){  
               swap(pix, r, r-w*3, g, g-w*3, b, b-w*3);  
            }  
  

but no dice. Although that does seem to affect how the bubble sort performs on a row by row basis rather than on the array as a whole.

Is this even a good way to go about doing a bubble sort on the rgb array? I have to keep the pixel’s in their right order, hence performing the swap on all three channels simultaneously. I feel like my code is a little silly, but it’s the only way I could figure it out. It seems like a better way would be to create a struct and contain the rgb inside of that and then sort the structs, but I’ve never worked with structs so I’m a bit uncertain about how to implement such a thing.

Thanks for reading and for the help in advance!

It looks to me like you don’t have rigorous bounds checks on your indicies

  
unsigned char * pix = screen.getPixels();    
    
    for(int y = 0; y<h; y++){    
        for(int x = 0; x<w; x++){    
            int loc = (x+y*w)*3;    
                
            if(pix[loc-3] > pix[loc] ){    
                swap(pix, r, r-3, g, g-3, b, b-3);    
            }    
              
        }    
    }  

When x = 0 and y = 0 then loc = (0+0*w)*3 = 0… So pix[loc-3] is going after pix[-3] which is obviously is problematic - Maybe add another check if (loc-3) > 0???

Not 100% sure why it wouldn’t crash all the time though as it looks like that should get hit at least once each time the loop gets run.