gpt4 book ai didi

c - 无法定位 "dereferencing pointer to incomplete type"错误的来源

转载 作者:行者123 更新时间:2023-12-04 12:27:21 29 4
gpt4 key购买 nike

作为大学类(class)作业的一部分,我需要用 C(不是 C++)编写一个 CPU 调度模拟器。我在使用 GCC 编译时遇到了一个问题,它给了我几个关于“取消引用指向不完整类型的指针”的错误。所有错误都是同一代码的结果,这让我认为该代码存在问题。

违规代码是:

//Push a record of this state change to the back of the simulation's history list
listPushBack(sim->history, (void*)newRecord(p->pid, sim->currentTime, wait, ready));

这与跟踪模拟历史以供稍后分析有关。记录定义为:

//History.h

typedef struct record* Record;

//History.c

typedef struct record recordType;

struct record{
long time;
int pid;
State oldState;
State newState;
};

Record newRecord(int pid, long time, ProcessState oldState, ProcessState newState){
Record r = (Record)malloc(sizeof(recordType));
if(r == NULL){
fprintf(stderr, "History.c:newRecord:Failed to allocate memory for new Record\n");
//This is serious, abort execution
exit(-1)
}
r->time = time;
r->pid = pid;
r->oldState = oldState;
r->newState = newState;
return r;
}

其中 ProcessState 定义为:

//Process.h

typedef enum process_state ProcessState;

enum process_state{
arrive = 0,
ready = 1,
run = 2,
wait = 3,
done = 4
};

我玩了一下,得到了同样的错误:

Process p = (Process)listGetAt(sim->waitingQueue, i);
ProcessState old = p->state;
listPushBack(sim->history, (void*)newRecord(p->pid, sim->currentTime, old, p->state));

谢天谢地,这不会再过一个星期,所以我有时间玩,但我希望有人能在我浪费太多时间搞砸之前给我指明正确的方向。

编辑:按评论中出现的顺序回答问题,

//DoubleLinkList.c

bool listPushBack(DoubleList l, void* data){
return listInsert(l, data, -1);
}
bool listInsert(DoubleList l, void* data, int pos){
int index = pos;
//If value is negative, convert to a index relative to the end of the list
if(index < 0){
index = listSize(l) + index + 1;
}
//Check index bounds
if(index > listSize(l) || index < 0){
fprintf(stderr, "DoubleLinkList.c:listInsert:Insert index %i out of bounds\n", pos);
//This is not serious enough to warrent an abort
return false;
}
//Data is null
if(data == NULL){
fprintf(stderr, "DoubleLinkList.c:listInsert:Data value for doubly linked list node cannot be NULL\n");
//This is not serious enough to warrent an abort
return false;
}
Node insertNode = newNode(data);
//Case: End of list
if(index == listSize(l)){
l->tail->next = insertNode;
insertNode->prev = l->tail;
l->tail = insertNode;
l->size++;
return true;
}
//Case: Start of list
else if(index == 0){
l->head->prev = insertNode;
insertNode->next = l->head;
l->head = insertNode;
l->size++;
return true;
}
//Case: Middle of list
Node node = l->head;
//Scan through list to reach index pos
int i;
for(i = 0; i < index; i++){
if(node == NULL){
fprintf(stderr, "DoubleLinkList.c:listGetPosition:NULL encoutered unexpectedly while traversing doubly linked list at index %i\n", i);
//This is a serious problem, abort execution
exit(-1);
}
node = node->next;
}
//Insert before Node at index pos
insertNode->next = node;
insertNode->prev = node->prev;
node->prev->next = insertNode;
node->prev = insertNode;
l->size++;
return true;
}

是的,过程是:

typedef process_Struct* Process

模拟声明:

//Simulation.c
struct simulation{
SimType type;
long currentTime;
unsigned int totalProcesses;
DoubleList arriveQueue;
DoubleList readyQueue;
DoubleList waitingQueue;
DoubleList doneQueue;
Process running;
DoubleList history;
};

抛出问题的方法是 runSimulation(Simulation sim) 其中:

typedef simulation* Simulation;

runSimulation 在 Simulation.c 中声明

确切的错误信息是:source/Simulation.c:154:50: error: dereferencing pointer to incomplete type这就是为什么这被证明如此烦人,它没有提供任何更多信息,即使使用 -verbose、-g 和其他一些调试标志也是如此。

我意识到我不应该使用 typdef 指针,但是,愚蠢的是,教授将其作为作业的要求。去引用:“如果可能,使用 typedef 来定义指向结构的指针。这样 TA 就可以更轻松地阅读您的模拟代码。”我对此非常恼火,因为这是一个糟糕的主意,助教应该能够阅读代码。

最佳答案

您的评论表明您正在 history.c 源文件中定义 struct record。如果您在任何其他源文件中使用 struct record,那么您应该在 history.h 中定义它,否则当您尝试使用指向另一个文件中的 struct record 的指针。

关于c - 无法定位 "dereferencing pointer to incomplete type"错误的来源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13215133/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com