2013-01-17 91 views
2
#pragma pack(push, 1) 
typedef struct 
{ 
     /*...*/ 
    unsigned int  dataoffset;   //No of bytes before actual pixel data 
}HEADER; 

typedef struct 
{ 
    /*...*/ 
    unsigned int  width; 
    unsigned int  height; 
    unsigned short bits_per_pixel;   //code is written for 24 bits only and No other format is supported.. 
    /*...*/ 
}INFO_HEADER; 

typedef struct 
{ 
    unsigned char b; 
    unsigned char g; 
    unsigned char r;   
}COLORMAP; 

#pragma pack(pop) 


int main() 
{ 
     // Var decl. 
    INFO_HEADER  *pHeader = NULL; 
    FILE    *pImage; 
    COLORMAP   **ppColors; 
    /*...*/ 

/* File opened in read, binary mode, memory allocated for pHeader*/  

fread (pHeader, sizeof(INFO_HEADER), 1, pImage); 

/*Next block is actually problematic.. Posting 'as is' from my code*/ 
     ppColors = (COLORMAP**)malloc((pHeader -> height) * sizeof(COLORMAP)); 
    for(i = 0 ; i < pHeader -> height ; i++) 
    ppColors[i] = (COLORMAP*)malloc(pHeader -> width * sizeof(COLORMAP)); 
fseek(pImage, pHeader -> fileheader.dataoffset, SEEK_SET); 
    for (i = 0 ; i < pHeader -> width ; i++) 
    { 
     for (j = 0 ; j < pHeader -> height ; j++) 
     { 
      fread(&b, sizeof(unsigned char), 1, pImage); 
      fread(&g, sizeof(unsigned char), 1, pImage); 
      fread(&r, sizeof(unsigned char), 1, pImage); 

      ppColors[i][j].b = b; 
      ppColors[i][j].g = g; 
      ppColors[i][j].r = r; 

      printf("width = %d height = %d %d:\t", i, j, cnt); 
      printf("%d ", (int)ppColors[i][j].b); 
      printf("%d ", (int)ppColors[i][j].g); 
      printf("%d\n", (int)ppColors[i][j].r); 
      cnt++; 
     } 
    } 

    /*And at last free()ing..*/ 
    for(i = 0 ; i < pHeader -> height ; i++)  free(ppColors[i]); 
    free(ppColors); 
    cleanup(); 
    return(0) 
} 

可能重复:HTTP://stackoverflow.com/questions/1568042/optimal-way-to-free-a-malloced-2d- array-in-c结构的2D阵列 - malloc()和游离()

虽然上面的链接无法解决我的问题。

  1. 我用完了内存。我有malloc()ed为高度,然后为每个高度,宽度再次malloc()编辑。我正在尝试仅处理宽度X高度域。这似乎是问题与高度。如果更改

ppColors = (COLORMAP**)malloc((pHeader -> height) * sizeof(COLORMAP));ppColors = (COLORMAP**)malloc((pHeader -> height + 6) * sizeof(COLORMAP));

那么这个问题就会消失。

  1. 但是,在免费()ing的时候,我在核心转储中获得了双倍空闲/损坏。

我该死的确定我在某个地方出错..我不指望有人纠正我的代码,我只是运行它。只是暗示会做..

感谢 装饰

回答

2

我可以看到几个问题:

  • ppColors是一个指针数组。数组中的每个元素都是COLORMAP*,因此您需要使用numElements * sizeof(COLORMAP*)来计算大小。 COLORMAP只有3个字符,因此它有可能是sizeof(COLORMAP*)>sizeof(COLORMAP)。你当前的分配太小,所以你最终会在数组的末尾写入;这有未定义的效果,但可能会崩溃。
  • 使用宽度&高度在分配和循环之间是相反的,所以您最终会在循环中的某个位置写入未分配的内存。
+0

啊..问题是第一个子弹点什么。对于第二个项目符号 - >对不起,我已经在发布之前编辑了代码..并发布'原样'..但我仍然不明白为什么'sizeof(COLORMAP *)'..?你能详细解释一下..吗? – Adorn

+0

@Adorn很高兴帮助。我已经去更新我的答案了。让我知道如果它仍然不清楚。 – simonc

+0

That's clear ..谢谢。 – Adorn

3

指针指针是而不是与数组数组相同。

当使用指针到指针以模拟多维数组,像这样的声明:

char **pointer; 

存储器看起来像这样:

 
+------------+------------+-----+-------------------+ 
| pointer[0] | pointer[1] | ... | pointer[size - 1] | 
+------------+------------+-----+-------------------+ 
    |   |     | 
    v   v     v 
    Data   Data    Data 

虽然适当的多维array

char array[X][Y]; 

在内存中看起来像这样:

 
+-------------+-------------+-----+-----------------+-------------+-----+ 
| array[0][0] | array[0][1] | ... | array[0][Y - 1] | array[1][0] | ... | 
+-------------+-------------+-----+-----------------+-------------+-----+ 

所以在一个适当的多维数组中,所有的内存都在一个单独的块中,而使用指针指针指向的是指针数组而不是数组数组。


所以,你应该做的是单独分配所有子阵列:

ppColors = malloc(pHeader->height * sizeof(COLORMAP *)); 
/* Note how I allocate the width times the size of a COLORMAP pointer */ 

for (int i = 0; i < pHeader->height; i++) 
    ppColors[i] = malloc(pHeader->width * sizeof(COLORMAP)); 

不要忘了,你现在必须调用循环free呢!

1

这可能不是一个答案,但可能帮助你与其他答案一起。

  • 这可能与fread检查的问题,你在身边pHeader -> widthpHeader -> heightpHeader -> width

  • 使用括号获得malloc(pHeader -> width * sizeof(COLORMAP))

+0

谢谢..将铭记编码风格.. – Adorn