我正在尝试编写一个链接列表,该列表由两个函数访问,一个创建节点并附加到列表,另一个搜索并从列表中删除。我还有一个单独的函数,它初始化列表和每个节点的内存。以下是我的代码片段:
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()函数按预期工作,并将正确的消息存入列表。但是,当receive函数尝试扫描列表以进行检索时,它会将列表清空!我不确定我在这里缺少什么。我知道这是非常小的......但我会感激一些帮助!
答案 0 :(得分:1)
您
strcpy(msg1, msg);
不会很好用。请记住,字符串由第一个零字节终止,因此如果结构包含任何零(如int
包含任何包含零的字节,或指针,或只包含零的任何内容){{1将停止。
不要将字符串函数与非字符串数据一起使用。在这种情况下,我猜您可能需要memcpy
。
顺便提一句,您应该从strcpy
调用中收到警告,因为一个指针不正确。另外,为什么要分配所有strcpy
个字节而不是1024
?