Segment faults are caused by attempting to address an invalid address. I will assume
data
is a pointer to the base of an image buffer. What you can do is check the validity of the rest of that expression. That means you can write a function that looks something like this :
int IsValidPixelOffset( int i, int j, int width, int bufferSize )
{
int offset = ( j + width * i ) * byte_per_pixel;
if( offset < bufferSize )
return 1;
return 0; }
Call this function before dereferencing the pointer and it should tell you if will you have a valid address or not. The function signature and implementation will likely have to change to suit your application. If it tells you the offset is correct and you still get a fault then your buffer was probably not allocated to be large enough.