在其他文件的结构/数组上使用malloc [英] Using malloc on structs / arrays in other files

查看:88
本文介绍了在其他文件的结构/数组上使用malloc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:分段错误的问题不在此函数内,如下所述,它在同一程序的另一个函数之内.

我正在尝试制作一个动画弹跳球的程序,但是我很困惑,无法弄清楚我在做什么错.我相信我已经将问题隔离在下面的功能之内.我已经弄清楚它与新模型语句有关.

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.

无论如何,在运行代码时,我遇到了分段错误,并且函数绘制的值(以三角形表示)偏离了它们应有的位置.我应该获得介于0到1600之间的值,但是有时最终我得到94百万.

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! Triangle_t *模型指针指向一个描述多个三角形的数组.

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

包括对象的结构:

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 */
};

以及三角形的结构:

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

推荐答案

此行仅复制第一个三角形:

This line is copying only the first triangle:

*new->model = *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.

替换为:

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

其他评论:

  • 记住,释放object
  • 时要释放model
  • 如果您考虑使用c ++编译器进行编译,请将new替换为newObj之类的东西
  • 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

更多信息:

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

关于分段错误:您的函数现在是正确的,除非内存不足,否则不会引起SEGFAULT,这是非常不可能的.无论如何,如果您的内存不足并在该函数中获取SEGFAULT,则问题可能是:

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:

  • 您没有在其他地方正确分配内存,然后内存泄漏使内存用尽不正确.
  • 您的平台需要更多的内存,尽管可能性不大,但可能会发生,尤其是在有限的嵌入式平台上

发布另一个带有段错误回溯的问题.

Post another question with the backtrace of the segfault.

这篇关于在其他文件的结构/数组上使用malloc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