gpt4 book ai didi

c - 双重免费或损坏(fasttop)错误c

转载 作者:行者123 更新时间:2023-12-03 12:08:51 24 4
gpt4 key购买 nike

我在下面的代码中收到了双重免费或损坏(fasttop)。似乎无法弄清楚哪个指针正在创建此错误。看起来错误是在 echo_cnt 函数的 while 循环中产生的。谢谢!

    static void init_echo_cnt(void) {

sem_init(&mutex, 0, 1);
byte_cnt = 0;

}

void echo_cnt(int connfd) {
int n;

FILE *fp = fopen("words.txt", "r");
static pthread_once_t once = PTHREAD_ONCE_INIT;
pthread_once(&once, init_echo_cnt);
char *buf = calloc(MAXLINE, sizeof(char));

while((n=readLine(connfd, buf, MAXLINE)) != 0) {
char *str = fgets(buf, sizeof buf, fp);
char *token;
token = strtok(str, " ");
char *result = malloc(strlen(token +64));
while (token != NULL){
printf("In the while loop\n");
if (spellcheck(token, fp)==1){
strcat(result, token);
strcat(result, "OK");
}
else{
strcat(result, "MISPELLED");
}
ssize_t kick = write(connfd, result, MAXLINE);

free(result); }

}
close(connfd);
}

void *thread(void *vargp) {
int connfd;
pthread_detach(pthread_self());
printf("World\n");

while(1) {

connfd=sbuf_remove(sbuf); //remove socket
echo_cnt(connfd); //service client

}
close(connfd); //close socket
}

最佳答案

我只检查您认为错误所在的部分,并且我看到您第一次获得 token ,并且在循环中永远不会抓取另一个 token 。只需在 while 循环中获取另一个 token 。

例如:

    /* get the first token */
token = strtok(str, s);

/* walk through other tokens */
while( token != NULL ) {
printf( " %s\n", token );

token = strtok(NULL, s);
}

此示例取自: https://www.tutorialspoint.com/c_standard_library/c_function_strtok.htm

关于c - 双重免费或损坏(fasttop)错误c,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47245000/

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