ソケットから読み込み、printf(必須)を使用してstdoutに出力しようとしています。char *のprintfがセグメンテーションフォールトを取得する
しかし、私は正常なWebサイトから特定のファイル(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は渡されなかったパラメータにアクセスしようとします。試してみてください。 –