ソケットから受信したデータを保存しようとしているときに、ポインターへのポインターを関数に渡して動的に割り当てています。1 つの要求に対しては機能しますが、2 番目の要求では通常、セグ フォールトが発生します。Valgrind の不満: 条件付きのジャンプまたは移動は、応答ポインターを参照する初期化されていない値に依存します。
ポインターを初期化するにはどうすればよいですか、またはこれを安全にするために何ができますか? そして、メイン関数で解放するのは正しいですか?
int main(int argc, char **argv) {
char * response;
char readbuf[BUFFSIZE + 1] = "";
//here I read some data into readbuff which I will send to the server below
handle_request_data(readbuf, &response);
//do some stuff with response, send to another socket
free(response); // can I do that?
}
int handle_request_data(char * readbuf, char ** response) {
//create tcp socket, connect to it and send readbuf to server
int recv_total = 0;
char buffer[BUFFSIZE + 1] = "";
*response = malloc(BUFFSIZE + 1);
while ((tmpres = recv(sock_tcp, buffer, BUFFSIZE, 0)) > 0) {
if (recv_total > 0) {
//need more memory for buffer
*response = realloc(*response, BUFFSIZE + recv_total + 1);
}
memcpy(*response + recv_total, buffer, tmpres);
recv_total += tmpres;
}
}
ご協力ありがとうございました!