C++ 复制两个结构指针时出现分段错误
下面的代码是用gcc编译的C++ 复制两个结构指针时出现分段错误,c++,c,struct,websocket,segmentation-fault,C++,C,Struct,Websocket,Segmentation Fault,下面的代码是用gcc编译的 typedef struct { char *device_id; char *device_type; char *home_id; char *op_code; char *arg_name; char *arg_value; } query_state_t; enum request_type { INVALID, GET_DEVICE_TEMP}; enum request_type get_reque
typedef struct {
char *device_id;
char *device_type;
char *home_id;
char *op_code;
char *arg_name;
char *arg_value;
} query_state_t;
enum request_type { INVALID, GET_DEVICE_TEMP};
enum request_type get_request_type(const json_t *root_obj, query_state_t *query_state_out) {
json_t *query = json_object_get(root_obj,"query");
if (!query || !json_is_object(query)) {
return INVALID;
}
...
const unsigned char *request_type = json_string_value(op_code_str);
if (strcmp(request_type, "get_DeviceTemp") == 0) {
json_t *arg_name = json_object_get(op_code, "argName");
json_t *arg_value = json_object_get(op_code, "argValue");
if (!arg_name || !json_is_string(arg_name)) {
return INVALID;
}
if (!arg_value || !json_is_string(arg_value)) {
return INVALID;
}
query_state_t *query_state = malloc(sizeof(query_state_t));
query_state->device_id = (char *)json_string_value(device_id);
query_state->device_type = (char *)json_string_value(device_type);
query_state->home_id = (char *)json_string_value(home_id);
query_state->arg_name = (char *)json_string_value(arg_name);
query_state->arg_value = (char *)json_string_value(arg_value);
query_state->op_code = (char *)request_type;
memcpy(query_state_out, query_state, sizeof(query_state_t)); //Segmentation fault (SIGSEGV)
return GET_DEVICE_TEMP;
}
else {
return INVALID;
}
}
当我试图记忆两个结构指针时,我遇到了一个分段错误
get_request_type函数接受一个json_对象和一个结构指针(out参数),然后返回一个显示结果的枚举。(无效或请求类型)
gdb回溯显示了以下内容
#0 0x00007ffff77432a7 in ?? () from /lib/x86_64-linux-gnu/libc.so.6 │~
#1 0x0000000000401206 in get_request_type (root_obj=0x6263f0, query_state_out=0x7c00000077) │~
at websocketserver.c:412 │~
#2 0x00000000004013f2 in callback_web_socket (this=0x603010, wsi=0x625b50, reason=LWS_CALLBACK_RECEIVE, │~
user=0x0, in=0x6262c2, len=161) at websocketserver.c:473 │~
#3 0x00007ffff79bfd1c in user_callback_handle_rxflow () from /usr/local/lib/libwebsockets.so.4.0.0 │~
#4 0x00007ffff79c39d0 in libwebsocket_rx_sm () from /usr/local/lib/libwebsockets.so.4.0.0 │~
#5 0x00007ffff79c40f9 in libwebsocket_interpret_incoming_packet () │~
from /usr/local/lib/libwebsockets.so.4.0.0 │~
#6 0x00007ffff79bead4 in libwebsocket_read () from /usr/local/lib/libwebsockets.so.4.0.0 │~
#7 0x00007ffff79c1b20 in libwebsocket_service_fd () from /usr/local/lib/libwebsockets.so.4.0.0 │~
#8 0x00007ffff79c1c0a in libwebsocket_service () from /usr/local/lib/libwebsockets.so.4.0.0 │~
#9 0x0000000000401586 in main () at websocketserver.c:641
显然,第1帧是有问题的帧。这就是我得到的:
(gdb) frame 1 │~
#1 0x0000000000401206 in get_request_type (root_obj=0x6263f0, query_state_out=0x7c00000077) │~
at websocketserver.c:412 │~
412 memcpy(query_state_out, query_state, sizeof(query_state_t));
但我不明白,我已经查询了_statestruct变量,并且可以单独打印它的成员。由于某种原因,在memcpy上抛出了分段错误
任何帮助都会得到帮助 您正在
memcpy
指向未初始化的指针。在主功能中尝试以下操作:
query_state_t *query_param = malloc(sizeof(query_state_t));
enum request_type request_type = get_request_type(root, query_param);
这修复了分段错误问题,但现在我无法立即将任何字符串值分配到我的结构成员(即:query\u state->device\u id始终为0x0),我看不出任何明显的原因。您可能希望在get\u request\u type
中的每个return
s上设置断点(如果愿意,也可以添加额外的printf
s),以确保json库以您期望的方式进行解析。是的,我制作了一系列printf语句来确认我的json库正在返回值。是的,但它是常量字符*类型。类型转换是否会在某种程度上扰乱任务?不。转换是在乞求其他东西在以后出现可怕的错误,但它不应该导致这种情况。我在结构上做了memset,它解决了这个问题。谢谢你的帮助。在这种情况下,如果库中给了我一个常量char*,但我需要char*,因为我可能需要修改它们,那么我应该如何处理强制转换?
query_state_t *query_param = malloc(sizeof(query_state_t));
enum request_type request_type = get_request_type(root, query_param);