【问题标题】:Returning pointers properly正确返回指针
【发布时间】:2018-03-21 10:22:02
【问题描述】:

我在 C 中创建了一个 ADT。它基本上是一个链表 where。列表中的每个节点都有一个存储项目的成员变量。

void *item

我有一个用于添加到列表和删除以及其他的成员函数。

在我的 main.c 文件中,我调用 ListAdd() 函数,该函数接受一个列表并将一个项目添加到下一个可用节点。 ListFirst() 函数转到列表的前面并返回存储在该节点的项目。

return list->item

我的主要功能是存储一个结构,我将其命名为 pcb,作为每个节点的项目。 所以这是我的问题。当我测试 ADT 时,ListFirst() 函数在返回 void *item 之前,具有该项目的所有正确成员数据。但在我的 main.c 文件中,所有信息都不正确。有谁知道为什么会发生这种情况?

我认为由于该项目是无效的 * 我将地址返回给该项目。所以在我的 main.c 文件中,代码如下所示:

typdef struct PCB{
        //member variables 
}pcb;

pcb *readypcb;
readypcb = (pcb *)ListFirst(listHead);
//print out data

所以我认为 readypcb 指针会指向 ListFirst() 返回的项的地址。此外,我将返回值转换为(pcb *),因为我返回了void *

总而言之,在我返回 void *item 之前的行正确打印出项目成员数据。然后在它分配给 readypcb 之后,我再次打印出数据,只是这一次都不正确。有谁知道怎么回事?

这是你们要求的一些额外代码:

我的PCB数据结构的头文件:

typedef enum Priority{

    high,
    medium,
    low

}Priority;

typedef enum State{

    running,
    ready,
    blocked
}State;

typedef struct PCB{

    Priority priority;              //records the priority level of the PCB
    int pid;                    //The process id of the control block
    State state;                    //records the state of the PCB

    char received[200];             //records any messads sent to this PCB
    char replied[200];              //records the messages that another process replies with

}pcb;

然后是我分配返回项目的 main.c 文件:

int checkForReadyProcesses(listPtr highP, listPtr medP, listPtr lowP, pcb *readypcb){
    int retCode = 0;

    //check that the item counts for the ready queues are 0
    if(highP->itemCount > 0){
         readypcb = (pcb *)ListFirst(highP);
        //ListRemove(highP);

        if(TESTING_READY_QUEUES){
            printf("We have a PCB in the high priority queue\n");
        }

    }else if(medP->itemCount > 0){
        readypcb = (pcb *)ListFirst(medP);
        //ListRemove(medP);

        if(TESTING_READY_QUEUES){
            printf("We have a PCB in the medium priority queue\n");
        }

    }else if(lowP->itemCount > 0){
        readypcb = (pcb *)ListFirst(lowP);
        //ListRemove(lowP);

        if(TESTING_READY_QUEUES){
            printf("We have a PCB in the low priority queue\n");
        }

    }else{
        retCode = 1;
    }

    if(TESTING_READYPCB){
        printf("The ready address is: %p\n", readypcb);
        printf("The ready state is: %d\n", readypcb->state);
        printf("The ready priority is: %d\n", readypcb->priority);
        printf("The ready id is: %d\n", readypcb->pid);
    }

    return retCode;

}

最后是 LstFirst():

void *ListFirst(LIST *list){
    void *tempItem;

    list->currentItem = list->firstNode->item;
    list->currentNode = list->firstNode;

    //updates the position
    list->position = first;

    tempItem = ((list->firstNode->item));

    pcb curpcb = *(pcb *)list->firstNode->item;

    printf("the item has the adress: %p\n", list->firstNode->item);
    printf("the item pid is: %d\n", curpcb.pid);
    printf("the item state is: %d\n", curpcb.state);
    printf("the item priority is: %d\n", curpcb.priority);

    return list->firstNode->item;
}

List.h 文件:

typedef struct NODE *nodePtr;
typedef struct List *listPtr;

//define the node structire used for each node in the list except the head node
typedef struct NODE{    
    nodePtr nextNode;       //points to the next node in the list
    nodePtr previousNode;       //points to the previous node in the list
    void *item;         //holds the item for the node
} node;

//define the head structure for the List
typedef struct List{

    nodePtr firstNode;      //points to the first node of the list
    int itemCount;          //holds the number of items in the list
    nodePtr end;            //points to the end of the list
    void *currentItem;      //points to the current item
    nodePtr currentNode;        //points to the current node
    listPtr freedHeadNode;      //used for pointing to freedHead nodes when more than one is available

    //using an enum to represent where the current node is
    enum currentPos{
        beyondEnd = 2,
        end = 1,
        middle = 0,
        beforeStart = -1,
        first = -2,     //this represents the first item ever to be added
    } position;
} LIST;

//FUNCTIONS
LIST *ListCreate();         //creates a new list when called
int ListCount(LIST *list);      //returns the number of items in the list
void *ListFirst(LIST *list);        //returns the first item in the list
void *ListLast(LIST *list);     //returns a pointer to the last item
int ListAdd(LIST *list, void *item);    //adds an item to a list
void *ListCurr(LIST *list);     //points to the current item in the list
void *ListNext(LIST *list);     //points to the next item after the current spot
void *ListPrev(LIST *list);     //points to the previous item before the current node
int ListInsert(LIST *list, void *item); //inserts a node just before the previous node
int ListPrepend(LIST *list, void *item);//adds an item to the begining of the list
int ListAppend(LIST *list, void *item); //adds an item to the end of the list
void *ListRemove(LIST *list);       //removes and returns the current item from the list
void *ListTrim(LIST *list);     //returns and removes the last item of the list
void *ListSearch(LIST *list, int(*comparator)(void *, int), int comparatorArg);
void ListFree(LIST *list, void(itemFree)(void *));

我认为这应该会有所帮助。如果您需要任何其他信息,请告诉我。

【问题讨论】:

  • 您能否发布足够的代码以便我们对其进行测试?例如 ListFirst 函数以及如何分配这些 PCB。
  • 代码不足,请edit您的问题并显示所有相关代码。
  • 我们需要查看ListFirst的代码,所有相关的数据结构以及打印数据的代码。
  • 好的,所以我添加了更多代码。我还发布了我的 ADT 实现文件中的所有函数头。这样,如果你们需要看到其中一个,你们知道我叫它什么,然后我可以为你们抓住它并发布它

标签: c pointers


【解决方案1】:

虽然我没有看到你的所有声明,但很可能你通过了 listhead,head 得到了更新,但无法返回。它无法返回的原因是因为您没有传递“双指针”。考虑:

void example(list *head) {
    if (head==NULL) head= malloc(sizeof(list));
    //...

这里你将内存分配给head,但head是一个本地变量;调用者无法看到对其进行的任何更改。相反,这样做:

void example(list **head) {
    if (*head==NULL) *head= malloc(sizeof(list));
    //...

然后像这样调用:

list *myList;
example(&myList);

【讨论】:

    猜你喜欢
    • 2019-11-01
    • 2018-03-01
    • 2014-10-25
    • 1970-01-01
    • 1970-01-01
    • 2014-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多