我傳遞緩衝器(字符*)到功能中C.用於緩衝和附加一個用C
在函數內部傳遞一個緩衝器(字符*)於函數時,我分配存儲器字符串(來自虛擬服務器的響應)。當在函數內部打印時,字符串正在從服務器發送顯示。
當試圖從功能外讀取緩衝區,我沒有得到結果,我需要:
一個。 // char * server_message; //不工作,錯誤::分段錯誤(核心轉儲)
b。 // char * server_message = calloc((BUFFER_SIZE)+ 1,sizeof(char)); //不起作用,沒有產生錯誤,下面的server_message的printf打印出一個空值:==> server_message :: || ||
c。 char * server_message = calloc((1000 * BUFFER_SIZE)+ 1,sizeof(char)); //分配超過需要的內存工作:: ==> server_message :: || 2016-03-08 12:20:13 hello there ||
我不喜歡選項c,因爲我分配的內存比我需要的多。 出於測試目的,BUFFER_SIZE當前設置爲10。
這裏是主叫部分::
write_to_server(client_socket, message, server_message);
free(message);
printf("\n\n==> server_message:: || %s ||\n\n", server_message);
if (server_message != NULL)
free(server_message);
這裏是write_to_server功能代碼::
void write_to_server(int file_descriptor, char* message, char* server_message)
{
char* msg = strcat(message, "\n");
int n_bytes = write(file_descriptor, msg, strlen(msg) + 1);
if (n_bytes <= 0)
{
perror("write");
exit(EXIT_FAILURE);
}
if (DEBUG_MODE)
printf("\nsuccessfully written %d bytes to server:: %s", n_bytes, msg);
// char* message_back = calloc(BUFFER_SIZE + 1, sizeof(char));
if (server_message == NULL)
{
server_message = calloc(BUFFER_SIZE + 1, sizeof(char));
}
if (server_message == NULL)
{
perror("Could not allocate memory for server_message");
exit(EXIT_FAILURE);
}
char* tmp_message_back = calloc(BUFFER_SIZE + 1, sizeof(char));
if (tmp_message_back == NULL)
{
if (DEBUG_MODE)
perror("Could not allocate memory for tmp_message_back");
exit(EXIT_FAILURE);
}
int n_read = 0;
int n_total_read = 0;
while ((n_read = read(file_descriptor, tmp_message_back, BUFFER_SIZE)) > 0)
{
n_total_read += n_read;
tmp_message_back[n_read] = '\0';
/*
char* strcat(char* destination, const char* source);
Appends a copy of the source string to the destination string.
The terminating null character in destination is overwritten by the first character of source,
and a null-character is included at the end of the new string formed by the concatenation of both in destination.
*/
server_message = strcat(server_message, tmp_message_back);
// void* realloc(void* ptr, size_t size);
/*
char* new_message_back = realloc(server_message, n_total_read + (BUFFER_SIZE * sizeof(char)));
if (new_message_back == NULL)
{
perror("Could not allocate memory for server_message while receiving bytes from server");
free(tmp_message_back);
return;
}
server_message = new_message_back;
*/
server_message = realloc(server_message, n_total_read + (BUFFER_SIZE * sizeof(char)));
server_message[n_total_read] = '\0';
if (DEBUG_MODE)
printf("\nread %d (%d in total), size:: %zu ===> | %s |\t| %s |\n", n_read, n_total_read, strlen(server_message), server_message, tmp_message_back);
}
server_message = realloc(server_message, n_total_read);
if (DEBUG_MODE)
{
printf("\n\n\n\n-- read %d in total, size:: %zu\n", n_total_read, strlen(server_message));
printf("\n\nserver_message:: %s\n\n", server_message);
}
free(tmp_message_back);
// free(message_back);
}
C是通過值,即使是指針。 – 2501
您正在分配參數。如果你想修改調用函數中的變量,你需要傳遞指針給它們。指針沒有什麼特別之處。 – molbdnilo
這:'void write_to_server(int file_descriptor,char * message,char * server_message)'不能返回char *緩衝區。 –