作为我为大学课程做的作业的一部分,我需要用 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: dereference pointer to incomplete type 这就是为什么这很烦人,它没有提供更多信息,即使使用 -verbose, -g,和其他一些调试标志。
我意识到我不应该输入 typdef 指针,但是,愚蠢的是,教授把它作为作业的要求。引用:“如果可能,请使用 typedef 定义指向结构的指针。这样 TA 可以更轻松地阅读您的模拟代码。” 我对此感到非常恼火,因为这是一个糟糕的主意,助教应该能够阅读代码。