c++ program crash when creating global instance of class whose constructor references a global variable -
i'm trying make global instance of class constructor references global variable.
the program compiles without error. when run, crashes on reference of global variable.
how create global instance of class without constructor crashing it?
here sscce made:
/* main.cpp */ #include "testclass.h" // need global instance of testclass testclass j; int main() { return 0; }
-
/* c.h */ #ifndef c_h_included #define c_h_included #include <string> // global extern const std::string s; #endif // c_h_included
-
/* c.cpp */ #include "c.h" #include <string> // extern definition of global const std::string s = "global string data";
-
/* testclass.h */ #ifndef testclass_h_included #define testclass_h_included class testclass { public: testclass(); }; #endif // testclass_h_included
-
/* testclass.cpp */ #include "testclass.h" #include <iostream> #include "c.h" // s global testclass::testclass() { std::cout << s << std::endl; // line crashes program }
debugger messages crash:
program received signal sigsegv, segmentation fault. in std::basic_ostream<char, std::char_traits<char> >& std::operator<< <char, std::char_traits<char>, std::allocator<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) () () #1 0x004014f9 in testclass::testclass (this=0x4a0024 <j>) @ e:\cpp\externconsttest\testclass.cpp:9 e:\cpp\externconsttest\testclass.cpp:9:117:beg:0x4014f9 @ e:\cpp\externconsttest\testclass.cpp:9 #1 0x004014f9 in testclass::testclass (this=0x4a0024 <j>) @ e:\cpp\externconsttest\testclass.cpp:9 e:\cpp\externconsttest\testclass.cpp:9:117:beg:0x4014f9
this example crashes in operator<<, crashes on reference s no matter how it's referenced.
i guess crashes because global const std::string s
not initialized yet @ time when contructor of testclass
called. general problem global , static variables in c++: in general not know in order global , static variables initialized (actually initialized in order in pass object files linker @ linking stage - not helpful). there few different solutions problem. 1 of them is:
create function static variable in body returns reference variable (instead of using global variable call function). simmilar singleton design pattern:
const std::string& get_my_string() { static const std::string s; return s; }
then in constructor:
testclass::testclass() { std::cout << get_my_string() << std::endl; }
calling get_my_string
force initialization of static string only once (the first time function called) exactly @ time when need it. please notice example not take threads account (in multi-threaded application should synchronize get_my_string()
function protect initialization of static string).
i hope helps.
by way: may simmilar problems global testclass j
.
this way going solve half of problem - initialization (you still don't know order of destruction) - in cases sufficient though.
another option create string on heap (probably using simmilar approach described above) - have delete
@ time know safe so.
Comments
Post a Comment