简体   繁体   English

将数组分配给结构中的数组时出现分段错误

[英]Segmentation fault when assigning array to array in structure

I get segmentation fault from this code.我从这段代码中得到分段错误。 It's a function for vertical flipping of a BMP image.这是一个用于垂直翻转 BMP 图像的 function。 I am trying to assign an array to another array in a structure.我正在尝试将一个数组分配给结构中的另一个数组。 I would appreciate your help.我会很感激你的帮助。

struct bmp_image *flip_horizontally(const struct bmp_image *image) {
    if (image == NULL)
    {
        return NULL;
    }

    struct bmp_image *transformed = NULL;
    transformed = (struct bmp_image *)calloc(1, sizeof(image->data) + sizeof(image->header));

    transformed->header = image->header;

    struct pixel data_array[image->header->height][image->header->width];

    int index = 0;
    for (int i = 0; i < image->header->height; i++)
    {
        for (int j = (int)image->header->width - 1; j >= 0; j--)
        {
            data_array[i][j].blue = image->data[index].blue;
            data_array[i][j].green = image->data[index].green;
            data_array[i][j].red = image->data[index].red;
            index++;
        }
    }

    struct pixel *transformed_data = calloc((image->header->width * image->header->height), sizeof(struct pixel));

    index = 0;
    // I think that the code after this line causes the segmentation fault
    for (int i = 0; i < image->header->height; i++)
    {
        for (int j = 0; j < image->header->width; j++)
        {
            transformed_data[index].blue = data_array[i][j].blue;
            transformed_data[index].green = data_array[i][j].green;
            transformed_data[index].red = data_array[i][j].red;
            index++;
        }
    }
    transformed->data = transformed_data;

    return transformed;
}

In my opinion... I would create a single array per row of pixels in the bmp image, and an array of pointers to arrays of pixels, pointing each to each of the successive rows of your image.在我看来......我会在 bmp 图像中的每行像素创建一个数组,以及一个指向 arrays 像素的指针数组,将每个像素指向图像的每一行。 Then flipping the rows is just going from the extremes of the array with two indices, and swapping the pointers, until both indices meed at the middle of the array.然后翻转行只是从具有两个索引的数组的极端开始,并交换指针,直到两个索引都在数组的中间。 Then encode again the array and you will have done this without having to copy all the pixels up and down.然后再次对数组进行编码,您将完成此操作,而无需上下复制所有像素。

+-------+      +-----+-----+-----+-----+-----+-----+-----+-----+-----+
| P[0] =======>| Pxl | Pxl | ... |     |     |     |     |     |     |
+-------+      +-----+-----+-----+-----+-----+-----+-----+-----+-----+
| P[1] =======>|     |     |     |     |     |     |     |     |     |
+-------+      +-----+-----+-----+-----+-----+-----+-----+-----+-----+
  ...
+-------+      +-----+-----+-----+-----+-----+-----+-----+-----+-----+
| P[N] =======>|     |     |     |     |     |     |     |     |     |
+-------+      +-----+-----+-----+-----+-----+-----+-----+-----+-----+

As you have not posted complete code, it's impossible for me to tweak your code to make it simpler and show a solution by modifying it.由于您尚未发布完整的代码,因此我无法调整您的代码以使其更简单并通过修改它来显示解决方案。 But something like (beware, this code is not tested):但是类似的东西(请注意,此代码未经测试):

void bitimage_swap(struct pixel **to_swap, size_t nrows)
{
    struct pixel **start = to_swap, **end = to_swap + nrows;
    while (start < --end) {
        struct pixel *temp = *start;
        *start++ = *end;
        *end = temp;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM