可以对malloc缓冲区执行整数运算(例如加法或减法)吗?

To learn about JPEG compression, I downloaded a simple BMP/JPEG encoder that I have been examining. I have several questions, hopefully someone can help me with. I'm not a trained computer scientist, just a hobby programmer. Grateful for any help.

以下代码读取位图,为简洁起见,我省略了一些信息。我知道位图标头是作为结构体编写的,因此可以将组件直接读取为变量,而无需进行迭代。

BYTE是uint8_t的typedef

    int bmp_load(BMP *pb, char *file)
{
    BMPFILEHEADER header = {0};
    FILE         *fp     = NULL;
    BYTE         *pdata  = NULL;
    int           i;

    fp = fopen(file, "rb");
    if (!fp) return -1;

    fread(&header, sizeof(header), 1, fp);
    pb->width  = header.biWidth;
    pb->height = header.biHeight;
    pb->stride = ALIGN(header.biWidth * 3, 4);
    pb->pdata  = malloc(pb->stride * pb->height);// allocate a byte for each element of bitmap?
    if (pb->pdata) {
        pdata  = (BYTE*)pb->pdata + pb->stride * pb->height; // Title question: Cast pdata as uint8_t buffer but what is the addition for? I think it has to do with padding a BMP file.  Without addition there is a bus error: 10 
        for (i=0; i<pb->height; i++) {
            pdata -= pb->stride; // What is the subtraction for? 
            fread(pdata, pb->stride, 1, fp);
        }
    }

    fclose(fp);
    return pb->pdata ? 0 : -1; // If pdata was being modified in the for loop, why is pb->pdata returned? 
}

我的问题是代码中的注释,也粘贴在下面。 加法有什么作用?我认为这与填充BMP文件有关。不添加总线错误:10

    pdata  = (BYTE*)pb->pdata + pb->stride * pb->height;

减法是什么?

    pdata -= pb->stride;

最后,为什么返回pb-> pdata,但是实际上在代码中修改了它的pdata呢?

评论