How to correct a segmentation fault? - c++

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, e.g., 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.

Related

Program seems to be accepting file input but not printing the information

Hello and thanks to all who considered reading. I recently started learning c++ in a class and I am just now learning file input and output. I have the following program,
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <fstream>
using namespace std;
class Cups{
string owner;
public:
string type;
float size;
Cups(string owner, string type, float size){
this->owner = owner;
this->type = type;
this->size = size;
}
string get_owner(){
return owner;
}
};
vector<Cups> extract(string filename){
ifstream inFile;
string line;
string owner, type;
float size;
vector<Cups> o;
inFile.open(filename);
if(!inFile.is_open()){
cout<<"Error, file not found..."<<endl;
exit(1);
}
while(!inFile.eof()){
getline(inFile, line);
stringstream ss(line);
getline(ss, owner, ',');
getline(ss, type);
if(type.compare("Short")==0){
size = 1.0;
}
else if(type.compare("Tall")==0){
size = 1.5;
}
else if(type.compare("Grande")==0){
size = 2.0;
}
else if(type.compare("Venti")==0){
size = 2.5;
}
else{
cout<<"Error, "<<owner<<"'s cup size not found."<<endl;
exit(1);
}
o.push_back(Cups(owner, type, size));
}
inFile.close();
return o;
}
int main(int argc, char** argv){
if(argc < 2){
cout<<"Please compile with and orders file."<<endl;
exit(1);
}
int i;
vector<Cups> orders = extract(argv[1]);
for(i = 0; i < orders.size(); i++){
cout<<orders[i].get_owner()<<", "<<orders[i].type<<endl;
cout<<orders[i].size<<endl;
}
}
with the following file, input as an executable argument
Natassa,Grande
Demy,Tall
Elena,Short
The program outputs the warning when you compile without a file just fine, however when running it, it outputs
Natassa, Grande
2
Demy, Tall
1.5
Elena, Short
1
, Short
1
I've tried running it in gdbonline and there seems to be a problem with my extraction perhaps, but I can't figure it out. Thanks for all who bothered to read and a bigger thanks to any who help.
Edit: Added the not symbol ot the inFile.eof() check, but still getting the ghost line shown above. I would post to codereview but this code is still not working correctly.

Ifstream not retrieving data using array in C++

I've been trying to read data from employee-info.txt using ifstream and storing it in an array, but it did not read anything.
The main goal of my code is, every time that it was able to read a value in employee-info.txt, It will loop and the integer variable valueChecker will increment by one. After the loop is done, the value of valueChecker will be returned in order to determine how many strings are retrieved from employee-info.txt. Based on the contents of employee-info.txt, it should return an int of 8, but it is only returning an int of 0, which is the initialized value.
I also checked through debugger and the record array did not read anything from the file. I already checked the address of the file and it is correct.
Here is the code in Employee.h:
#pragma once
#include<string>
#include<iostream>
class Employee
{
public:
struct EmployeeRecord {
static const int recordSize = 100;
static const int fieldSize = 4;
std::string record[recordSize][fieldSize];
};
public:
Employee();
~Employee();
int employeeDataChecker();
void employeeWriteData();
void employeeDisplayData();
EmployeeRecord& employeeReturnRecordArray();
private:
EmployeeRecord emp_record;
};
Employee.cpp:
#include "Employee.h"
#include <string>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
int Employee::employeeDataChecker()
{
//Check if there are data in the employee-info.txt
EmployeeRecord emp;
int valueChecker = 0;
std::ifstream inFile;
inFile.open("C:\\Users\\RJ\\Desktop\\employee-info.txt");
for (int index = 0; index < emp.recordSize; index++) {
for (int index2 = 0; index2 < emp.fieldSize; index2++) {
while (inFile >> emp.record[index][index2]) {
valueChecker++;
}
}
}
inFile.close();
return valueChecker;
}
employee-info.txt:
ID Firstname Lastname Sales
1 Joe Satriani 500000.00
Yes, first, you have to check if file is correctly opened (i.e. inFile.is_open()).
Then, forget iteration on fields and try to do something like this:
int id;
char firstname[64] = { 0 };
char lastname[64] = { 0 };
float sales;
inFile >> id;
inFile.get(firstname, 64, ' ');
inFile.get(lastname, 64, ' ');
inFile >> sales;
On failure, [inFile >>] operator could return false and then it wont increment valueChecker.

C++ Array pointer-to-object error

