线程 1:EXC_BAD_ACCESS (code=1) 错误 c++ 项目
Thread 1: EXC_BAD_ACCESS (code=1) error c++ project
我是编程新手。所以我不知道如何正确使用调试工具。 Xcode 告诉我“线程 1:EXC_BAD_ACCESS(代码=1,地址=0xffefbf3804)”。如果您能帮我解决这个问题并修复错误,我将不胜感激。程序必须读取文件.bmp,然后我想计算像素亮度值并将它们放入数组[][]中并使用它们。
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;
}
array_bmp[i][j]
将超出范围,因为它的列只有 width
个元素,而 j
将在 width*3
附近。应该是 array_bmp[i][j/3]
.
我是编程新手。所以我不知道如何正确使用调试工具。 Xcode 告诉我“线程 1:EXC_BAD_ACCESS(代码=1,地址=0xffefbf3804)”。如果您能帮我解决这个问题并修复错误,我将不胜感激。程序必须读取文件.bmp,然后我想计算像素亮度值并将它们放入数组[][]中并使用它们。
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;
}
array_bmp[i][j]
将超出范围,因为它的列只有 width
个元素,而 j
将在 width*3
附近。应该是 array_bmp[i][j/3]
.