C++ external function modifies the value of a class member through a pointer

1. Problems arise

  I transcribed my C code to C++. In my code, I created a new class for location data. I originally parsed a GPS string through C code and then assigned a value to it. The code snippet is as follows

// Extract NMEA-0183 information
 // gpsx: nmea information structure
 // buf: the first address of the received GPS data buffer 
void GPS_Analysis(nmea_msg *gpsx,uint8_t * buf)
{
    if((strstr((const char *)buf,"$GPRMC")))
        NMEA_GPRMC_Analysis(gpsx,buf);    //GPRMC解析
}

Originally, a global structure variable was passed in, and there is no problem with this code.

But now I pass in such a variable: a structure pointer in the class is passed into this function.

class PACK
{
    public:
         nmea_msg msg;
};

PACK pack;



GPS_Analysis(&pack.msg,Temp_buf);

But I found a problem, I parsed the data, but I can't assign a value to my structure through a pointer in this c function, which is very painful.

 

2. Problem solving

  I try to convert gps.c into gps.cpp, and then change this parameter to the way of passing by reference.

void GPS_Analysis(nmea_msg & gpsx,uint8_t *buf)
{
        NMEA_GPRMC_Analysis(&gpsx,buf);    //GPRMC解析
}

  but! I found that every time I successfully assign a value once, a segmentation fault will be reported the second time. This makes me even more sore.

 Finally, I found out that this is because of the incoming value. Sometimes strstr can't find the position of the parameter and returns the value null, but this happens when the null is manipulated again.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324890609&siteId=291194637