Using malloc on structs / arrays in other files

2019-08-26 06:54发布

问题:

UPDATE: The problem with the segmentation fault is not within this function as described below, it is within another function of the same program.

Im trying to make a program that animates bouncing balls, however I am quite stuck and can't figur out what I am doing wrong. I believe I have isolated the problem to be within the function below. I have sort of figured out that it has something to do with the new-model statements.

Anyway, upon running the code I get segmentation fault and the values drawn by the function (in terms of triangles) are way out of where they should be. I should be getting values between 0 and 1600 but I end up with 94 miillion sometimes.

Any help is greatly appreciated!

object_t *create_object(SDL_Surface *surface, triangle_t *model, int numtriangles){
    object_t *new=malloc(sizeof(object_t));
    new->surface = surface;
    new->model = malloc(sizeof(triangle_t)*numtriangles);
    *new->model= *model;
    new->numtriangles = numtriangles;
    new->tx = surface->w/2;
    new->ty = surface->h/2;
    new->scale = 0.1;
    new->rotation = 0.0;

    return new;
}

NB! The triangle_t *model pointer points to an array wich describes multiple triangles.

EDIT: Including struct of object:

typedef struct object object_t;

struct object {
       float       scale;          /* Object scale */
       float       rotation;       /* Object rotation */
       float       tx, ty;         /* Position on screen */

       float       speedx, speedy; /* Object speed in x and y direction */
       unsigned int ttl;           /* Time till object should be removed from screen */

       int         numtriangles;   /* Number of triangles in model */
       triangle_t  *model;         /* Model triangle array */

       SDL_Surface *surface;       /* SDL screen */
};

And struct of triangles:

typedef struct triangle triangle_t;

struct triangle {
    /* Model coordinates, where each pair resemble a corner  */
    int x1, y1;
    int x2, y2;
    int x3, y3;

    /* The color the triangle is to be filled with */
    unsigned int fillcolor;

    /* Scale factor, meaning 0.5 should half the size, 1 keep, and 2.0 double */
    float scale;

    /* The point (tx, ty) where the center of the teapot should be placed on-screen */
    int tx, ty;

    /* The degrees the triangle is supposed to be rotated at the current frame */
    float rotation;

    /* 
     * Bounding box of on-screen coordinates:
     * rect.x - x-coordinate of the bounding box' top left corner
     * rect.y - y-coordinate of the bounding box' top left corner
     * rect.w - width of the bounding box
     * rect.h - height of the bounding box
     */
     SDL_Rect rect;

    /* On-screen coordinates, where each pair resemble a corner */
    int sx1, sy1;
    int sx2, sy2;
    int sx3, sy3;
};

回答1:

This line is copying only the first triangle:

*new->model = *model;

From the point of view of your function model is only a pointer to an object. The compiler doesn't know it points to an array of triangles, hence we need to pass the number of triangles in there as an argument.

Replace it for:

memcpy( new->model, model, sizeof(triangle_t)*numtriangles);

Additional comments:

  • Remember to free the model when freeing the object
  • Replace new for something else like newObj if you ever consider to compile this with a c++ compiler

More info:

  • https://linux.die.net/man/3/memcpy
  • https://en.cppreference.com/w/c/string/byte/memcpy

[EDIT] Regarding to the segmentation fault: your function is correct now and it is not causing SEGFAULT unless you are running out of memory, what is very unlikely. Anyway, if you are running out of memory and getting a SEGFAULT in that function then the problem is either:

  • you are not deallocating memory correctly somewhere else and then you have a memory leak making you run out of memory improperly.
  • your platform needs more memory what, despite unlikely, is possible especially if it is a limited embedded platform

Post another question with the backtrace of the segfault.



标签: c struct malloc