c++jsonpointersdouble-pointer

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


I have a function as follows

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();
}

I have a json file and I am reading it and assigning the values to ePort and wPort. This is the variable ePort and 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;
        }
.....

Here is the json file

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

It is crashing at

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

I know I am making some mistake in accessing/assigning double pointer. Can someone please point me to it?

Edit: I am passing it as a double pointer because I want to access the same value from other function as well. And how I am passing is

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

Solution

  • You're passing the addresses of null pointers, so both *ePort and *wPort are null.
    And then you dereference them, and it goes boom.

    The function is probably supposed to (dynamically) create new objects and assign their addresses to *ePort and *wPort.

    It's not obvious what type of objects to create, but I would expect it to look something like this (but with some error handling):

    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;
    }
    

    Another option is that it's not supposed to create objects dynamically but just modify them.
    In that case, you need just one level of pointer (or a reference):

    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);