c - 不确定是什么导致我的链表数据在函数之间丢失

标签 c

我正在尝试编写一个由两个函数访问的链表,一个函数创建节点并将其追加到列表中,另一个函数搜索并从列表中删除节点。我还有一个单独的函数,用于初始化列表和每个节点的内存。下面是我的代码片段:

static mbox *send_recv;    // global statement

void mbox_create(mbox **mb)  // list creation
{
    *mb = malloc(sizeof(mbox));
    sem_init(&(*mb)->sem_mbox, 1);
    (*mb)->msg_queue = NULL;
}

void send(int tid, char *msg, int len)
{
    if (flag == 0)
    {
        mbox_create(&send_recv);
        flag = 1;
    }

    char* msg1 = malloc(1024);
    int a = len;

    struct msg *temp = send_recv->msg_queue;
    struct msg *temp1 = GetNewMsgNode(a);

    // copy message into a temp variable
    strcpy(msg1, msg);

    //lock the send-recv mailbox
    sem_wait(send_recv->sem_mbox);

    if(temp == NULL)
    {
        temp1->sender = tid;
        strcpy(temp1->message, msg1);
        temp1->next = NULL;

        temp = temp1;
    }
    else
    {
        // traverse to the last node again of the mailbox and insert the new node
        while(temp->next != NULL)
            temp = temp->next;

        // set the sender's tid to the newly deposited message
        temp = temp->next;

        temp1->sender = tid;
        strcpy(temp1->message, msg1);
        temp1->next = NULL;

        temp = temp1;
    }

    // unlock the send-recv mailbox
    sem_signal(send_recv->sem_mbox);

}

void receive(int *tid, char *msg, int *len)
{
    struct msg *temp = send_recv->msg_queue;
    struct msg *temp1;
    char *msg1 = malloc(1024);

    if(*tid == 0)       // if tid = 0, receive the first message from the mailbox if the mailbox is not empty
    {
        // lock the send-recv mailbox
        sem_wait(send_recv->sem_mbox);

        if(flag == 0)       // if the send() was never called, means the mailbox is empty
        {
            *len = 0;
            *tid = 0;
        }
        else if(temp != NULL)
        {
            *len = temp->length;
            strcpy(msg1, temp->message);

            if(send_recv->msg_queue->next != NULL)
                send_recv->msg_queue = send_recv->msg_queue->next;

        }
        // unlock the send-recv mailbox
        sem_signal(send_recv->sem_mbox);

        // copy the message back into the original pointer, ie, msg
        strcpy(msg, msg1);
    }
    else      // search the mailbox for a message with the matching tid in the sender's field and retrieve that message, if mailbox is not empty
    {
        // lock the send-recv mailbox
        sem_wait(send_recv->sem_mbox);

        if(flag == 0)       // if the send() was never called, means the mailbox is empty
        {
            *len = 0;
            *tid = 0;
        }
        else
        {
            if(send_recv->msg_queue->sender == *tid)
            {
                *len = send_recv->msg_queue->length;
                strcpy(msg1, send_recv->msg_queue->message);

                // delete the above message from the mailbox

                if(send_recv->msg_queue->next != NULL)
                    send_recv->msg_queue = send_recv->msg_queue->next;

                free(temp);

            }
            else
            {
                // traverse the mailbox queue to find a matching message
                while(temp->next != NULL)
                {
                    if(temp->sender == *tid)
                    {
                        *len = temp->length;
                        strcpy(msg1, temp->message);

                        // delete the above message from the mailbox

                        temp1 = temp->next;
                        temp->length = temp1->length;
                        temp->sender = temp1->sender;
                        temp->receiver = temp1->receiver;
                        strcpy(temp->message, temp1->message);
                        temp->next = temp1->next;

                     //   free(temp1);

                    }
                    else
                        temp = temp->next;

                }

            }

        }
        // unlock the send-recv mailbox
        sem_signal(send_recv->sem_mbox);

        // copy the message back into the original pointer , ie, msg
        strcpy(msg, msg1);
    }
}

send() 函数按预期工作并将正确的消息存入列表中。然而,当接收函数尝试扫描列表进行检索时,它发现列表是空的!我不确定我在这里缺少什么。我知道这是非常小的事情......但我希望在这里得到一些帮助!

最佳答案

你的

strcpy(msg1, msg);

不会很好地工作。请记住,字符串以第一个零字节终止,因此如果结构包含任何零(例如 int 以及任何包含零的字节,或指针,或任何可以包含零的内容) strcpy 将停止。

不要将字符串函数与非字符串数据一起使用。在这种情况下,我猜您可能需要 memcpy .

顺便说一句,您应该从 strcpy 调用中收到警告,因为一个指针不正确。另外,为什么分配所有 1024 字节而不是 sizeof(struct msg)

关于c - 不确定是什么导致我的链表数据在函数之间丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23614883/

相关文章:

c - 将字符串设置为C中的子字符串

c - 比 rand() 快?

c++ - 如何在构建时将 JSON 文件/对象包含在 C++ 中

c - 如何解决随机给出错误数字的问题

c - 在c中使用链表排序

c - 可能的 GCC 错误 : Long arguments shortened when linking a library to project. C\C++

c - GtkAlignment 一次只能包含一个小部件

c - 防止数组接受重复值

c - ISO C Void * 和函数指针

c - Lseek SEEK_END 不工作?