0

I have a class called Dynamic Array which holds the elements of some generic object, T. I want to create a storehouse where I will hold medicine, and for this I want to use that dynamic array as the representation of the storehouse. This is my dynamic array's constructor and destructor (I have other methods for it, but they are not relevant for this question):

class DynamicArray {
private:
    int capacity, size;
    T* elements;
    
public:
    DynamicArray();
    ~DynamicArray();
};

DynamicArray::DynamicArray() {
    this->size = 0;
    this->capacity = INITIAL_CAPACITY;
    this->elements = new TElem[INITIAL_CAPACITY];
}

DynamicArray::~DynamicArray() {
    delete[] this->elements;
}

Now I want to create the storehouse. This is what I tried:

class StoreHouse {
private:
    DynamicArray medicines;

Thing is, I though I also need to create the constructor of the storehouse, since medicines is just an uninitialized dynamic array right now. So I did this (consider this to be right below the above code):

public:
    StoreHouse();

And implemented it:

StoreHouse::StoreHouse() {
    this->medicines = DynamicArray();
}

But now every time I create a new StoreHouse I get a Segmentation Fault at the end of the scope where I created it. I think there is some sort of problem with the destructor, but I can't figure out what and why. If I remove the constructor for the store house (so the above 2 blocks of code wouldn't exist) and instead of calling StoreHouse store = StoreHouse() I simply do StoreHouse store everything works, no segmentation fault. I used the DynamicArray class in other places and every time I used it I instantiated an object of it with DynamicArray da = DynamicArray(), so I explicitly called the constructor every time. It worked without problems. Here, it doesn't seem to work. I am very new to C++ so I don't really understand what's going on, even after debugging. As I said, in the end I did get it to work if I removed that constructor, but I want to know what's going on and why it didn't work the way I though it would.

abc xyz
  • 119
  • 1
  • 9
  • 2
    Please try to create a [mcve] to show us. And always remember [the rules of three/five/zero](https://en.cppreference.com/w/cpp/language/rule_of_three). – Some programmer dude Mar 20 '21 at 14:03
  • `Thing is, I though I also need to create the constructor of the storehouse, since medicines is just an uninitialized dynamic array right now.` It _does_ get constructed. – tkausl Mar 20 '21 at 14:06
  • Your template needs a copy constructor and an assignment operator. If the "other methods for it" you mentioned do include a copy constructor and assignment operator that attempt to properly copy/assign instances of this class, then they would be, very much, "relevant to this question" since this kind of a class requires a copy constructor and an assignment operator. See the linked question for more information. – Sam Varshavchik Mar 20 '21 at 14:06
  • This doesn't make sense: `this->medicines = DynamicArray();` The member `medicines` is already constructed by the time the `StoreHouse` constructor body is entered. Just like this: `DynamicArray da = DynamicArray();` make no sense (though the initialization will probably be optimized). If anything that code would be `DynamicArray da;` – WhozCraig Mar 20 '21 at 14:06
  • The real problem is in your `StoreHouse::StoreHouse()`. `this->medicines = DynamicArray();` copies a new Dynamic Array into your DynamicArray on the stack. This takes up too much memory so the program exits. Use `new` to allocate memory for DynamicArray on the [heap](https://stackoverflow.com/questions/5836309/stack-memory-vs-heap-memory) and use a pointer to keep track of its address. As an example: 1) define DynamicArray medicines: `DynamicArray *medicines;` (pointer) 2) allocate memory: `this->medicines = new DynamicArray();`. 3) call function in medicines: `medicines->function();` – Leo Qi Mar 20 '21 at 14:55

0 Answers0