This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

C++ Static String Causes Segmentation Fault



I am trying to compile a program that uses a static basic string, but I
am getting a segmentation fault when I run the executable produced by
g++.  At the bottom of this message are three source files that produce
the problem (main.cpp, StringTest.h, and StringTest.cpp.)  These source
files are distilled down from a production system that is comprised of
approximately 100 source files, so the example doesn't "do" anything,
but it does illustrate the problem.

The problem centers on using a statically-declared basic string from the
C++ <string> library.  The program has two classes:

   - The StaticString class contains the static string as a private
      member.
   - The UseTheString class has a member function StringUsageFunction()
      that the StaticString class calls from its constructor.  It's the
      usage (assignment operator) of the static string in the member
      function that causes the segmentation fault in the executable.

The target program produces a segmentation fault only when it is
compiled in the following manner:

   - The static string is declared in a separate source file from
      main.cpp.
   - The main.cpp source file contains a global declaration of the
      StaticString class.
   - The main.cpp file is passed to the g++ compiler _after_
      StaticString.cpp when compiling for the Ubuntu x86 target.
   - The main.cpp file is passed to the g++ compiler _before_
      StaticString.cpp when compiling for the ARM target.

I first discovered the problem running on an embedded ARM Linux system,
but I have reproduced the problem on Ubuntu x86.  Here are the compiling
environments:

- Linux distributions:
   Ubuntu 8.10 / x86
   Debian 5.0 / arm-unknown-linux-gnueabi

- $ g++ --version:
   g++ (Ubuntu 4.3.2-1ubuntu12) 4.3.2
   Copyright (C) 2008 Free Software Foundation, Inc.
   This is free software; see the source for copying conditions.  There is NO
   warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

- $ arm-unknown-linux-gnueabi-g++ --version:
   arm-unknown-linux-gnueabi-g++ (crosstool-NG-1.4.0) 4.3.2
   Copyright (C) 2008 Free Software Foundation, Inc.
   This is free software; see the source for copying conditions.  There is NO
   warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

- Compiler invocations (x86 and ARM cross compiler) to produce the
   segmentation fault:
      $ g++ -o main StringTest.cpp main.cpp
      $ arm-unknown-linux-gnueabi-g++ -o main main.cpp StringTest.cpp

- Program output with the segmentation fault:
   $ ./main
   Test 01
   Test 02
   Segmentation fault

- Compiler invocations (x86 and ARM cross compiler) to avoid the
   segmentation fault:
      $ g++ -o main main.cpp StringTest.cpp
      $ arm-unknown-linux-gnueabi-g++ -o main StringTest.cpp main.cpp

- Program output with no segmentation fault:
   $ ./main
   Test 01
   Test 02
   Test 03
   Test 04
   The String

- Below are the three source files that produce the problem.

Regards,

Ben

//----------------------- Source for StringTest.h ----------------------
#ifndef STRINGTEST_H
#define STRINGTEST_H

#include <string>
using namespace std;

class UseTheString
{
public:
   void StringUsageFunction (const string &TheString,
                              string &ReturnString);
};

class StaticString
{
public:
   StaticString (void);

   string         accessableString;
   UseTheString   stringUsageObject;

private:
   static string  myString;
};

#endif

//----------------------- Source for StringTest.cpp --------------------
#include <iostream>
using namespace std;

#include "StringTest.h"

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;
}

//----------------------- Source for main.cpp --------------------------
#include <iostream>
using namespace std;

#include "StringTest.h"

StaticString   MyStaticString;

int
main (int argc,
      char **argv)
{
   cout << MyStaticString.accessableString << endl;
   return 0;
}


      


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]