我试图从套接字读取并使用printf(必须)打印到标准输出;char * printf获取分段错误
但是,每次从理智的网站读取特定文件(HTML)时,我都会收到分段错误。
请看看这段代码,告诉我什么是错的。
int total_read = 0;
char* read_buff = malloc(BUF_SIZE);
char* response_data = NULL;
if (read_buff == NULL){
perror("malloc");
exit(1);
}
while((nbytes = read(fd, read_buff, BUF_SIZE)) > 0){
int former_total = total_read;
total_read += nbytes;
response_data = realloc(response_data, total_read);
memmove(response_data + former_total, read_buff, nbytes); //start writing at the end of spot before the increase.
}
if (nbytes < 0){
perror("read");
exit(1);
}
printf(response_data);
谢谢。
您应该通过解决您的问题的答案点击对勾形图标。 – erjiang 2010-12-05 20:55:19
什么可能在response_data中?如果它包含printf格式的字符,printf将尝试访问一些你没有通过的参数。试试看呢? – 2010-12-05 20:55:55