C++ Static String Causes Segmentation Fault

Ian Lance Taylor iant@google.com
Wed Jul 22 19:50:00 GMT 2009


Ben Atkinson <bwa4992@yahoo.com> 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



More information about the Libstdc++ mailing list