I am trying to use the new operator to create a global reference, by declaring
tNMEA2000 &NMEA2000=*(new tNMEA2000_stm());
outside of any functions.
When I use tNMEA2000 &NMEA2000=*(new tNMEA2000_stm());
outside of any functions, it ends up getting set to a NULLPTR. If I declare it inside of a function, it works correctly.
This isn't the actual code, but a simplified version. I haven't included the actual class definitions
#include "nmea_wrapper.h" //This defines the tNMEA2000_stm class
#include "NMEA2000.h" //This defines the tNMEA2000 class that tNMEA2000_stm inherits from
tNMEA2000 &NMEA2000 = *(new tNMEA2000_stm()); //This ends up with a NULLPTR
void setup(){
tNMEA2000 &NMEA2000 = *(new tNMEA2000_stm()); //This correctly initializes the memory
}
From what I can understand declaring it globally should work, and not get set to a NULLPTR. I am not sure if this is a quirk of using newlib, and I can't seem to find any documentation to tell me one way from the other if it's valid or not.