【问题标题】:Not able to understand why segmentation fault?无法理解为什么分段错误?
【发布时间】:2013-02-10 12:03:07
【问题描述】:

我已经实现了下面的程序来理解复合设计模式。我也使用了 C++11 中的几个概念。但对我不利的是,该程序在运行时出现分段错误。我尝试使用 GDB 进行调试,发现 getID() 函数存在一些问题。

#0  0x08049a12 in Employee::getID (this=0x0) at Composite.cpp:27
27      int getID(){return ID;}

但我仍然无法理解该功能有什么问题?感谢有人可以提供帮助。

#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

class Employee
{
    protected:
    int ID;
    string Name;
    string Role;

    public:
    Employee(int empID, string empName, string empRole)
    {
        ID=empID;
        Name=empName;
        Role=empRole;
    }
    virtual void showDetails()=0;
    virtual void addWorker(shared_ptr<Employee> newWorker)=0;
    virtual void deleteWorker(shared_ptr<Employee> employee)=0;
    virtual ~Employee(){}
    int getID(){return ID;}
};

class Worker : public Employee
{
    public:
    Worker(int empID, string empName, string empRole)
        : Employee(empID, empName, empRole) {}

    void showDetails()
    {
        cout<<Name<<" ("<<ID<<") "<<Role<<endl;
    }

    void addWorker(shared_ptr<Employee> newWorker){};
    void deleteWorker(shared_ptr<Employee> employee){};
};

class Supervisor : public Employee
{
    private:
    vector<shared_ptr<Employee>> myTeam;

    public:
    Supervisor(int empID, string empName, string empRole)
        : Employee(empID, empName, empRole) {}

    void addWorker(shared_ptr<Employee> newWorker)
    {
        myTeam.push_back(newWorker);
    }

    void deleteWorker(shared_ptr<Employee> employee)
    {
        int pos=0;
        for (auto temp : myTeam)
        {
        if (temp->getID()!=employee->getID())
            ++pos;
        else
            myTeam.erase(myTeam.begin()+pos);
        }
    }

    void showDetails()
    {
        cout<<Name<<" ("<<ID<<") "<<Role<<" ---->"<<endl;
        for (auto worker : myTeam)
        {
        worker->showDetails();
        }
        cout<<endl;
    }
};

int main()
{
    shared_ptr<Employee> Tushar(new Worker(376653,"Tushar Shah","Team mate"));
    shared_ptr<Employee> Ranjeet(new Worker(469725,"Ranjeet Aglawe","Team mate"));
    shared_ptr<Employee> Kiran(new Supervisor(137581,"Kiran Asher","Manager"));
    shared_ptr<Employee> Namita(new Supervisor(122110,"Namita Gawde","Manager"));
    shared_ptr<Employee> Rumman(new Supervisor(122022,"Rumman Sayed","Manager"));
    shared_ptr<Employee> Rajendra(new Supervisor(111109,"Rajendra Redkar","Manager"));
    shared_ptr<Employee> Sameer(new Supervisor(106213,"Sameer Rajadhyax","Group Lead"));

    Kiran->addWorker(Tushar);
    Kiran->addWorker(Ranjeet);
    Sameer->addWorker(Kiran);
    Sameer->addWorker(Namita);
    Sameer->addWorker(Rumman);
    Sameer->addWorker(Rajendra);

    Sameer->showDetails();

    Sameer->deleteWorker(Rumman);
    Sameer->showDetails();

    return 0;
}

【问题讨论】:

  • 问题是你有四个经理,一个组长,只有两个工人。也许这里实现的设计模式实际上是控制反转?
  • 问题不在于函数本身。这是您在某处的空 Employee 实例上调用该非静态成员函数(请注意调试器消息中的 this = 0x0)。
  • 通常是一个糟糕的任务
  • 崩溃的原因在来自 GDB 的消息中:this=0x0 上调用栈,看看函数在哪里被调用,那里发生了什么。

标签: c++ design-patterns c++11 segmentation-fault


【解决方案1】:

改变

else
    myTeam.erase(myTeam.begin()+pos);

else {
    myTeam.erase(myTeam.begin()+pos);
    break;
}

修复崩溃(demo on ideone;没有breakit crashes with SIGSEGV)。问题是,即使在删除其中一个元素之后,您仍会继续迭代该向量,这是不允许的。

由于您将只删除一名工作人员(假设 ID 是唯一的),因此在删除元素后继续操作并不是一个好主意。

【讨论】:

  • 非常感谢。该程序适用于您建议的修改。再次感谢。
【解决方案2】:

您的问题是 C++ 和 STL 的一个非常常见的缺陷:您在迭代过程中不小心擦除。这里:

    for (auto temp : myTeam)
    {
    if (temp->getID()!=employee->getID())
        ++pos;
    else
        myTeam.erase(myTeam.begin()+pos);
    }

在迭代 STL 容器的同时从 STL 容器中擦除通常是不可行的。相反,请考虑“标记”稍后要删除的元素,或者使用基于整数或迭代器的迭代,以便您可以更精确地控制它。我可能会在这里使用一个简单的基于整数的 for 循环,因为无论如何你都有这个“pos”变量。

【讨论】:

  • 同意约翰。但是这个程序主要是为了理解复合设计模式,所以我尽量保持简单和快速。但我很欢迎你的建议。谢谢。
  • 简单快捷,但错误。至少你得到了三分之二! ;-)
【解决方案3】:

问题可能出在您的 deleteWorker 方法中。您在修改容器时正在迭代容器。

我建议两种可能的解决方案之一

  • 查找要删除的项目的索引,并将它们按升序存储在向量中。然后按降序迭代向量并删除它们。
  • 将 std::remove_if 与 vector::erase 结合使用。你可以看一个例子here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-14
    • 2014-05-18
    • 2019-09-20
    • 1970-01-01
    • 2022-06-19
    • 2021-07-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多