Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7734589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:17:14+00:00 2026-06-01T07:17:14+00:00

I get Segmentation Fault when I try to run my program. Can someone please

  • 0

I get Segmentation Fault when I try to run my program. Can someone please help me find out what Im doing wrong?

Compiling with this:

    g++ sms_out.cpp -o sms_out
    g++ -c -fPIC SMSDispatch.cpp
    g++ -shared SMSDispatch.o -o libSMSDispatch.so

It should be a shared library and dynamic linking. I get Segmentation Fault when I try to run sms_out.

//sms_out.cpp

#include <iostream>
#include<cstdlib>
#include<fstream>
#include<sstream>
#include<string>
#include "SMSDispatch.h"
using namespace std;

string sms = "";

void sendSMS(string sms)
{
  SMSDispatch* sPtr=0;
  sPtr->sendSMS(sms);
}

int main(int argc, char *argv[])
{
  if(argv[1])
  {
    string input = argv[1];
    string test = "--author";


 if(input == test)
    {
      cout << "s149113" << 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 &)
  {
  }
  //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 << sms << endl;
  sendSMS(sms);

}

//SMSDispatch.h

#include <string>
#ifndef SMSDISPATCH_H
#define SMSDISPATCH_H
using namespace std;

class SMSDispatch{
  public:
  virtual void sendSMS(string json);
  };
#endif

//SMSDispatch.cpp

 #include <iostream>
 #include <fstream>
 #include "SMSDispatch.h"
 using namespace std;

/*virtual*/void SMSDispatch::sendSMS(string json)
{
  ofstream myfile;
  myfile.open ("sms_out.log");
  myfile << json;
  myfile.close();
}

int main()
{

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T07:17:16+00:00Added an answer on June 1, 2026 at 7:17 am

    In your sendSMS function in sms_out.cpp you declare a pointer and initialize it to null-poiter (0). The next line tries to access an object via that pointer and call a member function. Since the pointer is null (which means it does not point to a valid object), this operation fails wilth a seg.fault

    void sendSMS(string sms) 
    { 
      SMSDispatch* sPtr=0; // this sets pointer to null
                           // assign the address of a valid `SMSDispatch` object instead
      sPtr->sendSMS(sms); 
    } 
    

    To fix it, forst you need an instance of that type.

    Depending on your need you can do

    • SMSDispatch dp; sPtr=&dp; or
    • sptr=new SMSDispatch;

    In the first case, you could just as well do

    SMSDispatch dp;
    dp.sendSMS(sms);
    

    In the secons case you will need to call delete sptr; after you don’t need the object any more.

    Also, note that in order to compile the program, the compiler will need the definition of the SMSDispatch::sendSMS function. By including the SMSDispatch.h header, you are only supplying the declaration.

    You will either need to

    • add SMSDispatch.cpp to the g++ invokation for direct bundling of the code or
    • link agains the shared library by adding -lSMSDispatch to g++ options after building the shared library from SMSDispatch.cpp
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I get a segmentation fault when I try to execute my project. At the
I get a segmentation fault whenever I try to call authorize on the Authorize.net
I get a segmentation fault after free ing a certain pointer: free(studentDB->name); I can
Why do I get a segmentation fault when I try to show a negative
Possible Duplicate: Why do I get a segmentation fault when writing to a string?
In the following code I get a segmentation fault: Set *getpar() {...} char function(...)
I am working on a program which generates a segmentation fault and I cannot
I get segmentation fault accessing an object which looks valid and fully accessible in
I am getting a segmentation fault from bash when I try to SSH to
Trying to create a QtRuby application, I get the following error: /usr/lib64/ruby/site_ruby/1.8/Qt/qtruby4.rb:2144: [BUG] Segmentation

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.