在其他文件的结构/数组上使用malloc
更新:分段故障的问题不在如下所述的该功能内,而是在同一程序的另一个功能内 我试图制作一个程序,动画弹跳球,但我很卡住,不能找出我做错了什么。我相信我已将问题隔离在以下功能范围内。我已经发现它与新的模型语句有关 无论如何,在运行代码时,我得到了分段错误,并且函数绘制的值(以三角形表示)与它们应该位于的位置不符。我应该得到0到1600之间的值,但有时我会得到9400万 非常感谢您的帮助在其他文件的结构/数组上使用malloc,c,struct,malloc,C,Struct,Malloc,更新:分段故障的问题不在如下所述的该功能内,而是在同一程序的另一个功能内 我试图制作一个程序,动画弹跳球,但我很卡住,不能找出我做错了什么。我相信我已将问题隔离在以下功能范围内。我已经发现它与新的模型语句有关 无论如何,在运行代码时,我得到了分段错误,并且函数绘制的值(以三角形表示)与它们应该位于的位置不符。我应该得到0到1600之间的值,但有时我会得到9400万 非常感谢您的帮助 object_t *create_object(SDL_Surface *surface, triangle_t
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;
}
注意!三角形*模型指针指向一个数组,该数组描述多个三角形
编辑:
包括对象的结构:
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;
};
此行仅复制第一个三角形:
*new->model = *model;
从函数的角度来看,模型
只是指向对象的指针。编译器不知道它指向一个三角形数组,因此我们需要将其中的三角形数作为参数传递
将其替换为:
memcpy( new->model, model, sizeof(triangle_t)*numtriangles);
补充意见:
- 释放
对象时,请记住释放
模型
<>替换为<代码>新< /代码>,如“代码> NeWOBI< /COD>”,如果您考虑用C++编译器编译此代码
- 您没有将内存正确地释放到其他地方,然后内存泄漏导致内存不足
- 您的平台需要更多的内存,尽管不太可能,但这是可能的,特别是如果它是一个有限的嵌入式平台
用segfault的回溯发布另一个问题。这一行仅复制第一个三角形:
*new->model = *model;
从函数的角度来看,模型
只是指向对象的指针。编译器不知道它指向一个三角形数组,因此我们需要将其中的三角形数作为参数传递
将其替换为:
memcpy( new->model, model, sizeof(triangle_t)*numtriangles);
补充意见:
- 释放
对象时,请记住释放
模型
<>替换为<代码>新< /代码>,如“代码> NeWOBI< /COD>”,如果您考虑用C++编译器编译此代码
- 您没有将内存正确地释放到其他地方,然后内存泄漏导致内存不足
- 您的平台需要更多的内存,尽管不太可能,但这是可能的,特别是如果它是一个有限的嵌入式平台
发布另一个带有segfault回溯的问题。您应该包括结构的定义。当然!我现在已经把它们包含进来了,技术上没有错,在C中调用变量<代码>新< /Cord>可能会混淆,因为<代码>新< /COD>是C++中的保留字。建议将其命名为其他名称。您应该包括结构的定义。当然!我现在已经把它们包含进来了,技术上没有错,在C中调用变量<代码>新< /Cord>可能会混淆,因为<代码>新< /COD>是C++中的保留字。建议换个名字。谢谢!它确实使三角形正确地平移,现在它们有了正确的值!分割错误仍然存在?分割错误不是由该功能引起的是的,我开始意识到这可能根本不是问题所在!但是谢谢大家!谢谢它确实使三角形正确地平移,现在它们有了正确的值!分割错误仍然存在?分割错误不是由该功能引起的是的,我开始意识到这可能根本不是问题所在!但是谢谢大家!