我試圖從套接字讀取並使用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