This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: C++ Static String Causes Segmentation Fault
Thanks for the response, Ian. With an eye toward correcting the problem
in my source code, does C++ make any guarantees about global
constructors in the same source?
Several initial tests indicate that the following code works regardless of
whether main.cpp or StringTest.cpp appears first on the g++ command
line, but does C++ guarantee it?
-------- %< -------------------------
string StaticString::myString = "The String";
StaticString MyString;
int
main (int argc, char **argv)
{
...
}
----------%<----------------------------
Regards,
Ben
Ian Lance Taylor wrote:
----- Original Message ----
> Ben Atkinson writes:
>
> > void
> > UseTheString::StringUsageFunction (const string &TheString,
> > string &ReturnString)
> > {
> > cout << "Test 02" << endl;
> > ReturnString = TheString; // Causes the sementation fault.
> > cout << "Test 03" << endl;
> > }
> >
> > string StaticString::myString = "The String";
> >
> > StaticString::StaticString (void)
> > {
> > cout << "Test 01" << endl;
> > stringUsageObject.StringUsageFunction(myString, accessableString);
> > cout << "Test 04" << endl;
> > }
>
> This effectively requires that StaticString::myString be constructed
> before any global StaticString object is created. You have a global
> StaticString object in the other source file. You are assuming that
> global constructors will be run in a particular order across different
> files. C++ makes no such guarantee.
>
> Ian