gpt4 book ai didi

c - 为什么这个 realloc() 失败?

转载 作者:行者123 更新时间:2023-11-30 15:22:40 25 4
gpt4 key购买 nike

我的第一个 C 项目是一个简单的类似 cURL 的 HTTP 客户端,它检索发送到服务器的请求的 header 、内容和状态。到目前为止,一切都在进行中,但尚未完成,我需要一些帮助。内存分配对我来说是一个障碍,当前的问题是我无法独自解决的。

在名为 HTTPresponse *requestHTTPresponse(URLdata *requestDestination) 的函数中,由于某种未知原因,保存原始 HTTP 响应 header 和内容的字符串的重新分配失败。原始 HTTP 响应字符串的指针被设置为 NULL,我被迫退出程序。 为什么会发生这种情况以及如何解决它?此外,当我编译程序时,它有时会卡住并拒绝按预期打印文本或退出,这可能是 DNS 的结果挂断或其他一些奇怪的网络问题。也欢迎任何其他建议。

完整的 C 源代码可以在 here 找到.

HTTPresponse *requestHTTPresponse(URLdata *requestDestination) {
char ip[INET6_ADDRSTRLEN], *request, *response = malloc(1), *headerTerminator = NULL, *responseTerminator = NULL, *headerString, *contentString, buffer[100];
int sockfd, bufferLength, responseLength;
struct addrinfo hints, *servinfo, *p;

/* Initialize HTTP response structure */
HTTPresponse *returnresponse = malloc(sizeof(HTTPresponse));
returnresponse->status = 0;
returnresponse->headers = NULL;
returnresponse->content = NULL;

/* Retrieve IP address of host */
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
if (getaddrinfo(requestDestination->host, requestDestination->port, &hints, &servinfo) != 0) {
return returnresponse;
}

/* Loop through all possible connections */
for (p = servinfo; p != NULL; p = p->ai_next) {
if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
continue;
}
if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
close(sockfd);
continue;
}
break;
}

if (p == NULL) {
return returnresponse;
}

/* Print IP address & free server information */
inet_ntop(p->ai_family, get_in_addr((struct sockaddr*)p->ai_addr), ip, sizeof(ip));
//printf("The IP address is: %s\n", ip);
freeaddrinfo(servinfo);

/* Format HTTP request */
request = malloc(27 + (int)strlen(requestDestination->path) + (int)strlen(requestDestination->host) * sizeof(char));
if (request == NULL) {
printf("Failure to allocate memory.\n");
exit(1);
}
sprintf(request, "GET %s HTTP/1.1\r\nHost: %s\r\n\r\n", (*(requestDestination->path) == 0)?"/":requestDestination->path, requestDestination->host);

/* Send HTTP request */
send(sockfd, request, 26 + strlen(requestDestination->path) + strlen(requestDestination->host), 0);
free(request);

/* Receive HTTP response */
do {
bufferLength = recv(sockfd, buffer, 99, 0);
if (bufferLength == -1) {
break;
}
responseLength += bufferLength;
response = realloc(response, (responseLength + 1) * sizeof(char)); //Source of issues
if (response == NULL) {
printf("Failure to allocate memory.\n");
exit(2);
}
strncpy(response + responseLength - bufferLength, buffer, bufferLength);
if (headerTerminator) {
responseTerminator = strstr(response, "\r\n\r\n");
} else {
headerTerminator = strstr(response, "\r\n\r\n");
}
} while (responseTerminator == NULL);

/* Store data in structs */
headerString = malloc((headerTerminator - response + 1) * sizeof(char));
if (headerString == NULL) {
printf("Failure to allocate memory.\n");
exit(1);
}
strncpy(headerString, response, headerTerminator - response);
contentString = malloc((responseTerminator - headerTerminator) * sizeof(char));
if (contentString == NULL) {
printf("Failure to allocate memory.\n");
exit(1);
}
strncpy(contentString, headerTerminator, responseTerminator - headerTerminator);
returnresponse->headers = generateHeaderList(headerString);
returnresponse->content = contentString;
free(response);

/* Close connection & return HTTP response */
close(sockfd);
return returnresponse;
}

最佳答案

确保您的变量已初始化。在最初的问题中,您尝试重新分配空指针,并且需要确保您的responselength变量初始化为0。

-----编辑-----

此外,当从网络连接获取数据时,如果您使用 strcpy 等函数,请确保您的字符串以零结尾。

关于c - 为什么这个 realloc() 失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29137089/

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