分配内存以保存C中的结构数组

时间:2015-11-14 16:02:07

标签: c arrays struct

我试图分配内存来保存结构数组

SERVER* topology = malloc(sizeof(struct SERVER*)* 10 );
    for (int i = 0; i < 10; ++i)
    {
        topology[i] = malloc(sizeof(struct SERVER));
    }
    PATH* paths = malloc(sizeof(struct PATH*)*10);
    for (int i = 0; i < 10; ++i)
    {
        paths[i] = malloc(sizeof(struct PATH));
    }

这些是我的结构

typedef struct{
    int id;
    char ip_addr[MAX_IP + 1];
    int port;
}SERVER;

typedef struct{
    int server1;
    int server2;
    int weight;
}PATH;

然后在我的代码中我尝试使用

释放它
for (int i = 0; i < 10; ++i)
   {
     free(paths[i]);
   }
   free(paths);

   for (int i = 0; i < 10; ++i)
   {
     free(topology[i]);
   }
   free(topology);

我一直收到以下错误

error: invalid application of 'sizeof' to an incomplete type 'struct SERVER'
                topology[i] = malloc(sizeof(struct SERVER));
                                     ^     ~~~~~~~~~~~~~~~
:18:42: note: forward declaration of 'struct SERVER'
        SERVER* topology = malloc(sizeof(struct SERVER*)* 10 );
                                                ^
:26:21: error: invalid application of 'sizeof' to an incomplete type 'struct PATH'
                paths[i] = malloc(sizeof(struct PATH));
                                  ^     ~~~~~~~~~~~~~
:23:37: note: forward declaration of 'struct PATH'
        PATH* paths = malloc(sizeof(struct PATH*)*10);

...........

c:97:11: error: passing 'PATH' to parameter of incompatible type 'void *'
         free(paths[i]);
              ^~~~~~~~
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/stdlib.h:143:18: note: passing argument to parameter here
void     free(void *);
                    ^
:103:11: error: passing 'SERVER' to parameter of incompatible type 'void *'
         free(topology[i]);
              ^~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/stdlib.h:143:18: note: passing argument to parameter here
void     free(void *);

我一般都是C的新手。感谢任何帮助。

2 个答案:

答案 0 :(得分:3)

试试这个:

X  A     B      C
1  0.75  0.25   0
2  0     0      1
3  0.25  0.25   0.5

你知道,你使用typedef struct SERVER { .... } SERVER; typedef struct PATH { .... } PATH; struct SERVER,但你还没有声明这些。您确实在未命名的结构上使用struct PATH声明了类型SERVER和类型PATH

或者,您可以按原样保留结构,并使用typedefsizeof(PATH*)

答案 1 :(得分:2)

这里有几个问题:

SERVER* topology = malloc(sizeof(struct SERVER*)* 10 );
...
PATH* paths = malloc(sizeof(struct PATH*)*10);
  • 您有一个名为SERVER的类型,它是匿名结构的typedef。您没有定义struct SERVER,因此请使用SERVER代替struct SERVER。这同样适用于PATH
  • 您为SERVER分配10个指针的空间,但是topology是指向SERVER的指针,这意味着它可以充当SERVER的数组,而不是一个SERVER *数组,这就是你使用它的方式。这就是您在调用free时收到错误的原因,因为topology[i]SERVER,而不是SERVER *。同样适用于PATH

为了使其正常工作,您需要按如下方式定义topologypaths

SERVER **topology = malloc(sizeof(SERVER *) * 10 );
PATH **paths = malloc(sizeof(PATH *) * 10);

或者,不是分配指针数组,而是分配单个元素,而是保留当前定义并立即分配整个数组:

SERVER *topology = malloc(sizeof(SERVER) * 10);
PATH *paths = malloc(sizeof(PATH) * 10);

然后像这样清理:

free(topology);
free(paths);
相关问题