0
因此,我對RPC程序進行了一些更新,現在對其進行了segfaulting,我不確定我在這裏做了什麼錯誤。兩者之間的區別在於刪除了將值賦給args
結構的if語句。當我創建並向我的結構添加值時發生了某些事情,導致出現段錯誤
段錯誤
void
database_1(char *host, char *action, char *message)
{
printf("Action: %s\n", action);
printf("Message: %s\n", message);
CLIENT *clnt;
rpc_args *result_1;
//struct rpc_args action_1_arg;
//rpc arguments struct to pass to server
struct rpc_args *args = malloc(sizeof(struct rpc_args));
char *id = generate_id();
if (strcmp(action, "GET") == 0) {
strcpy(args->action, action);
strcpy(args->id, id);
} else if(strcmp(action, "PUT") == 0) {
strcpy(args->action, action);
strcpy(args->id, id);
strcpy(args->message.content, message);
}
#ifndef DEBUG
clnt = clnt_create (host, DATABASE, ASSIGNMENT_7, "udp");
if (clnt == NULL) {
clnt_pcreateerror (host);
exit (1);
}
#endif /* DEBUG */
result_1 = action_1(args, clnt);
if (result_1 == (rpc_args *) NULL) {
clnt_perror (clnt, "call failed");
}
#ifndef DEBUG
free(args);
clnt_destroy (clnt);
#endif /* DEBUG */
}
不段錯誤
void
database_1(char *host, char *action, char *message)
{
printf("Action: %s\n", action);
printf("Message: %s\n", message);
CLIENT *clnt;
rpc_args *result_1;
//struct rpc_args action_1_arg;
//rpc arguments struct to pass to server
struct rpc_args *args = malloc(sizeof(struct rpc_args));
char *id = generate_id();
#ifndef DEBUG
clnt = clnt_create (host, DATABASE, ASSIGNMENT_7, "udp");
if (clnt == NULL) {
clnt_pcreateerror (host);
exit (1);
}
#endif /* DEBUG */
result_1 = action_1(args, clnt);
if (result_1 == (rpc_args *) NULL) {
clnt_perror (clnt, "call failed");
}
#ifndef DEBUG
free(args);
clnt_destroy (clnt);
#endif /* DEBUG */
}
你可以顯示'struct rpc_args' ?.會員'行動'和'ID'可能是字符指針?如果是這樣,你也必須爲它們中的每一個分配內存。 – Unimportant
@ user1320881是的,這正是它。 – chrisd1100