gpt4 book ai didi

C 中带有缓冲环的 Char 链表

转载 作者:行者123 更新时间:2023-11-30 16:51:47 26 4
gpt4 key购买 nike

我有一个服务器和一个客户端。客户端可以通过输入 ADD:Joe:2 来发送添加到列表之类的请求,它应该添加到列表中。键 = 乔值 =2。

我正在尝试将键:值添加到使用最多 5 个元素的缓冲环的链接列表中。当链表达到元素的最大大小(5)并且输入新的 key:value 时,最旧的元素将被新元素覆盖。

每次我添加它似乎都添加得很好,但是当达到最大大小并尝试删除第一个 KEY:VALUE 时,它就会崩溃。

添加到列表:

 void push_item(struct item** Front,struct item** Rear,char *new_key,char *new_value,int newsockfd)
{
char buffer [50] = {0};

struct item* new_node = malloc(sizeof(struct item));
strcpy(new_node->value,new_value);
strcpy(new_node->key,new_key);
//new_node->next = NULL;
if(p_size==5)
{


char *itemVal;
char *itemKey;
/* if queue is empty */
if ( *Front == NULL )
{
printf ( "List is empty");
}
else
{
if (*Front == *Rear)
{
strcpy(itemVal,(*Front)->value);
strcpy(itemKey,(*Front)->key);
free(*Front);
*Front = NULL ;
*Rear = NULL ;
}
else
{
//delete node
new_node = *Front;
strcpy(itemVal,new_node->value);
strcpy(itemKey,new_node->key);
*Front=(*Front)->next;
(*Rear)->next=*Front;
free(new_node);
}
printf("Node deleted Key: %s Value: %s",itemKey,itemVal);
}
}
else
{
*Rear=new_node;
(*Rear)->next=new_node;

if(*Front==NULL)
{
(*Front)=new_node;
p_size++;
}
else
{
(*Rear)->next=new_node;
p_size++;
printf("Elements in List: %d\n",p_size);
}

}

sprintf(buffer,"Added");
int num_bytes = write(newsockfd, buffer, strlen(buffer));
if (num_bytes < 0)
{
fprintf(stderr, "Thread ERROR: write() failed\n");
}
}

服务器:

struct item *Front = NULL;
struct item *Rear = NULL;
char buffer[BUFFER_SIZE] = {0};

void* handle_client(void *socket)
{
int newsockfd = (int)socket;
pthread_t thread_id = pthread_self();

printf("----------\nThread %lu using socket %x\n", (unsigned long)thread_id, newsockfd);

/* Start communicating */
int num_bytes = read(newsockfd, buffer, BUFFER_SIZE-1);
if (num_bytes < 0) {
fprintf(stderr, "Thread %lu ERROR: read() failed\n", (unsigned long)thread_id);
return NULL;
}
printf("Thread %lu recieved request\n", (unsigned long)thread_id);

if(strncmp(buffer,"ADD",3)==0)
{
char* key = buffer+4;
int charIndex;
char *new_value = strchr(buffer+4 , ':')+1;
charIndex = (int)(new_value-buffer)-1;
buffer[charIndex] = '\0';

push_item(&Front,&Rear,key,new_value,newsockfd);

printf("Value:%s Key:%s\n",new_value,key);
}

编辑:CMD 的图像: Left cmd is the client and the right one is the server

p_size表示链表中当前元素的数量。进入 if 条件 P_size == 5 后崩溃。

最佳答案

在查看 push_item() 函数以查找 if(p_size==5) 情况下的问题时,错误发生在将节点的数据写入到被删除。

错误 1 - 临时 itemValitemKey 被声明为 char * 但从未分配给数组。

// To do
strcpy(itemVal,(*Front)->value);
strcpy(itemKey,(*Front)->key);
// or
strcpy(itemVal,new_node->value);
strcpy(itemKey,new_node->key);

Both char *itemVal; and char *itemKey; shall be allocated.

// Local variables (STR_SIZE has to be adjusted)
char itemVal[STR_SIZE+1];
char itemKey[STR_SIZE+1];

警告 1 - 但解决该问题将揭示使用链表时环形缓冲区算法中的故障。

In the push_item() function, the writing/push pointer *Front and the reading/pop pointer *Rear are managed independently. When the Ring Buffer is full (if(p_size==5)) the new item is deleted instead of the oldest one.

关于C 中带有缓冲环的 Char 链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41594544/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com