【发布时间】:2015-10-12 23:09:47
【问题描述】:
我试图创建一个函数,它将任何 URL 作为输入并从中删除 http://,然后从 URL 的其余部分获取 uri 和域。当我执行下面的代码时,我在 memcpy 语句中收到一个分段错误,标记为“//seg fault here”。
执行后,我收到以下输出:
TEST
http:// found
Segmentation fault
我预计会出现以下情况:
TEST
http:// found
/ found
www.x.com /a/b/c
当我声明一个大的缓冲区空间来复制结果时,为什么会收到分段错误?我的程序中是否存在导致指向结果的无效指针的内容?
代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void getdomurl(const char* in,char* uri,char* dom){
char b[200000],*p=strstr(in,"http://");
if (p){
printf("http:// found\n");
memcpy(b,p+7,100000); //seg fault here
}else{
printf("http:// not found\n");
memcpy(b,in,100000);
}
printf("/ scan\n");
p=strstr(b,"/");
if (p){
printf("/ found\n");
memcpy(dom,b,p-b);memcpy(uri,p,100000);
}else{
printf("/ not found\n");
memcpy(dom,b,100000);uri[0]='/';uri[1]='\0';
}
}
int main(int argc,char* argv[]){
char uri[100000];char dom[10000];
printf("TEST\n");
getdomurl("http://www.x.com/a/b/c",uri,dom);
printf("%s %s",uri,dom);
return 0;
}
【问题讨论】:
标签: c pointers segmentation-fault buffer