malloc“损坏的顶部大小”错误的未知原因

问题描述 投票:0回答:1

我一直在开发一个小项目,就在完成间接调用下面的函数后,我开始收到

malloc: corrupted top size
错误。我已经使用 Valgrind 将问题范围缩小到上述函数中的
fread
,但我找不到它的编写方式有任何错误。我只是盲目的,还是我应该开始寻找其他地方?

有问题的功能:

bmpHead_t* readBMPHeader(FILE* inputFile){
    checkBMPFile(inputFile);
    bmpHead_t* newHeader = malloc(sizeof(bmpHead_t));
    int headerSize = sizeof(bmpHead_t) / 2;
    uint16_t* wordBuff = malloc(headerSize);
    fseek(inputFile, 0, SEEK_SET);
    fread(wordBuff, sizeof(uint16_t), headerSize, inputFile);
    newHeader->signature[0] = (uint8_t)((wordBuff[0] & 0xFF00) >> 8);
    newHeader->signature[1] = (uint8_t)(wordBuff[0] & 0xFF);
    uint32_t tmp = ((uint32_t)wordBuff[2] << 16) | wordBuff[1];
    newHeader->fileSize = tmp;
    newHeader->reserved1 = wordBuff[3];
    newHeader->reserved2 = wordBuff[4];
    tmp = ((uint32_t)wordBuff[6] << 16) | wordBuff[5];
    newHeader->offToPixels = tmp;
    return newHeader;
}

对于上下文,我正在读取的标头是 Windows BITMAPFILEHEADER

c malloc valgrind bmp memory-corruption
1个回答
0
投票

正如杰里米·弗里斯纳(Jeremy Friesner)在我的帖子下面的评论中所说,我分配了不正确的内存量。

© www.soinside.com 2019 - 2024. All rights reserved.