首页 > 解决方案 > 重新分配结构?复制粘贴结构

问题描述

您好,我的任务是将完整结构复制到一个新结构中,并分配结构内存。我想将一个结构复制到另一个结构中,并且我想复制它的内存。因此,如果我“释放”第一个结构,那么我将在内存中找到此信息。

 struct bmp_image* flip_vertically(const struct bmp_image* image) {
    struct bmp_image* bmp = NULL;
    bmp = (struct bmp_image*)realloc(bmp, sizeof(struct bmp_image)); 
    memcpy(bmp, image, sizeof(struct bmp_image));

   return bmp;
}

可能是什么问题?

例如:

如果我知道

image->data[5].blue 是 255,我想复制它,但我还需要将该信息保存到内存中

主要任务是翻转bmp图片。一切对我来说都很完美,但是如果我把这个源代码用于测试,它会写道:

Running suite(s): flip_horizontally()

stderr

double free or corruption (!prev)

这意味着测试在某处释放旧结构,所以我在新结构中没有这些信息

struct bmp_header{
    uint16_t type;              // "BM" (0x42, 0x4D)
    uint32_t size;              // file size
    uint16_t reserved1;         // not used (0)
    uint16_t reserved2;         // not used (0)
    uint32_t offset;            // offset to image data (54B)
    uint32_t dib_size;          // DIB header size (40B)
    uint32_t width;             // width in pixels
    uint32_t height;            // height in pixels
    uint16_t planes;            // 1
    uint16_t bpp;               // bits per pixel (1/4/8/24)
    uint32_t compression;       // compression type (0/1/2) 0
    uint32_t image_size;        // size of picture in bytes, 0
    uint32_t x_ppm;             // X Pixels per meter (0)
    uint32_t y_ppm;             // X Pixels per meter (0)
    uint32_t num_colors;        // number of colors (0)
    uint32_t important_colors;  // important colors (0)
} __attribute__((__packed__));


/**
 * This structure describes a color consisting of relative intensities of
 * red, green, and blue.
 */
struct pixel {
    uint8_t blue;
    uint8_t green;
    uint8_t red;
    //uint8_t alpha;
} __attribute__((__packed__));


/**
 * Structure describes the BMP file format, which consists from two parts:
 * 1. the header (metadata)
 * 2. the data (pixels)
 */
struct bmp_image {
    struct bmp_header* header;
    struct pixel* data;         // nr. of pixels is `width` * `height`
};

主程序

int main () {
struct bmp_image* image = NULL;
    FILE *stream = fopen("assets/saboteur.bmp", "r");
    image = read_bmp(stream);

    FILE *output_p1 = fopen("square2.bmp", "w");
    struct bmp_image* newimage1 = NULL;
    newimage1 = flip_vertically(image);
    free_bmp_image(image);
    write_bmp(output_p1, newimage1);

    free(newimage1);     
    fclose(output_p1);
    fclose(stream);
    return 0;
}

如果我释放图像(旧结构),它会向我显示很多错误,我无法将其写入文件。这对我来说意味着它想从旧结构中读取。

标签: cpointersstructure

解决方案


memcpy进行“浅”复制而不是“深”复制。浅拷贝将只复制其中的指针值,struct bmp_image而不是它指向的内存。要进行深度复制,需要分配和复制各个字段。这是一些说明性代码。为简洁起见省略了错误检查,但对于最终代码,应检查所有分配结果。

struct bmp_image* flip_vertically(const struct bmp_image* image) {
    bmp = malloc(sizeof(*bmp)); 

    bmp->header = malloc(sizeof(*(bmp->header)));
    *bmp->header = *(image->header);

    size_t pixel_data_size =
        sizeof(*(bmp->data)) * bmp->header->width * bmp->header->height;
    bmp->data = malloc(pixel_data_size);
    memcpy(bmp->data, image->data, pixel_data_size);

   return bmp;
}

推荐阅读