-1

I'm new to programming. So I don't know how to properly use the debugging tools. Xcode tells me that " Thread 1: EXC_BAD_ACCESS (code=1, address=0xffefbf3804)". I would really appreciate it if you could help me figure this out and fix the bug. The program must read the file .bmp, then I want to count the pixel brightness values and put them in an array[][] and work with them.

int main()
{
char* filename = (char*)("/…/second.bmp");      
FILE* f = fopen(filename, "rb");
char info[54];
fread(info, sizeof(char), 54, f);
int width = *(int*)&info[18];
int height = *(int*)&info[22];

cout << "  Name: " << filename << endl;
cout << " Width: " << width << endl;
cout << "Height: " << height << endl;
cout << endl;

int row_padded = (width * 3 + 3) & (~3);
unsigned char* data = new unsigned char[row_padded];
int array_bmp[height][width];
int arr_bmp[height][width];

for(int i = 0; i < height; i++)
{
    fread(data, sizeof(char), row_padded, f);
    for(int j = 0; j < width*3; j += 3)
    {      
//    Y = 0.3 * R + 0.59 * G + 0.11 * B - яркость пикселя
//           ============================
        array_bmp[i][j] = 0.3 * (int)data[j+2] + 0.59 * (int)data[j+1] + 0.11 *  (int)data[j]; // error message here
//            ==========================
        }
}
delete[] data;
fclose(f);


return 0;
}
tadman
  • 208,517
  • 23
  • 234
  • 262
Aver666
  • 11
  • 1
  • 5
    There's a lot of alarming problems here, many of which stem from an almost pathological pattern of casting everyone plus dog. **Don't**. Stop doing this. YOu're throwing away *important type information*. `const char* filename = "..."` is the correct version. Casting away the `const` is a huge problem. – tadman May 21 '21 at 22:53
  • 3
    `int width = *(int*)&info[18];` Undefined behaviour. Your CPU, if it was manufactured in the 21st century, is going to tell you to get lost when you're trying to access a misaligned `int`. You *need* to use the proper tools to recast these. You cannot just bludgeon the values into your expected form. – tadman May 21 '21 at 22:54
  • `"So I don't know how to properly use the debugging tools"` -- I strongly suggest that you read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Andreas Wenzel May 21 '21 at 23:15
  • 1
    My general rule of thumb when I see a C-style cast like `(char*)` or a `reinterpret_cast` is to look closer for the bug. My first step is often to remove the cast and rebuild to see what helpful diagnostics the compiler gives me. The compiler is your friend and the errors and warnings it gives are a sign of love. They're the compiler telling you it doesn't want you wasting your life fixing simple logic errors. – user4581301 May 21 '21 at 23:18
  • `int array_bmp[height][width]; int arr_bmp[height][width];` -- Note that this is not valid C++. Arrays in C++ must have their sizes denoted by compile-time constants, not runtime variables. Even if this were valid, you are risking exhausting the stack memory if `height` and/or `width` are large values. Instead, use `std::vector> array_bmp(height, std::vector(width)); auto arr_bmp = array_bmp;` , then when satisfied, change that to using a single dimension `std::vector arr_bmp(height * width)` and compute the row/col location. – PaulMcKenzie May 21 '21 at 23:35

1 Answers1

1

array_bmp[i][j] will be out-of-range because its column has only width elements while j will go around width*3. It should be array_bmp[i][j/3].

MikeCAT
  • 73,922
  • 11
  • 45
  • 70