gpt4 book ai didi

C: 如何将经过 malloc 处理的指针传递给函数并释放它们?

转载 作者:太空宇宙 更新时间:2023-11-04 02:28:14 25 4
gpt4 key购买 nike

我正在开发一个使用套接字和线程的客户端-服务器程序。我有一个客户端菜单,我试图从客户端向服务器发送一个字符,然后调用相应的函数。我正在尝试使用指针和 malloc,但我认为我不太了解如何传递和释放它们。我遇到内存泄漏和错误,例如:

./server': double free or corruption (fasttop): 0x00007f37dc000a50 ***

Segmentation fault

这是我的客户端菜单:

char *choice = (char*) malloc(sizeof(char));

do {

// get a string from the server
get_msg(sockfd);

printf("\nFile and Information System\n");
printf("===========================\n");
printf("1. Show IP and Student ID\n");
printf("2. Display server time\n");
printf("3. Display system information\n");
printf("4. List files on server\n");
printf("5. File Transfer\n");
printf("6. Exit\n");
printf("Enter choice: ");
scanf("%s", choice);

//Send input to server
send_menu_choice(sockfd, choice);

switch (*choice)
{
case '1':
get_ip(sockfd);
break;
case '2':
get_time(sockfd);
break;
case '3':
get_and_send_uname(sockfd);
break;
case '4':
get_file_list(sockfd);
break;
case '5':
printf("File transfer not yet implemented\n");;
break;
case '6':
printf("Exiting...\n");
break;
default:
printf("Invalid choice\n");
}

//if(menu_choice != NULL)
//{
// free(menu_choice);
//}

} while (*choice != '6');

if (choice != NULL)
{
free(choice);
}

将菜单选项发送到服务器:

void send_menu_choice(int socketNumber, char *choice)
{
printf("Sending menu choice...\n");
size_t n = strlen(choice) + 1;

writen(socketNumber, (unsigned char *) &n, sizeof(size_t));
writen(socketNumber, (unsigned char *) choice, n);

printf("Sent choice: %s\n\n", choice);
}


服务器端:

char *menu_choice = (char*) malloc(sizeof(char));

do {

printf("Waiting for client to select option...\n\n");
send_msg(connfd);

get_menu_choice(connfd, menu_choice);
printf("Client %d choice was: %s\n", connfd, menu_choice);

switch (*menu_choice)
{
case '1':
send_ip(connfd);
break;
case '2':
send_time(connfd);
break;
case '3':
get_and_send_uname(connfd, *uts);
break;
case '4':
send_file_list(connfd);
break;
case '5':
printf("File Transfer not implemented\n");
break;
case '6':
break;
default:
printf("Invalid choice\n");
}

//if(menu_choice != NULL)
//{
// free(menu_choice);
//}

} while (*menu_choice != '6');

if (choice != NULL)
{
free(choice);
}

从客户端获取菜单选项:

void get_menu_choice(int socketNumber, char *choice)
{
size_t n;

readn(socketNumber, (unsigned char *) &n, sizeof(size_t));
readn(socketNumber, (unsigned char *) choice, n);

printf("Received: %zu bytes\n\n", n);
}


要求的附加功能:

发送消息(服务器端):

void send_msg(int socket)
{
char msg_string[] = "\nPlease enter an option:";

size_t n = strlen(msg_string) + 1;
writen(socket, (unsigned char *) &n, sizeof(size_t));
writen(socket, (unsigned char *) msg_string, n);
}

获取消息(客户端:

void get_msg(int socket)
{
char msg_string[32];
size_t k;

readn(socket, (unsigned char *) &k, sizeof(size_t));
readn(socket, (unsigned char *) msg_string, k);

printf("%s\n", msg_string);
}

我在这里做错了什么?我不会影响指针指向的值吗?

最佳答案

do 循环中,您反复释放指针

if(menu_choice != NULL)
{
free(menu_choice);
}

但是你只在循环之前分配一次。

关于C: 如何将经过 malloc 处理的指针传递给函数并释放它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47746112/

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