I am having what seems to be a common issue however reading through the replies to the similar questions I can't find the solution to my issue at all as I have already done what they are suggesting such as making the variable an array. I have the following code:
#include "stdafx.h"
#include <cstring>
#include <fstream>
#include <iostream>
#include <string>
#include <algorithm>
#include <future>
using namespace std;
string eng2Str[4] = { "money", "politics", "RT", "#"};
int resArr[4];
int main()
{
engine2(eng2Str[4], resArr[4]);
system("Pause");
system("cls");
return 0;
}
void engine2(string &eng2Str, int &resArr)
{
ifstream fin;
fin.open("sampleTweets.csv");
int fcount = 0;
string line;
for (int i = 0; i < 4; i++) {
while (getline(fin, line)) {
if (line.find(eng2Str[i]) != string::npos) {
++fcount;
}
}
resArr[i] = fcount;
}
fin.close();
return;
}
Before you mark as duplicate I have made sure of the following:
The array and variable I am trying to assign are both int
Its an array
The error is:
expression must have pointer-to-object type
The error is occurring at the "resArr[i] = fcount;" line and am not sure why as resArr is an int array and I am trying to assign it a value from another int variable. I am quite new to C++ so any help would be great as I am really stuck!
Thanks!
The problem is that you've declared your function to take a reference to a single string and int, not arrays. It should be:
void engine2(string *eng2Str, int *resArr)
or:
void engine2(string eng2Str[], int resArr[])
Then when you call it, you can give the array names as arguments:
engine2(eng2Str, resArr);
Another problem is the while loop in the function. This will read the entire file during the first iteration of the for() loop. Other iterations will not have anything to read, since it will be at the end of the file already. You could seek back to the beginning of the file, but a better way would be to rearrange the two loops so you just need to read the file once.
while (getline(fin, line)) {
for (int i = 0; i < 4; i++) {
if (line.find(eng2Str[i]) != string::npos) {
resArr[i]++;
}
}
}
I would suggest to use std::vector instead of pure C array.
In your code, there are more issues.
You are passing the fourth element of both arrays to the engine2 function.
From your definition of void engine2(string &eng2Str, int &resArr) you expect reference to a string (not array / vector) and an address / reference of int - you need to pass an pointer to the first element of resArr.
#include <cstring>
#include <fstream>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <future>
using namespace std;
vector<string> eng2Str = { "money", "politics", "RT", "#" };
int resArr[4] = {};
void engine2(const vector<string>& eng2Str, int* resArr)
{
ifstream fin;
fin.open("sampleTweets.csv");
int fcount = 0;
string line;
for (int i = 0; i < 4; i++)
{
while (getline(fin, line))
{
if (line.find(eng2Str[i]) != string::npos)
{
++fcount;
}
}
resArr[i] = fcount;
}
fin.close();
return;
}
int main()
{
engine2(eng2Str, resArr);
system("Pause");
system("cls");
return 0;
}

Reading in input to construct an object

I am trying to read a string line for line down a .txt file in order to initiate an array of objects using a constructor that takes a string.
The text file is written like
TransAm
Mustang
Corvette
I feel like my loop is not iterating the information I want to be set correctly. Is there an easy way of accomplishing this?
main.cc
#include <string>
#include <iostream>
#include "Car.cc"
#include <fstream>
using namespace std;
int main()
{
Car cars[3];
string STRING;
ifstream infile;
infile.open("cars.txt");
// THIS IS HOW IT'S ACHIEVED USING FOR-LOOP - Sam
for(int i = 0; i<3 && infile;++i){
getline(infile,STRING);
cars[i].setName(STRING);
}
/* THIS IS WHAT I HAD
while(!infile)
{
getline(infile,STRING);
for(int i = 0; i<sizeof(cars);i++){
cars[i].setName(STRING);
}
}
*/
infile.close();
for(int j = 0;j<sizeof(cars);j++){
cars[j].print();
}
}
Car.h
#include <string>
using namespace std;
class Car{
public:
Car();
Car(string);
string getName();
void setName(string);
void print();
private:
string name;
};
Car.cc
#include <string>
#include "Car.h"
using namespace std;
Car::Car()
{
}
Car::Car(string s)
{
setName(s);
}
void Car::setName(string s)
{
name = s;
}
string Car::getName()
{
return name;
}
void Car::print()
{
cout << name;
}
These points need to be corrected:
while (!infile) prevents you from entering the loop.
You don't need two loops.
You can modify your loop like this:
for (int i = 0; i < sizeof(cars) && getline(infile, STRING); ++i)
cars[i].setName(STRING);
Or like this:
for (int i = 0; i < sizeof(cars) && infile; ++i) {
getline(infile, STRING);
cars[i].setName(STRING);
}
Your loop does at the moment nothing if the file is correctly opened. It will only enter if the call to open was unsuccessful.
Change your loop to either
while (getline(infile,STRING))
{
//...
}
or
while (infile)
{
//...
}
As it's been said, "Change while(!infile) to while(getline(infile,STRING))" but do not forget to remove the getline(infile,STRING); afterwards.

Failed to generate disassembly for stack frame because the URL cannot be translated & Segmentation fault: 11

