title: Segmentation fault, shared library
body: <p>I get Segmentation Fault when I try to run my program. Can someone please help me find out what Im doing wrong?</p>
  
  <p>Compiling with this:</p>
  
  <pre><code>    g++ sms_out.cpp -o sms_out
      g++ -c -fPIC SMSDispatch.cpp
      g++ -shared SMSDispatch.o -o libSMSDispatch.so
  </code></pre>
  
  <p>It should be a shared library and dynamic linking. I get Segmentation Fault when I try to run sms_out.</p>
  
  <p>//sms_out.cpp</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include&lt;cstdlib&gt;
  #include&lt;fstream&gt;
  #include&lt;sstream&gt;
  #include&lt;string&gt;
  #include "SMSDispatch.h"
  using namespace std;
  
  string sms = "";
  
  void sendSMS(string sms)
  {
    SMSDispatch* sPtr=0;
    sPtr-&gt;sendSMS(sms);
  }
  
  int main(int argc, char *argv[])
  {
    if(argv[1])
    {
      string input = argv[1];
      string test = "--author";
  
  
   if(input == test)
      {
        cout &lt;&lt; "s149113" &lt;&lt; endl;
        return 0;
      }
    }
  
    string line = "";
    string file = "sms_out.txt";
    ifstream myfile(file.c_str());
  
  
  while(getline(myfile, line))
  {
        string idnr, landcode, number, error;
        istringstream linestream(line);
  
        unsigned short errorcode;
  
        //Split the sentence
        getline(linestream, idnr, '\t');
        getline(linestream, landcode, ':');
        getline(linestream, number, '\t');
        getline(linestream, error);
  
    if(idnr == "") break;
  
    //Make string to int
      try
    {
      errorcode = atoi(error.c_str() );
    }
    catch (exception &amp;)
    {
    }
    //Put together landcode and tlfnumber
    string nr = landcode + number;
  
    string txt = "Thank you for your vote!";
    if(errorcode == 100) txt = "Invalid question, please try again";
    else if(errorcode == 110) txt = "Sorry, only one vote pr. number";
    else if(errorcode == 200) txt = "Invalid alternative, please try again";
    else if(errorcode == 300) txt = "Missing a statement after other, please try again";
    else if(errorcode == 999) txt = "An error occurred, please try again";
  
    sms += "{\"ID\":" + idnr + ",\"nr\":" + nr + ",\"txt\":" + "\"" + txt + "\"" + "}\n";
    }
    cout &lt;&lt; sms &lt;&lt; endl;
    sendSMS(sms);
  </code></pre>
  
  <p>}</p>
  
  <p>//SMSDispatch.h</p>
  
  <pre><code>#include &lt;string&gt;
  #ifndef SMSDISPATCH_H
  #define SMSDISPATCH_H
  using namespace std;
  
  class SMSDispatch{
    public:
    virtual void sendSMS(string json);
    };
  #endif
  </code></pre>
  
  <p>//SMSDispatch.cpp</p>
  
  <pre><code> #include &lt;iostream&gt;
   #include &lt;fstream&gt;
   #include "SMSDispatch.h"
   using namespace std;
  
  /*virtual*/void SMSDispatch::sendSMS(string json)
  {
    ofstream myfile;
    myfile.open ("sms_out.log");
    myfile &lt;&lt; json;
    myfile.close();
  }
  
  int main()
  {
  
  }
  </code></pre>