shahril
shahril

Reputation: 11

Segmentation Fault With Array object when calling constructor

This is the base class

class Product{

    protected:
        
        string model;
        double price;
        int qty;
        
    public:
        
        Product() { // default constructor
            model = "";
            price = 0.00;
            qty = 0;
        }
        
        //setter method:
        void setProduct(string m, double p, int q){ // set the values of Product (model, price, and quantity)
            model = m;
            price = p;
            qty = q;
        }
        
        //getter method:
        string getProduct(){ // get the product by returning the string model (unique data in the list)
            return model;
        }
        
        int countLength(); // functino to count the number of products in a list
        int seqSearch(string item, int length); // sequential search to search thru the list
        void add();
        void insertAt(int length, string newModel, double newPrice, int newQty); // add() and insertAt method used to insert a product into a list
        void del(); 
        void removeAt(int loc, int length); // del() and removeAt() methods is used to remove a product from a list
        void view(); // display all info in a list
        void select(); // method to select a product/item in a list
        void view(int location); // view a selected product in a list... overloading the view() method
        
        ~Product(){ // destructor
            delete [] this;
        }
};

This is the derived classes

class Computer: public Product{
    
    public:
        
        //constructors
        Computer(){} // default constructor
        Computer(string m, double p, int q); // overloading constructor for preset data
                
        void view();
        
        ~Computer(){ // destructor
            delete [] this;
        }
 };

class Accessory: public Product{
    
    public:
        
        //constructoirs
        Accessory(){} // default constructor
        Accessory(string m, double p, int q);
        
        void view();
        
        ~Accessory(){ // destructor
            delete [] this;
        }
};

class Printer: public Product{
    
    public:
        
        //constructors
        Printer(){} // default constructor
        Printer(string m, double p, int q);
        
        void view();
        
        ~Printer(){ // destructor
            delete [] this;
        }
};

class Other: public Product{
    
    public:
        
        //constructors
        Other(){} // default constructor
        Other(string m, double p, int q);
        
        void view();
        
        ~Other(){ //destructor
            delete [] this;
        }
};

Here is the overloaded constructors of derived classes

Computer::Computer(string m, double p, int q){
    this->setProduct(m, p, q);
}

Accessory::Accessory(string m, double p, int q){
    this->setProduct(m, p, q);
}

Printer::Printer(string m, double p, int q){
    this->setProduct(m, p, q);
}

Other::Other(string m, double p, int q){
    this->setProduct(m, p, q);
}

This is the objects declarations

Computer *Computers = new Computer[maxSize];
Accessory *Accessories = new Accessory[maxSize];
Printer *Printers = new Printer[maxSize];
Other *Others = new Other[maxSize];

The problem is when I call the Object[i] = Class(string, double, int) and it is a segmentation fault I guess

void presetData(){
    Computers[0] = Computer("C1C1C1", 2000.00, 6);
    Computers[1] = Computer("C2C2C2", 2500.00, 2);
    Accessories[0] = Accessory("A1A1A1", 200.00, 6);
    Printers[0] = Printer("P1P1P1", 300.00, 4);
    Others[0] = Other("O1O1O1", 100.00, 1);
}

Why can't I access the Object[i] and it gives segmentation fault?

Upvotes: 1

Views: 85

Answers (1)

molbdnilo
molbdnilo

Reputation: 66371

When the temporary object Computer("C1C1C1", 2000.00, 6) is destroyed, it does delete [] this;, but this does not point to the first element of an array created with new.
This has undefined behaviour, and you're lucky it crashes.
To make it even worse, it does this twice - once in Product::~Product and once in Computer::~Computer.

Remove all the lines that say delete [] this; - this thing does not belong in a destructor, and you have never seen any example that does it.
(Unless it was an example of things that you shouldn't do.)

Then read some more about constructors and destructors, and how to work with dynamic allocation, in your favourite C++ book.

Upvotes: 2

Related Questions