I am still a newbie in programming. I am writing a program of 2D Snell's Law. I know the problem may due to wrong localisations in Xcode, but I am writing in C++ only and g++ even gives me segmentation fault error after compiling successfully.
Here are my code for main function:
#include <string>
#include "Snell.hpp"
int main(int argc, const char * argv[]){//thread 1 exc_bad_access (code=2 address=0x7fff5f238304)
string filename;
double time;
Snell S[3600];
for (int i=1; i<=1; i++) {
while (S[i].angle_tr>0) {
filename="VPVSMOD"+to_string(i)+".txt";
S[i].Open(filename);
time=S[i].Locate(i);
cout<<"The "<<i<<"th event takes "<<time<<" seconds to reach the destination"<<endl;
S[i].angle_tr-=0.01;
}
}
return 0;
}
Here is the code for Snell.hpp
#ifndef Snell_hpp
#define Snell_hpp
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <string>
#include <algorithm>
#include <cmath>
using namespace std;
class Snell{
private:
double GetV(double lat,double dep);
int ny,nz,time;
double la[30],h[20],v[10][30];
double lat,alt,step;
public:
Snell();
void Open(string filename);
double Locate(int i);
double angle_tr;
};
#endif /* Snell_hpp */
and Snell.cpp:
#include "Snell.hpp"
Snell::Snell(){
ny=1,nz=3,time=0;
lat=0,alt=0,step=1;
angle_tr=M_PI/2;
}
void Snell::Open(string filename){
ifstream fin(filename);
stringstream ss;
string str,tok;
for (int i=0; i<nz; i++) {
(getline(fin, str));
ss.str(str);
for (int j=0; j<ny; j++) {
getline(ss, tok, ',');
v[i][j]=stod(tok);
cout<<v[i][j]<<",i="<<i<<",j="<<j<<endl;
}
ss.clear();
}
fin.close();
angle_tr=v[1][0]/v[0][0];
}
double Snell::GetV(double lat, double dep){
int index_la = 0,index_dep = 0;
index_dep=round(dep);
return (v[index_dep][index_la]+v[index_dep+1][index_la])/2;
}
double Snell::Locate(int i){
string filename;
double count_t=0;
double latt=lat,altt=alt,step_altt_all=0,angle=0,angle_p=0;
double vsy,vsz;
double vs,vs_n;
ofstream fout;
angle=M_PI/2-atan(angle_tr);
vs=GetV(lat, alt);
filename="Test"+to_string(i)+"_"+to_string(time)+".txt";
fout.open(filename,ios::out);
fout<<lat<<","<<alt<<endl;
while (altt!=2) {
//cout<<"Compute Velocity in each dimension"<<endl;
angle_p=angle;
vsy=vs*cos(angle);
vsz=vs*sin(angle);
//cout<<"Check Velocity"<<endl;
if (vsy==0||vsz==0) {
break;
}
//cout<<"Compute reflection point"<<endl;
step_altt_all=step/vsz;
count_t=count_t+step/vsz;//time plus one
latt=latt+vsy*(step_altt_all);
step_altt_all=0;
altt=altt+step;
//cout<<"Compute New Velocity"<<endl;
vs_n=GetV(latt,altt);
if ((vs_n*cos(angle)/vs)>1) {
break;
}
else{
angle=M_PI/2-asin(vs_n*cos(angle)/vs);
vs=vs_n;
if (angle!=angle_p)
fout<</*"position:"<<*/latt<<","<<altt<<endl;
}
}
fout.close();
filename="Result"+to_string(i)+"_"+to_string(time)+".txt";
fout.open(filename);
fout<<0<<" "<<latt<<" "<<altt<<" "<<step<<endl;
fout.close();
return count_t;
}
My immediate guess is: You must have blown your stack. Please see why is stack memory size so limited?
....And yes, On my platform, my guess was correct...
Reproducing your program, but modifying your main.cpp ...
int main(int argc, const char * argv[]){//thread 1 exc_bad_access (code=2 address=0x7fff5f238304)
string filename;
double time;
//Snell S[3600];
std::cout << sizeof(Snell) << " bytes" << std::endl;
return 0;
}
It gives an output of
2848 bytes
....And You are trying to allocate 3600 of them... ~10MB!!
The solution to that is to allocate it on the heap using a std::unique_ptr or better still, your good friend, std::vector.
Modify your main to this
#include <string>
#include <memory>
//or #include <vector>
#include "Snell.hpp"
int main(int argc, const char * argv[]){//thread 1 exc_bad_access (code=2 address=0x7fff5f238304)
string filename;
double time;
std::unique_ptr<S[]> p(new Snell[3600]);
//or std::vector<Snell> S(3600);
for (int i=1; i<=1; i++) {
while (S[i].angle_tr>0) {
filename="VPVSMOD"+to_string(i)+".txt";
S[i].Open(filename);
time=S[i].Locate(i);
cout<<"The "<<i<<"th event takes "<<time<<" seconds to reach the destination"<<endl;
S[i].angle_tr-=0.01;
}
}
return 0;
}