当结构作为双指针传递时,访问结构变量

Access a struct variable when struct is passed as a double pointer

本文关键字:结构 变量 访问 指针      更新时间:2024-09-29

我有一个函数,如下

int check_inband_status(Port **ePort, Port **wPort, InbandPort *inbandPort)
{
std::ifstream ring_config_file(RING_CONFIG_FILE);
Json::Value ring_config;
ring_config_file >> ring_config;
(*ePort)->port_id = ring_config["east_port"]["port_id"].asInt();
(*ePort)->port_type = ring_config["east_port"]["port_type"].asString();
(*wPort)->port_id = ring_config["west_port"]["port_id"].asInt();
(*ePort)->port_type = ring_config["west_port"]["port_type"].asString();
ring_config_file.close();
}

我有一个json文件,我正在读取它,并将值分配给ePort和wPort。这是变量ePort和wPort

class InbandPort : public Port
{
public:
uint32_t                vid;
uint32_t                nicid;
uint32_t                intf_id;
uint32_t                number_of_nni;
bool                    is_lag;
olt_intf_type_t         intftype;
Port                    *port1;
Port                    *port2;
SubportList             sPorts;
ERPSPort                *erpsPort;
bool                    is_active;
bool                    is_dhcp_done; /* to mark the dhcp is done or not is static dhcp case by default value                                                   is true*/
bool                    is_ring_configured;
InbandPort() : Port()
{
vid             = INVALID_ID;
nicid           = INVALID_ID;
intf_id         = INVALID_ID;
port1           = NULL;
port2           = NULL;
erpsPort        = NULL;
is_lag          = false;
is_dhcp_done    = false;
is_ring_configured = false;
port_id         = 0;
number_of_nni   = 0;
}
.....

这是json文件

{
"east_port" : {
"port_id" : 2,
"port_type" : "nni"
},
"west_port" : {
"port_id" : 4,
"port_type" : "nni"
}
}

它正在崩溃

(*ePort)->port_id = ring_config["east_port"]["port_id"].asInt();

我知道我在访问/分配双指针时犯了一些错误。有人能给我指一下吗?

编辑:我将它作为双指针传递,因为我也想从其他函数访问相同的值。我是如何通过的

Port                       *ePort  = NULL;
Port                       *wPort = NULL;
check_inband_status(&ePort, &wPort, inbandPort);

您正在传递空指针的地址,因此*ePort*wPort都为空
然后你取消引用它们,它就会爆炸。

该函数可能应该(动态地(创建新对象,并将它们的地址分配给*ePort*wPort

创建什么类型的对象并不明显,但我希望它看起来像这样(但有一些错误处理(:

int check_inband_status(Port **ePort, Port **wPort, InbandPort *inbandPort)
{
std::ifstream ring_config_file(RING_CONFIG_FILE);
Json::Value ring_config;
ring_config_file >> ring_config;
Port* east = new Port;
east->port_id = ring_config["east_port"]["port_id"].asInt();
east->port_type = ring_config["east_port"]["port_type"].asString();
Port* west = new Port;
west->port_id = ring_config["west_port"]["port_id"].asInt();
west->port_type = ring_config["west_port"]["port_type"].asString();
*ePort = east;
*wPort = west;
return something_meaningful;
}

另一种选择是而不是应该动态创建对象,而只是修改它们
在这种情况下,您只需要一个级别的指针(或引用(:

int check_inband_status(Port *east, Port *west, InbandPort *inbandPort)
{
std::ifstream ring_config_file(RING_CONFIG_FILE);
Json::Value ring_config;
ring_config_file >> ring_config;
east->port_id = ring_config["east_port"]["port_id"].asInt();
east->port_type = ring_config["east_port"]["port_type"].asString();
west->port_id = ring_config["west_port"]["port_id"].asInt();
west->port_type = ring_config["west_port"]["port_type"].asString();
return something_meaningful;
}
// ...
Port ePort;
Port wPort;
check_inband_status(&ePort, &wPort, inbandPort);

对我来说,它看起来像是一个典型的安装或初始化函数的C接口

使用双指针(object_type**(可以在堆上分配对象,并且可以将地址返回给函数的调用方。我担心,这就是你所缺少的。你试图写入一个不存在的对象。

如果函数的调用方是良性的,那么在将指针(或者更好地说,它的地址(传递给函数之前,他们初始化了指向nullptr的指针,您可以验证,必须首先创建它。但由于nullptr初始化不是默认的,所以您不能指望它

就在几天前,YouTube上的CppCon频道发布了一篇关于指针基础的去年活动的精彩演讲(https://www.youtube.com/watch?v=0zd8eznWv4k)。