【问题标题】:Segmentation fault, shared library分段错误,共享库
【发布时间】:2023-03-12 13:46:02
【问题描述】:

当我尝试运行我的程序时出现分段错误。有人可以帮我找出我做错了什么吗?

用这个编译:

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

它应该是一个共享库和动态链接。当我尝试运行 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()
{

}

【问题讨论】:

  • 你的调试器告诉你段错误发生在什么地方?
  • 原始代码转储不是提出问题的有效方式。学习使用调试器(阅读教程、买书、上课);然后缩小您的问题范围并提出具体问题。
  • 我没有看到你链接到libSMSDisplay.so
  • if(argv[1]) 替换为if (argc &gt; 1)。如果argc &lt;= 1 访问argv[1] 是非法的。

标签: c++


【解决方案1】:

取消引用NULL 指针将导致分段错误:

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

我看不出使用动态分配对象的原因,因此建议更改为:

void sendSMS(string sms)
{
  SMSDispatch sPtr;
  sPtr.sendSMS(sms);
}

【讨论】:

    【解决方案2】:

    在 sms_out.cpp 中的 sendSMS 函数中,您声明一个指针并将其初始化为空指针 (0)。下一行尝试通过该指针访问对象并调用成员函数。由于指针为空(这意味着它没有指向有效对象),因此此操作失败并出现 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); 
    } 
    

    要修复它,首先你需要一个该类型的实例。

    根据你的需要,你可以做

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

    在第一种情况下,你也可以这样做

    SMSDispatch dp;
    dp.sendSMS(sms);
    

    在秒的情况下,您将需要在不再需要该对象后调用delete sptr;

    另外,请注意,为了编译程序,编译器需要SMSDispatch::sendSMS 函数的定义。通过包含 SMSDispatch.h 标头,您只需提供 声明

    你要么需要

    • 将 SMSDispatch.cpp 添加到 g++ 调用以直接捆绑代码或
    • 在从 SMSDispatch.cpp 构建共享库后,通过将 -lSMSDispatch 添加到 g++ 选项来重新链接共享库

    【讨论】:

    • like SMSDispatch* sPtr=&SMSDispatch; ??
    • &amp;SMSDispatch 无效,因为 SMSDispatch 是一种类型。您需要该类型的实例。根据您的需要,您可以执行 (1)SMSDispatch dp; sPtr=&amp;dp; 或 (2)sptr=new SMSDispatch;,以防万一 (1),您也可以执行 SMSDispatch dp; dp.sendSMS(sms);,万一 (2) 您需要在之后致电 delete sptr;你不再需要这个对象了。
    • 嗯,好的,谢谢,但我试着写 SMSDispatch dp; SMSDispatch* sPtr=&dp; sPtr->sendSMS(sms);但后来我得到一个错误:未定义的引用`vtable for SMSDispatch'
    • 那是因为你没有链接到生成的 libSMSDispatch.so 像这样编译你的主程序:g++ sms_out.cpp -o sms_out -lSMSDispatch(参见here 替代方案)让我知道这是否有效,我会更新我的答案
    • 试图编译,但只是得到“找不到-lSMSDispatch”
    猜你喜欢
    • 2010-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-04
    • 2019-03-19
    • 2018-06-17
    相关资源
    最近更新 更多