如何纠正分段错误?

时间:2013-08-11 21:52:51

标签: c++ linux valgrind

我不断收到错误Segmentation fault(core dumped),所以我运行了valgrind。这是我第一次使用它,所以不知道如何使我的代码工作。我试过改变它,但它仍然说核心倾倒,或者我收到的错误比以前多。我尝试使用gdb调试代码,但调试器无法正常工作。

和相应的product.h

#ifndef GS_PRODUCT
#define GS_PRODUCT

#include <iostream>
#include <string>

using namespace std;

class Product
{
private:
        int plu_code;
        string name;
        double price;
        bool by_weight;
        double inventory;
public:
        Product(int p_code = 0, string p_name = "", bool p_by_weight = true, double p_price = 0.0, double p_inv = 0.0);
        bool sold_by_weight(void);
        double compute_cost(double weight_or_unit);
        string get_name(void);
        int get_plu_code(void);
        double get_price(void);
        double get_inventory(void);
};

#endif

这是我的product.cpp:41

 #include <iostream>
#include <string>

#include "product.h"

using namespace std;

Product::Product(int p_code, string p_name, bool p_by_weight, double p_price, double p_inv)
{
    plu_code = p_code;
    name = p_name;
    by_weight = p_by_weight;
    price = p_price;
    inventory = p_inv;
}

bool Product::sold_by_weight(void)
{
    return by_weight;
}

double Product::compute_cost(double weight_or_units)
{
    inventory -= weight_or_units;
    return weight_or_units * price;
}

string Product::get_name(void) {
    return name;
}

int Product::get_plu_code(void) {
    return plu_code;
}

double Product::get_price(void) {
    return price;
}

double Product::get_inventory(void) {
    return inventory;
}

这是我的主程序商店

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <sstream>

#include "product.h"
#include "Tokenizer.h"
#include "LookupTable.h"

using namespace std;


LookupTable<Product *> table;
int numProducts = 0;
void checkout()
{    

}

int main()
{
    int plu;
    string name;
    int weight;
    double inv;
    double price;

    table.addRange(0, 9999);
    table.addRange(90000, 99999);

    //  std::string line;
    //Input file
    ifstream infile("inventory.csv");
    if(infile.fail())
        perror("Could not open file ");

    stringstream ss;
    while(infile.good())
    {    
        string line = "";
        //read a product info from file
        getline(infile, line);

        Tokenizer tok(line, ",");
        ss<<line;
        //string token = tok.next();

        ss >> plu >> name >> weight >> price >>inv;

        table[plu] = new Product(plu, name,weight, price,inv);
        numProducts++;
    }
    infile.close();

    checkout();
    ofstream outfile;
    outfile.open("output.csv");
    for(int i=0; i< numProducts; i++)
    {
        outfile<< table[i]-> get_plu_code() << "" << table[i]->get_name()<<""<<table[i]->sold_by_weight() <<""<<table[i]->get_price() <<""<<table[i]->get_inventory();
    }
    outfile.close();

    return 0;

} Valgrind

2 个答案:

答案 0 :(得分:1)

分段错误的含义是您尝试访问的页面没有操作所需的权限(通常是读/写权限,但可能还有可执行权限)。也就是说,系统会告诉您,您尝试访问的内容实际上并不存在或无法访问。

最终导致分段错误的问题很多。以下是其中一些:

  • 取消引用未初始化的指针。
  • 取消引用空指针。
  • 在其边界之外访问数组,从而访问随机存储器,就好像它是一个数组元素一样。
  • 正在使用已发布的对象,就好像它是实时的,例如,删除后的对象或堆栈中的对象。
  • ......以及更多类似的东西。

要获得有关实际分段错误的帮助,您需要提供一个简短但完整的示例来展示问题。引用一些你认为与问题相关的行通常不太可能真正包含实际问题。

答案 1 :(得分:0)

您似乎没有从Product :: get_inventory()返回的任何值库存。我认为这不会编译,或者你有一些你没有表现出相关的代码。最有可能的是,后者就是这种情况,并且变量库存在返回时尚未初始化。