C++: string = NULL gives SEGFAULT 11? -
i modified project today allow save files in different folders, , found program crashed when startup:
segmentation fault: 11 because introduced many changes before testing program, started comment out functions added, no help. put
cout << "hello world" << endl; return 0; as first 2 lines in int main(), still crashed without showing anything.
finally, took me 1 hour figure out error. modification includes declaring global variable
string foldername = null; the line above seems innocence, declaring global variable.
then tried simple program:
#include <string> std::string = null; int main(){ return 0; } and crashed @ startup.
why declaring string global variable null make program silently crashed without info?
the std::string - opposite inherited c char* strings - holds valid string. may empty, cannot null. if try initialise std::string null try blindly copy c-like string null address, undefined behaviour.
just use
std::string a; and a initialised empty string.
Comments
Post a Comment