Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I cannout out why I get the error "core dumped" C++

I don’t know why when I run this piece of code :

#include <iostream>
#include <cstring>

class Masina{
    private:
        long long putere;
        int viteza;
        double masa; 
        bool calificativ; 
        char* nume;
    public:
        Masina();//constuctor default
        Masina(long long putere,int viteza,double masa,bool calificativ,char* nume);//constructor supraincarcat
        ~Masina();//destructor
    //  Masina(const Masina&) = delete;
    //  setter-e
        void setPutere(long long putere);
        void setViteza(int viteza);
        void setMasa(double masa);
        void setCalificativ(bool calificativ);
        void setNume(const char* nume);
    //  getter-e
        long long getPutere() const;
        int getViteza() const;
        double getMasa() const;
        bool getCalificativ() const;
        char* getNume() const;
};
//definire metode:
//constructor default
Masina::Masina():
    putere(2000), viteza(250), masa(1.5), calificativ(false),nume((char*)"o_marca")
{}
//constructor supraincarcat
Masina::Masina(long long putere,int viteza,double masa,bool calificativ,char* nume):
    putere(putere), viteza(viteza),masa(masa),calificativ(calificativ)
{}
//destructor default
Masina::~Masina()
{
    std::cout << "putere: "<<putere<<"\nviteza: "<<viteza<<"\nmasa: "<<masa<<"\ncalificativ: "<<calificativ<<"\nnume: "<<nume;
    std::cout<<"\nobiectul a fost sters\n\n";
}
//setter-e
void Masina::setPutere(long long putere)
{
    this->putere = putere;
}
void Masina::setViteza(int viteza)
{
    this->viteza = viteza;
}
void Masina::setMasa(double masa)
{
    this->masa = masa;
}
void Masina::setCalificativ(bool calificativ)
{
    this->calificativ = calificativ;
}
void Masina::setNume(const char* nume)
{
    strcpy(this->nume,(char*)nume);
}
//setter-e
long long Masina::getPutere() const
{
    return putere;
}
int Masina::getViteza() const
{
    return viteza;
}
double Masina::getMasa() const
{
    return masa;
}
bool Masina::getCalificativ() const
{
    return calificativ;
}
char* Masina::getNume() const
{
    return nume;
}

int main(){
    Masina* m1 = new Masina();
    {
        Masina m2 = *m1;
        m2.setNume("o_barca");
    }
    delete m1;
    return 0;
}

a segmentation fault occurs. As long as the copy constructor is not deleted I suppose that m2 should be a shallow copy of m1, thus, both char* nume keep the same value (pointing to the very same memory location). Thus, I expect that a change of the string stored at the address pointed by char * nume should change the content of both m1’s and m2’s string stored at char*nume.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You’re using c-style casts:

nume((char*)"o_marca")

That’s dangeours. Don’t. When should static_cast, dynamic_cast, const_cast and reinterpret_cast be used?

Simple c++: Live

#include <iostream>
#include <string>

struct Car {
    long long   mass      = 2000;
    int         speed     = 250;
    double      power     = 1.5;
    bool        qualifier = false;
    std::string name      = "o_marca";

    friend std::ostream& operator<<(std::ostream& os, Car const& c)
    {
        return os << "mass: " << c.mass << "\n"           //
                  << "speed: " << c.speed << "\n"         //
                  << "power: " << c.power << "\n"         //
                  << "qualifier: " << c.qualifier << "\n" //
                  << "name: " << c.name;
    }

    ~Car() { std::cout << "Object has been deleted: " << *this << "\n"; }
};

int main()
{
    Car m1, m2 = m1;
    m2.name = "o_barca";
}

Prints

Object has been deleted: mass: 2000
speed: 250
power: 1.5
qualifier: 0
name: o_barca
Object has been deleted: mass: 2000
speed: 250
power: 1.5
qualifier: 0
name: o_marca
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading