简体   繁体   中英

How to correct a segmentation fault?

I keep getting an error Segmentation fault (core dumped), so I ran valgrind. It is my first time using it, so not sure what to do to get my code working. I've tried changing it, but it still says core dumped, or I receive more errors than I had before. I did try debugging the code with gdb, but the debugger was not working properly.

and corresponding 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

This is my 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;
}

This is my main program store

#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;

} 瓦尔格朗德

The meaning of a segmentation fault is that you tried to access a page which doesn't have the permissions (normally read/write permission but possibly also executable permission) needed for the operation. That is, the system tells you that you tried to access something which isn't really there or which is inaccessible.

There are many typical problems eventually resulting in segmentation faults. Here are a few of those:

  • An uninitialized pointer is dereferenced.
  • A null pointer is dereferenced.
  • An array is accessed outside its boundaries, thus accessing random memory as if it were an array element.
  • A released object is being used as if it is live, eg, an object after being deleted or an object which was on the stack.
  • ... and many more similar stuff.

To get help with the actual segmentation fault you have, you'll need to provide a short but complete example exhibiting the problem. Quoting a few lines which you think are related to the problem are generally rather unlikely to actually contain the actual problem.

You don't appear to have any value inventory to return from Product::get_inventory(). I would think that either this wouldn't compile, or you have some code that you haven't shown that is relevant. Most likely, the latter is the case, and the variable inventory is not yet initialized at the time that it is returned.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM