소켓에서 읽고 printf (필수)를 사용하여 stdout으로 인쇄하려고합니다.char *의 printf가 세그먼트 오류를 가져옴
그러나 정교한 웹 사이트에서 특정 파일 (HTML)을 읽을 때마다 Segmentation Fault가 발생합니다.
이 코드를 살펴보고 잘못된 점을 알려주십시오.
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
response_data에는 어떤 내용이 들어 있을까요? printf 형식화 문자가 포함되어 있으면 printf는 전달하지 않은 매개 변수에 액세스하려고 시도합니다. 시도 대신에 넣어? –