ARTICLE AD BOX
I'm using the gpsmm library and the gpsd service in a C++ program querying a GPS module. I get a struct gps_data_t* variable with all the data the GPS module spits out, but when it can't fix enough satellites or the gpsd isn't running, the content of the variable is a nullptr. Please don't ask me how someone can come with such an unsafe solution (well, C++ is not Rust).
Now I've got a branch in my program, which reads somehow like this:
gpsmm gps_handler; struct gps_data_t *gps_data; tmc::tmc () : gps_handler ("localhost", DEFAULT_GPSD_PORT), { } tmc* tmc::Initialize () { tmc* instance = new tmc (); gps_data_t* response; if (!(response = instance -> gps_handler.stream (WATCH_ENABLE | WATCH_JSON))) { cout << "No GPSD running." << endl; } else { // do somethin' with the GPS data } }gpsmm::stream () returns a struct gps_data_t*, which hopefully contains the location data from the GPS module, otherwise a null pointer. Now I'd like to know how to check this pointer for being null without provoking a segfault. When I do it this way:
if ((response = instance -> gps_handler.stream (WATCH_ENABLE | WATCH_JSON)) == nullptr)…I get a segfault instantly because in the background, the == operator is translated into a strcmp () procedure call, which fails because one of its arguments is NULL. At least, gdb tells me that.
So: How can I check the variable for being a null pointer without running the risk of segfaulting?
