我很难在类项目的代码中找到分段错误(这部分未分级)。我正在为一个操作系统类实现一个队列,并且在添加函数中遇到了分段错误。
void AddQueue(QElem * head, QElem * item) {
printf("WHEREn");
if(head == NULL){
printf("THEn");
head = item;
//item->next = item;
//item->prev = item;
}
else{
printf("$^&*n");
(head->prev)->next = item;
printf("AREn");
item->prev = (head->prev);
printf("YOUn");
item->next = head;
printf("FAILINGn");
head->prev = item;
}
printf("!?!?!?n");
}
我有一个测试函数,我从另一个类调用它。。。
void TestAddQueue()
{
printf("********************************************n");
printf("Begin testing the add test functionn");
printf("********************************************n");
QElem * queue;
InitQueue(queue);
for(int i = 0; i < 10; i++)
{
printf("Adding element %dn", i+1);
QElem * newElem = NewItem();
printf("Changing payload valuen");
newElem->payload = i+100;
printf("Adding to the queuen");
AddQueue(queue, newElem);
printf("Item added, payload value = %dn", queue->payload);
printf("The previous payload = %dn", queue->prev->payload);
}
for(int i = 0; i < 10; i++)
{
printf("Rotating list", i+1);
RotateQ(queue);
printf("Printing element %dn", i+1);
printQElem(queue);
}
}
这是NewItem函数。。。
QElem * NewItem()
{
// just return a new QElem struct pointer on the heap
QElem * newItem = calloc(1,sizeof(QElem));
newItem->next = newItem;
newItem->prev = newItem;
newItem->payload = -1;
return newItem;
}
这是运行程序的输出。。。
********************************************
Begin testing the add test function
********************************************
Adding element 1
Changing payload value
Adding to the queue
WHERE
THE
!?!?!?
Segmentation fault
现在,传递到add函数的头指针应该是NULL,因为它被发送到一个初始化器函数,该函数只将指针的值设置为NULL,所以我认为这不会引起我的问题。
我的猜测是下面这句话引起了这个问题。。。
printf("Item added, payload value = %dn", queue->payload);
可能当我试图获取有效负载值时,我试图访问的结构已不存在,或者队列指针被移动到无效空间。如有任何反馈或向正确方向推动,我们将不胜感激。
附带说明:这是在Unix服务器环境(bash)中编译的,目前我无法访问IDE来调试和查看变量。
在C中,参数通过值传递,这意味着它们被复制。更改副本当然不会更改原始副本。
因此,在AddQueue
函数中,变量head
是一个副本,您可以随意更改,最初传递给函数的变量根本不会更改。
为了能够更改参数,您需要通过引用,而C没有,但它可以通过使用指针来模拟。当然,这意味着要通过引用传递指针,必须将指针传递给该指针。
所以对于你的代码来说,它就像
void AddQueue(QElem ** head, QElem * item) {
if(*head == NULL){
*head = item;
}
...
}
...
AddQueue(&queue, newElem);
上面的更改首先使AddQueue
取一个指向QElem
的指针,从而使其模仿引用传递习惯用法。要使用原始指针,请使用解引用运算符*
,它会为您提供指针指向的值(在本例中为原始指针)。然后,要真正将指针传递给指针,必须在指针变量上使用运算符&
的地址。
head = item
对AddQueue
函数外发生的事情没有任何影响。如果将空指针作为head
传递给AddQueue
,则在AddQueue
完成后,该指针仍将为空。
多亏了@Joachim,我的队列能够按预期运行。请参阅下面的重构代码。
首先是add函数。。。
////////////////////////////////////////////////////////////////////////////////
//
// Add Queue
//
// Adds a queue item, pointed to by `item`, to the end of the queue pointed
// to by `head`.
//
// Note: Tested 2-12-2015 using proj_1.c tests. PASSED -Dave
//
////////////////////////////////////////////////////////////////////////////////
int AddQueue(QElem ** head, QElem ** item) {
//If the queue is empty...
if(*head == NULL){
//Point the head to the item. The new item's next/prev were initialized
//to point to itself already.
*head = *item;
}
//If there are already elements in the queue...
else{
// insert the new element at the end of the list (just to the left
// of the head) setting the next and previous values of the
// appropriate nodes to the new values.
((*head)->prev)->next = *item;
(*item)->prev = ((*head)->prev);
(*item)->next = *head;
(*head)->prev = *item;
}
return TRUE;
}
接下来是新项目功能。。。
////////////////////////////////////////////////////////////////////////////////
//
// New Item
//
// Returns a pointer to a new queue element created in heap memory.
//
// Note: Calloc is a more precise way of allocating, but is basically the
// same as malloc, the 1 denotes how many of the item to reserve mem for.
// -Dave
//
// Note: Tested 2-12-2015 using proj_1.c tests. PASSED -Dave
//
////////////////////////////////////////////////////////////////////////////////
QElem * NewItem()
{
// just return a new QElem struct pointer on the heap with values initialized
QElem * newItem = calloc(1,sizeof(QElem));
newItem->next = newItem;
newItem->prev = newItem;
newItem->payload = -1;
return newItem;
}
现在对于测试添加功能。。。
////////////////////////////////////////////////////////////////////////////////
//
// A test function for the add function. It will create a queue of items
// and attempt to iterate through them and print the value of the payload
//
////////////////////////////////////////////////////////////////////////////////
void TestAddQueue(QElem ** queue){
printf("********************************************n");
printf("Begin testing the add test functionn");
printf("********************************************n");
InitQueue(&(*queue));
for(int i = 0; i < 10; i++)
{
printf("Adding element %dn", i+1);
QElem * newElem = NewItem();
printf("Changing payload valuen");
newElem->payload = i+100;
printf("Adding to the queuen");
AddQueue(&(*queue), &newElem);
printf("Item added, payload value = %dn", newElem->payload);
printf("The previous payload = %dn", (*queue)->prev->prev->payload);
}
}