C 为什么我的代码会产生分段错误?
我正在尝试创建一个下载网页的功能。我知道libcurl是有效的,但我拒绝使用它。我不喜欢这样的想法,即需要安装一些其他软件包才能使此功能正常工作,因此我想创建自己的软件包 不管怎么说,事情一直进展顺利,直到我因为未知原因开始收到分割错误。因此,我将代码缩减到尽可能小的程度,这样分段错误仍然会发生。然后我试着调试它 以下代码是导致分段错误的原因:C 为什么我的代码会产生分段错误?,c,segmentation-fault,C,Segmentation Fault,我正在尝试创建一个下载网页的功能。我知道libcurl是有效的,但我拒绝使用它。我不喜欢这样的想法,即需要安装一些其他软件包才能使此功能正常工作,因此我想创建自己的软件包 不管怎么说,事情一直进展顺利,直到我因为未知原因开始收到分割错误。因此,我将代码缩减到尽可能小的程度,这样分段错误仍然会发生。然后我试着调试它 以下代码是导致分段错误的原因: #include <stdio.h> #include <string.h> #include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct httpheader
{
char cache_control[21];
int unsigned long content_length;
char content_type[51];
char date[41];
char expires[41];
char last_modified[41];
char location[305];
char server[51];
int status;
char transfer_encoding[11];
};
int main (void)
{
struct httpheader headerinfo;
int buf_size = 4096;
char buf[buf_size];
strncpy(buf, "HTTP/1.1 200 OK\n", buf_size);
strncat(buf, "Date: Mon, 21 Jul 2014 20:52:38 GMT\n", buf_size);
strncat(buf, "Server: Apache\n", buf_size);
strncat(buf, "Set-Cookie: geofilter_country=US; path=/; domain=www.sony.com; expires=Mon, 14-Sep-2015 20:52:38 GMT\n", buf_size);
strncat(buf, "Accept-Ranges: bytes\n", buf_size);
strncat(buf, "Content-Length: 11978\n", buf_size);
strncat(buf, "Content-Type: text/html\n", buf_size);
// printf("1\n");
set_header(&headerinfo, &buf);
display_header(&headerinfo);
exit(0);
}
int set_header (struct httpheader *heady, char *data)
{
char *ptr1, *ptr2; // Used for string manipulation.
char **dummy;
data[12] = '\0';
heady->status = atoi(&data[9]);
data[12] = ' ';
if((ptr1 = strstr(data, "Content-Length: ")) == NULL)
heady->content_length = 0;
else
heady->content_length = strtoul(&ptr1[16], dummy, 10);
heady->content_length = 0;
strcpy(heady->date, "");
strcpy(heady->content_type, "");
strcpy(heady->cache_control, "");
strcpy(heady->expires, "");
strcpy(heady->last_modified, "");
strcpy(heady->location, "");
strcpy(heady->server, "");
strcpy(heady->transfer_encoding, "");
}
int display_header (struct httpheader *heady)
{
printf("Cache-Control : %s\n", heady->cache_control);
printf("Content-Length : %d\n", heady->content_length);
printf("Content-Type : %s\n", heady->content_type);
printf("Date : %s\n", heady->date);
printf("Expires : %s\n", heady->expires);
printf("Last-Modified : %s\n", heady->last_modified);
printf("Location : %s\n", heady->location);
printf("Server : %s\n", heady->server);
printf("Status : %d\n", heady->status);
printf("Transfer-Encoding : %s\n", heady->transfer_encoding);
}
编辑:
谢谢你的帮助,这是一个了不起的社区!无论如何,我想我的问题是“一点知识是危险的” 我对C语言的理解需要扩展。认为我可以使用几个“printf”语句进行调试显然是荒谬的(它有时可以工作,但对我来说还不够好)。另外,我需要返回到函数原型。我忘了一次,编译也没有问题,所以我停止了!好了,我又回来做了
如果crashmstr将他的评论作为答案,我会选择它。崩溃最可能的原因是代码中缺少前向声明:当您在声明函数之前调用函数时,编译器会假设函数的所有形式参数都是
int
类型。你应该得到这方面的警告
在main
之前添加以下两行以解决此问题:
int set_header (struct httpheader *heady, char *data);
int display_header (struct httpheader *heady);
下面是有关代码的一些附加说明:
- 如果要使用与,请使用索引零
set_header(&headerinfo,&buf[0])
- 或者,您可以使用
,而不使用符号和或索引buf
- 您不应该使用
/strncpy
函数系列,因为它们是为不同的目的而设计的。对于那些期望这些函数与固定长度字符串一起使用的读者来说,这是一种误导strncat
- 不要忽略警告。当C编译器报告警告时,几乎可以肯定您的代码有问题
int set_header (struct httpheader *heady, char *data)
不匹配
char buf[buf_size];
..
set_header(&headerinfo, &buf);
&buf是字符**不是字符*
你的函数原型在哪里?我希望编译器会发疯似地抱怨,在调试程序下运行它。请阅读
strncat
的man
,这不是strncat
第三个参数的工作方式。为什么set\u header
和display\u header
函数声明为返回int值,而它们都没有return
语句?您应该将它们声明为void
或返回一个int。还请启用警告并注意它们(gcc-Wall…
)代码>(未初始化)然后strtoul(&ptr1[16],虚拟,10)代码>看起来很奇怪。参见使用char*dummy的示例代码>然后strtoul(&ptr1[16],&dummy,10)代码>。。。在你的例子中,你甚至没有给它一个地址来使用。我可以在没有原型声明的情况下编译。我想我只是觉得我不需要再使用我正在使用的编译器了,因为没有抱怨。但是它在调试时会抱怨,所以我将添加它们。这一定是一个非常古老的宽容编译器。正如Blinkenlight,你对strncat的看法是正确的。最后一个论点应该是“buf_size-strlen(buf)-1”。我接受这个答案的原因如下:我的第一选择不可用;这一答复是首先提交的。
char buf[buf_size];
..
set_header(&headerinfo, &buf);