Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

c++ segmentation fault / invalid memory reference in constructor

0 votes
asked Feb 16, 2019 by Arlend
I'm coding a constructor with a default argument as a enum for a test-example. The code compiles just fine using the Online GDB c++ compiler, but it's throwing a segmentation fault error when using the clang compiler we're supposed to use for the test, too.

In the below code snippet the error occurs when the first Wagon object is constructed, for some reason it doesn't occur with the latter one, which is why I've commented it out. I've also removed most of the other methods and references in the code, to narrow it down as far as possible.

I suppose the segmentation fault /invalid memory reference (SIGSEGV) occurs somehow due to how the runtime_error is called, but I'm not sure - can someone maybe give me a pointer?

Thanks in advance!

#include <iostream>
#include <stdexcept>
#include <vector>
#include <string>

using namespace std;

enum class Feature{Bar,Restaurant,Couchette,Standard,Toilet};
const std::vector<std::string> feature_names{"bar", "restaurant", "couchette", "standard","toilet"};

class Wagon {
    int masse;
    Feature einrichtung;
    
  public:
  Wagon(int m, enum Feature f = Feature::Standard){
      
      if (m <5 || m > 100 ){throw runtime_error("mass to big or too small");}
      this->masse = m;
      this->einrichtung = f;
  }
    
    friend ostream &operator << (ostream &out, const Wagon &w){
        
        out << "output text, call a few methods, etc";
        
    }
    
};

int main(){
    
   cout << "Basisfunktionalitaet\n\nWagon\n";
    try {
        cout << Wagon{5};
        //cout << Wagon{4};
        cout << '\n';
    }
    catch (runtime_error& e) {
        cout << " Error1\n";
    }

    return 0;
}

2 Answers

0 votes
answered Feb 21, 2019 by anonymous
/*

You cannot dereference a temporary object.

Wagon{5} will break you programme.

You may create a variable which will have an adress or

you may use the operator &&

*/

// This file is a "Hello, world!" in C++ language by GCC for wandbox.
#include <iostream>
#include <stdexcept>
#include <vector>
#include <string>

using namespace std;

enum class Feature{Bar,Restaurant,Couchette,Standard,Toilet};
const std::vector<std::string> feature_names{"bar", "restaurant", "couchette", "standard","toilet"};

class Wagon {
    int masse;
    Feature einrichtung;
    
  public:
  Wagon(int m, Feature f = Feature::Standard){
      
      if (m <5 || m > 100 ){throw runtime_error("mass to big or too small");}
      this->masse = m;
      this->einrichtung = f;
  }
    
    friend ostream & operator << (ostream & out, const Wagon & w){
        
        out << "output text, call a few methods, etc";
        
        out << "\n masse "<< w.masse;
        
        return out;
    }
    
    friend ostream & operator << (ostream & out,  Wagon &&  w){
        
        out << "output text, call a few methods, etc";
        
        out << "\n masse "<< w.masse;
        
        return out;
    }
    
};

int main(){
    
   Wagon A{5};
    
   cout << "Basisfunktionalitaet\n\nWagon\n";
    try {
        cout << A;
        cout << '\n';
        cout << Wagon{6}; // temp object -> calling 2nd version of operator<<
        cout << '\n';
        cout << Wagon{4};
        cout << '\n';
    }
    catch (runtime_error& e) {
        cout << " Error1\n";
    }

    return 0;
}

// ass: MDurval12
0 votes
answered Feb 22, 2019 by shalky (140 points)

mass & einrichtung are not pointer in Wagon, so you can't use "->". 

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...