当结构作为双指针传递时,访问结构变量
Access a struct variable when struct is passed as a double pointer
我有一个函数,如下
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)。
相关文章:
- 在类构造函数中使用结构变量
- 在类构造函数中定义结构变量的参数
- 为什么我们再次从结构对象创建结构变量?
- 如何从其他结构访问受保护的结构变量
- 指向结构变量的指针未保存到eeprom
- 为什么我的程序没有更改结构变量中的值?
- 在 CPP 中初始化 typedef 结构变量
- 带有大结构变量的 CUDA 内核函数给出了错误的结果
- 有没有办法初始化不涉及编写构造函数的新结构变量?
- 结构变量查询
- 在结构声明的分号之前声明的结构变量是否为全局变量
- C++ 如何访问指针的结构变量
- 使用指向结构变量成员的指针访问该结构的成员的地址
- 为什么在 sizeof() 函数中与 * 运算符一起使用和不使用 * 运算符时,指向结构变量的指针大小会有所不同?
- 如何在类别内的类中访问类中的结构变量
- 尝试使用文本文件初始化多个结构变量
- 从类对象到结构变量的 memcpy
- C++结构变量初始化时出现问题
- 如何在 C++ 中使用带有类/结构变量的列表
- 动态数组添加元素而不创建结构变量/对象