重新分配结构?复制粘贴结构

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

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

 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`
};

Main.c]

int main () {
struct bmp_image* image = NULL;
    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);
    return 0;
}

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

c pointers structure
1个回答
0
投票

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;
}
© www.soinside.com 2019 - 2024. All rights reserved.