C语言 “sizeof”对不完整类型列表结构 C 的无效应用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17211345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
invalid application of 'sizeof' to incomplete type list struct C
提问by joseph
I'm trying to implement an replacement algorithm that deals with page faults. So i'm trying to creat a circular linked list using malloc and im getting the following error: "invalid application of sizeof' to incomplete typepageInMemory'.following is the code:
我正在尝试实现一种处理页面错误的替换算法。因此,我正在尝试使用 malloc 创建一个循环链表,但出现以下错误:“ sizeof' to incomplete typepageInMemory 的应用程序无效。以下是代码:
typedef struct {
int use;
int reference;
int free;
struct pageInMemory* next;
} pageInMemory;
int main()
{
int i;
struct pageInMemory* start, *nn, *temp, *hand;
start = NULL;
for(i=0; i< pNum; i++)
{
nn = (struct pageInMemory *)malloc(sizeof(struct pageInMemory));
nn->use = 0;
nn->free = 1;
if(start==NULL)
{
nn->next = nn;
start =nn;
}
else
{ // sfhsdifhsdifj sdijfjsd
temp = start;
while(temp->next != start)
{
temp = temp->next;
}
temp->next = nn;
nn->next = start;
start = nn;
}
}
hand = start;
temp = start;
while(temp->next != start->next)
{
printf("%d\n", temp->use); //hi
}
return 0;// bye
}
so am i not supposed to use malloc this way ?
所以我不应该这样使用 malloc 吗?
回答by Dayal rai
change your struct definition as
将您的结构定义更改为
struct pageInMemory{
int use;
int reference;
int free;
struct pageInMemory* next;
};
to get your code working. And just for your info do not typecast void* coming from malloc.
让你的代码工作。并且只是为了您的信息,不要对来自 malloc 的 void* 进行类型转换。
回答by alk
It should be either:
它应该是:
typedef struct pageInMemory_s {
int use;
int reference;
int free;
struct pageInMemory_s * next;
} pageInMemory;
pageInMemory* start, *nn, *temp, *hand;
...
nn = malloc(sizeof(pageInMemory));
or
或者
struct pageInMemory {
int use;
int reference;
int free;
struct pageInMemory* next;
};
struct pageInMemory* start, *nn, *temp, *hand;
...
nn = malloc(sizeof(struct pageInMemory));
A third variant would be:
第三种变体是:
typedef struct pageInMemory {
int use;
int reference;
int free;
struct pageInMemory * next;
} pageInMemory;
pageInMemory* start, *nn, *temp, *hand;
...
For this 3rd option you can use:
对于第三个选项,您可以使用:
nn = malloc(sizeof(pageInMemory));
or
或者
nn = malloc(sizeof(struct pageInMemory));
This latter variation I feel is very irritating as there is one name for two differentthings:
我觉得后一种变化非常令人恼火,因为两种不同的事物有一个名称:
- The structure definition/declaration
struct pageInMemory - The type definition/declaration
pageInMemory
- 结构定义/声明
struct pageInMemory - 类型定义/声明
pageInMemory
I would not recommend to use this 3rd option, but the second.
我不建议使用第三个选项,而是第二个。
回答by VoidPointer
pageInMemoryis itself defined as type. So instead of this,
pageInMemory本身被定义为类型。所以,而不是这个,
nn = (struct pageInMemory *)malloc(sizeof(struct pageInMemory))
Use this,
用这个,
nn = (pageInMemory *)malloc(sizeof(pageInMemory))

