I've written code to place a smaller BMP file (A) in a bigger BMP file (B) in Android Native Code.
Edit: more details on what fileA and fileB are like
//initialised as global variables
char * fileA;
char * fileB;
//file A and B are passed in to native C as byte arrays from Java
fileA=(char *) env->GetByteArrayElements(bufferA,&isCopy);
fileB=(char *) env->GetByteArrayElements(bufferB,&isCopy);
fileA consists of the BMP data from all BMPs of the 4 groups placed one after the other.
fileB is the BMP of the bigger file which I want to write on.
My initial implementation uses "=" assignment operator to copy byte by byte from file A to file B. Both are initialised as char arrays.
I had 4 groups of BMPs used for file A. BMPs of each group are the same size. Group 1 is 376 x 54, group 2 is 297 x 48, group 3 is 885 x 83 and group 4 is 238 x 108.
The simplified code to write 1 BMP (BMP X) from fileA into fileB.
//calculate offset on where to write in fileB
offset = ycoord_of_BMP_X * width_of_file_B + xcoord_of_BMP_X*BYTES_PER_PIXEL;
for (int a = 0; a < height_of_BMP_X; a++){
for (int b = 0; b< width_of_BMP_X; b++){
fileB[offset+ a*width_of_BMP_X + b] = fileA[position_of_BMP_X_in_file_A + (height_of_BMP_X-a-1)*width_of_BMP_X*BYTES_PER_PIXEL + b];
}
offset = offset + width_of_fileB*BYTES_PER_PIXEL-width_of_BMP_X;
}
This works fine with no segmentation fault. Which leads me to conclude that the algorithm and its implementation is fine.
I replaced the inner for loop with memcpy and resulted in the following code (again simplified):
for (int a = 0; a < height_of_BMP_X; a++){
memcpy(fileB[offset+ a*width_of_BMP_X],fileA[position_of_BMP_X_in_file_A + (height_of_BMP_X-a-1)*width_of_BMP_X*BYTES_PER_PIXEL],width_of_BMP_X*BYTES_PER_PIXEL]);
offset = offset + width_of_fileB*BYTES_PER_PIXEL-width_of_BMP_X;
}
Have to stress that I reference to fileA and fileB in both cases were almost identical (extracted below for clarity). I only removed the "+b" as it is no longer required for memcpy.
//the assignment case
fileB[offset+ a*width_of_BMP_X+b]
//memcpy case
fileB[offset+ a*width_of_BMP_X]
Segmentation fault (Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR)) occurs for group 3 and group 4 BMPs. Segmentation fault is reported when group 3 reaches a = 52. ie. memcpy is successful for first 52 out of 83 loops. For group 4, it is 92 out of 108 loops.
I printed out the array positions referenced in fileA and fileB arrays, for example values of "initial position x" for each iteration in the outer loop and it matches for both implementations.
Is the segmentation fault a result of some kind of optimisation done by memcpy, resulting in a race condition happening?
Thank you.