当我更改与之相关的结构时,内存分配不起作用

发布于 2025-01-20 03:58:08 字数 707 浏览 1 评论 0原文

我目前正在使用一个结构

struct Player{
   Object obj;
   int touched;
};
typedef struct Player *Player;

,这是该结构中元素的创建:

Player createPlayer(int posiX,int posiY){
    Player p;
    p=malloc(sizeof(p));
    p->obj.posi.x=posiX;
    p->obj.posi.y=posiY;
    p->obj.life=100;
    p->touched=0;
    p->obj.damage=5;
    p->obj.friend=true;
    return p;
}

这样的每个编译都完美。 但是,如果我的结构中有2个元素,

struct Player{
   Object obj;
   int touched;
   int frequency;
   int lastTouch;
};
typedef struct Player *Player;

我会在执行中收到此错误消息(代码完美地编译):

malloc():无效的大小(UNSORTED) 中止(核心倾倒)

我不明白为什么会收到此错误消息,因为我还没有使用这两个新变量。

I'm currently working with a structure

struct Player{
   Object obj;
   int touched;
};
typedef struct Player *Player;

And here's the creation of an element from this structure :

Player createPlayer(int posiX,int posiY){
    Player p;
    p=malloc(sizeof(p));
    p->obj.posi.x=posiX;
    p->obj.posi.y=posiY;
    p->obj.life=100;
    p->touched=0;
    p->obj.damage=5;
    p->obj.friend=true;
    return p;
}

Like this every compile perfectly.
But if I had 2 elements in my structure,

struct Player{
   Object obj;
   int touched;
   int frequency;
   int lastTouch;
};
typedef struct Player *Player;

I got this error message at the execution (The code compile perfectly) :

malloc(): invalid size (unsorted)
Aborted (core dumped)

I don't understand why I got this error message, because I don't use yet these two new variables.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱你不解释 2025-01-27 03:58:08

您可能应该按原样定义player将其定义。 Like this:

struct Player{
   Object obj;
   int touched;
};
typedef struct Player Player_t;

Then do this:

Player_t *createPlayer(int posiX,int posiY){
    Player_t *p = malloc(sizeof(Player_t));
    p->obj.posi.x=posiX;
    p->obj.posi.y=posiY;
    p->obj.life=100;
    p->touched=0;
    p->obj.damage=5;
    p->obj.friend=true;
    return p;
}

As a side note: It is never a good idea to typedef pointers as you can never know if it is a pointer or not from its name or declaration elsewhere. You should at the very least explicitly say that it is a pointer like this:

typedef struct Player *PlayerPtr;

or

typedef struct Player *Player_p;

Instead of defining Player as a pointer, you should probably define it as is. Like this:

struct Player{
   Object obj;
   int touched;
};
typedef struct Player Player_t;

Then do this:

Player_t *createPlayer(int posiX,int posiY){
    Player_t *p = malloc(sizeof(Player_t));
    p->obj.posi.x=posiX;
    p->obj.posi.y=posiY;
    p->obj.life=100;
    p->touched=0;
    p->obj.damage=5;
    p->obj.friend=true;
    return p;
}

As a side note: It is never a good idea to typedef pointers as you can never know if it is a pointer or not from its name or declaration elsewhere. You should at the very least explicitly say that it is a pointer like this:

typedef struct Player *PlayerPtr;

or

typedef struct Player *Player_p;
秋意浓 2025-01-27 03:58:08

在线上,

p=malloc(sizeof(p));

您仅在为整个结构分配时仅为指针分配。

您应该做:

p=malloc(sizeof *p);

添加*以放置指针并获取结构类型。

不建议使用typedef掩盖typedef struct player *player;之类的指针,因为它可能会引起这种混乱。

At the line

p=malloc(sizeof(p));

you are allocating only for a pointer while allocating for the whole structure is required.

You should do:

p=malloc(sizeof *p);

Add * to dereference the pointer and get the structure type.

Using typedef to obscure pointers like typedef struct Player *Player; is not recommended because it can cause this type of confusion.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文