Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 读取较大的.bmp文件时出现分段错误_C_Memory Management_Segmentation Fault - Fatal编程技术网

C 读取较大的.bmp文件时出现分段错误

C 读取较大的.bmp文件时出现分段错误,c,memory-management,segmentation-fault,C,Memory Management,Segmentation Fault,我在编译时遇到了一个分段错误 我想,当我尝试在main()中填充表数组时,第一个for循环中的内存分配有什么关系 如果我调用一个较小的文件,但不调用“biger”table.bmp文件,它就可以工作 我不明白为什么?(我对这个很陌生) 任何帮助都将不胜感激 提前谢谢 #include <stdio.h> #include <string.h> #include <malloc.h> unsigned char *read_bmp(char *fname,in

我在编译时遇到了一个分段错误

我想,当我尝试在main()中填充表数组时,第一个for循环中的内存分配有什么关系

如果我调用一个较小的文件,但不调用“biger”table.bmp文件,它就可以工作

我不明白为什么?(我对这个很陌生)

任何帮助都将不胜感激

提前谢谢

#include <stdio.h>
#include <string.h>
#include <malloc.h>

unsigned char *read_bmp(char *fname,int* _w, int* _h)
{
    unsigned char head[54];
    FILE *f = fopen(fname,"rb");

    // BMP header is 54 bytes
    fread(head, 1, 54, f);

int w = head[18] + ( ((int)head[19]) << 8) + ( ((int)head[20]) << 16) + ( ((int)head[21]) << 24);
int h = head[22] + ( ((int)head[23]) << 8) + ( ((int)head[24]) << 16) + ( ((int)head[25]) << 24);

// lines are aligned on 4-byte boundary
int lineSize = (w / 8 + (w / 8) % 4);
int fileSize = lineSize * h;

unsigned char *img = malloc(w * h), *data = malloc(fileSize);

// skip the header
fseek(f,54,SEEK_SET);

// skip palette - two rgb quads, 8 bytes
fseek(f, 8, SEEK_CUR);

// read data
fread(data,1,fileSize,f);



// decode bits
int i, j, k, rev_j;
for(j = 0, rev_j = h - 1; j < h ; j++, rev_j--) {
    for(i = 0 ; i < w / 8; i++) {
        int fpos = j * lineSize + i, pos = rev_j * w + i * 8;
        for(k = 0 ; k < 8 ; k++)
            img[pos + (7 - k)] = (data[fpos] >> k ) & 1;
    }`enter code here`
}

free(data);
*_w = w; *_h = h;
return img;
}

int main()
{

int w, h, i, j, x, y, b=0, butpos=0;

//Get array data

unsigned char* imgtable = read_bmp("table.bmp", &w, &h);
int table[w][h];

printf("w=%i \n", w);
printf("h=%i \n", h);

//make table array

 for(j = 0 ; j < h ; j++)
{

    for(i = 0 ; i < w ; i++)
        table[j][i] = imgtable[j * w + i] ? 0 : 1;

}
#包括
#包括
#包括
无符号字符*read\u bmp(字符*fname,int*\u w,int*\u h)
{
无符号字符头[54];
文件*f=fopen(fname,“rb”);
//BMP头是54字节
fread(负责人,1,54,f);

int w=head[18]+((int)head[19])如果有人有类似的问题,那似乎是内存分配的问题

下面的解决方案对我有效

unsigned char* img = read_table("table.bmp", &w, &h); 
int *data;
int **table;   

data = malloc(h * w * sizeof(*data));
table = malloc(h * sizeof(*table));
for (i = 0; i < h; ++i)
{
    table[i] = &data[i * w];
}   
unsigned char*img=read_table(“table.bmp”、&w、&h);
int*数据;
int**表格;
数据=malloc(h*w*sizeof(*数据));
表=malloc(h*sizeof(*表));
对于(i=0;i
您试图在堆栈上分配映像数据。当映像太大时,这会导致堆栈溢出。此代码就是问题所在:

int main()
{

    int w, h, i, j, x, y, b=0, butpos=0;

    //Get array data

    unsigned char* imgtable = read_bmp("table.bmp", &w, &h);
    int table[w][h];  // <-- HERE
    ...

现在可能是开始学习如何使用调试器的好时机。我尝试使用Valgrind,但在使用gdb时遇到了一个配置错误start out-这应该可以让您快速轻松地解决问题。我运行gdb并得到以下结果-程序接收信号SIGSEGV,Segmentation fault._alloca()位于/gnu/gcc/releases/respins/4.5.3-3/gcc4-4.5.3/src/gcc-4.5.3/libgcc/。/gcc/config/i386/cygwin.asm:45-45 in/gnu/gcc/releases/respins/4.5.3-3/gcc-4.5.3/libgcc/。/gcc/config/i386/cygwin.asm不只是“运行gdb”-一步一步地浏览代码,在每一步检查您的局部变量,看看代码是否真的按预期运行。首先,我觉得您试图读取的字节数比文件中实际存在的字节数还要多,除此之外,可能还有其他错误。
data = malloc(h * w * sizeof(*data));
table = malloc(h * sizeof(*table));
for (i = 0; i < h; i++)
{
    table[i] = &data[i * w];
}

...

free(table);
free(data);