【问题标题】:Structs and memory allocation结构和内存分配
【发布时间】:2017-12-03 04:46:46
【问题描述】:

我可以只为这个结构分配内存而不为其中的每个单独项目分配内存(在 C 中)吗?

    typedef struct _game {
        int grid[SIZE][SIZE];
        int array[10];
        Player player[MAX_PLAYERS];
        int currentPlayer;
    } game;

这些在单独的头文件中(并且 player 是在与游戏相同的文件中实现的结构):

    typedef struct _game *Game;
    typedef struct _player *Player;

我只是想知道,当我创建一个游戏的新实例时,我是否需要为游戏中的每个玩家(4 个玩家)分配内存(例如 calloc 或 malloc)?我想,既然我已经在游戏结构中有一个玩家数组(或指向玩家的指针)(并且这个数组大小没有改变),那么我只需要为游戏结构本身分配内存。是这样吗?如何使用内存分配?特别是它如何与结构一起使用?我是否也需要为结构中的所有其他项目分配内存?

【问题讨论】:

  • 您还需要为播放器分配内存。看到这个:stackoverflow.com/questions/2278933/…
  • 为什么不显示分配代码的示例?另请注意,将指针隐藏在 typedef 后面通常是一种糟糕的做法。
  • @DavidBowling 只是出于兴趣,为什么它被认为是不好的做法?而我应该怎么做呢?
  • 您可以查看我的答案中提供的阅读列表,以了解为什么您不应该键入定义指针。长话短说,当我们想将它与const 限定符一起使用时,这是很成问题的。

标签: c memory memory-management struct


【解决方案1】:

按照结构的设计方式,您确实需要分配个人玩家。

解决方案-1

你会做的

Game game = malloc(sizeof *game);

那么你有MAX_PLAYER_Player 指针变量。所以它会像

  for(size_t  i = 0; i<MAXPLAYERS; i++)
      game->player[i]= malloc(sizeof *game->player[i]);

不鼓励在typedef 下隐藏指针。这是一种不好的做法。您还需要检查malloc() 的返回值,并在使用完成后释放动态分配的内存。

你做了什么?

Player player[MAX_PLAYERS]; 是指针数组,而不是 _player 变量数组。这就是为什么需要为每个指针变量分配一些内存的原因。这样您就可以将玩家数据存储到其中。


解决方案-2

你可以这样做:

typedef struct _game {
    int grid[SIZE][SIZE];
    int array[10];
    Player player;
    int currentPlayer;
} game;

然后分配10个播放器变量内存,并将malloc返回的值赋值给player

Game game = malloc(sizeof *game);
..
game->player = malloc(sizeof *game->player *MAX_PLAYERS);
..

解决方案-3

typedef struct _game {
    int grid[SIZE][SIZE];
    int array[10];
    struct _player player[MAX_PLAYERS];
    int currentPlayer;
} game;

那么你就不需要为玩家单独分配了。它里面已经有MAX_PLAYERstruct _player 变量。


当您询问typedef 时,您可以简单地这样做

typedef struct _game {
    int grid[SIZE][SIZE];
    int array[10];
    Player player[MAX_PLAYERS];
    int currentPlayer;
} game;

...
...
game *mygame = malloc(sizeof *mygame);

这可以达到目的 - 让您无需输入 struct ...,而且它更易读和易于理解。

阅读清单

  1. Is it a good idea to typedef pointers?
  2. Is typedef'ing a pointer type considered bad practice?

【讨论】:

  • 感谢您的洞察力。出于好奇,为什么在 typedef 下隐藏指针是不好的做法?解决方案是不使用 typedef 吗?我在其他地方读到 typedef 可能有问题,不被鼓励。我只是通过界面(使用函数)访问游戏元素。但这仍然不是正确的方法吗?
  • @qwertyuiop.: 你检查过给定的两个参考资料吗?你会在那里得到详细的解释。在这里,您可以简单地拥有 typedef 原始结构,然后当您需要指向它的指针时,只需声明它而不使用指针作为 typedef。这样就解决了问题。
  • @qwertyuiop.:我已经修改了答案,您可以看到如何在不为指针创建 typedef 的情况下简单地完成它。
  • Game game = malloc(sizeof *game); -- 看看这行代码有多混乱;这应该作为 OP 的一个很好的例子,说明为什么不使用 typedef 这样的指针。 @qwertyuiop -- 请注意 typedef 确实有合法用途。一种很好的用途是从函数返回复杂类型。 typedefing 部分复杂的返回类型(例如,可能是数组)可以显着简化函数原型。
  • @DavidBowling.: 是的,在 struct 中隐藏指针基本上会导致大部分时间出现错误(且不可读)代码。因为您总是需要取消对它的引用才能获得结果,所以代码失去了可读性。并且每次您打开代码时有时会稍后。在您意识到它是指向结构的指针之前需要一些时间。你是对的,OP 应该从中得到一个想法。
【解决方案2】:

我是否也需要为结构中的所有其他项目分配内存?

不,结构内的所有项目都会自动分配内存。

这是同时清除结构内存的分配示例:

#include <stdio.h>

#define SIZE 10
#define MAX_PLAYERS 20

   typedef struct _player {  
        char name[10];
        int i;
    } Player;


    typedef struct _game {
        int grid[SIZE][SIZE];
        int array[10];
        Player player[MAX_PLAYERS];
        int currentPlayer;
    } game;

int main()
{
   game *g = calloc(1, sizeof (game));

   return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-20
    • 2016-01-27
    • 2022-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多