Adding an int member to a C structure calls segfault

I am still learning C and started using it to create images. I can’t understand why one of my programs was interrupted. Here's the source code shortened to 40 lines:

#include <stdio.h>
#include <stdlib.h>
struct color {
        unsigned char r, g, b;
};
struct image {
        int w, h/*, o*/;
        struct color **data;
};
int main() {
        // Declarations
        int x, y;
        struct color *black;
        struct image *img;
        // Set up color black
        black = (struct color *) malloc(sizeof(struct color *));
        black->r = 0;
        black->g = 0;
        black->b = 0;
        // Set up image img
        img = (struct image *) malloc(sizeof(struct image *));
        img->w = 1;
        img->h = 1;
        /*img->o = 0;*/
        img->data = (struct color **) malloc(img->h * sizeof(struct color *));
        for (y = 0; y < img->h; y++) {
                img->data[y] = (struct color *) malloc(img->w * sizeof(struct color));
        }
        // Fill in img with black
        for (x = 0; x < img->w; x++) {
                for (y = 0; y < img->h; y++) {
                        img->data[y][x].r = black->r;
                        img->data[y][x].g = black->g;
                        img->data[y][x].b = black->b;
                }
        }
        // Free black
        free(black);
        // Free img
        for (y = 0; y < img->h; y++)
                free(img->data[y]);
        free(img->data); // Segfaults
        free(img); // Also segfaults
        return 0;
}

It compiles and works fine (using gcc on Ubuntu and Vista with Cygwin), but uncommenting the two lines regarding img-> o interrupts it. I have a feeling this is related to this previous question , but I'm malloc'ing everything that should be malloc'ed (I think). Any help would be appreciated.

+3
source share
4 answers

malloc . mallocing , . 4 , .

black = malloc(sizeof(*black));

. sizeof(*black), , , black .

+21

, segfault. malloc , , . , :

img = (struct image *)malloc(sizeof(struct image))
img->o = 0
+1

JaredPar , segfault, , , valgrind. .

By the way, I spent days on this exact mistake. Be happy that you stumbled upon it at the beginning of your C programming career and will always keep an eye on it in the future.

+1
source

Unfortunately, the code has been disabled; I forgot to avoid the less sign. Here it is:

#include <stdio.h>
#include <stdlib.h>
struct color {
    unsigned char r, g, b;
};
struct image {
    int w, h/*, o*/;
    struct color **data;
};
int main() {
    // Declarations
    int x, y;
    struct color *black;
    struct image *img;
    // Set up color black
    black = (struct color *) malloc(sizeof(struct color *));
    black->r = 0;
    black->g = 0;
    black->b = 0;
    // Set up image img
    img = (struct image *) malloc(sizeof(struct image *));
    img->w = 1;
    img->h = 1;
    /*img->o = 0;*/
    img->data = (struct color **) malloc(img->h * sizeof(struct color *));
    for (y = 0; y < img->h; y++) {
        img->data[y] = (struct color *) malloc(img->w * sizeof(struct color));
    }
    // Fill in img with black
    for (x = 0; x < img->w; x++) {
        for (y = 0; y < img->h; y++) {
            img->data[y][x].r = black->r;
            img->data[y][x].g = black->g;
            img->data[y][x].b = black->b;
        }
    }
    // Free black
    free(black);
    // Free img
    for (y = 0; y < img->h; y++)
        free(img->data[y]);
    free(img->data);
    free(img);
    // Return
    return 0;
}
0
source

Source: https://habr.com/ru/post/1703710/


All Articles