Issues with dynamic arrays, pointers and copy-constructors


I'm having a lot of issues with creating a dynamic array containing objects.

As I've understood it, because my array is handling objects, the class stored in the array must have a copy constructor or an assignment operator so that all will be copied properly.

I've successfully created this program with a normal array of defined size. Now I have a lot of problems creating the same program with a dynamic array.

Class 1 The objects to be stored:

class objToBeStored{
    string dataToBeStored;
    int sizeOfArray;
    string *storedArray;
    objToBeStored(); //empty constructor
    objToBeStored& operator =(const objToBeStored& o); // assignment operator
    ~objToBeStored(); //destructor (no code inside);
    bool getData(istream &stream);
    //.....other methods to do stuff

objToBeStored& objToBeStored::operator=(const objToBeStored& o){
    if(this != o){
        dataToBeStored = o.dataToBeStored;
        for (int i = 0; i < sizeOfArray; i++){
            storedArray[i] = o.storedArray[i];
    return *this;

void objToBeStored::getData(istream &stream){
    stream >> dataToBeStored >> sizeOfArray;
    storedArray = new string[sizeOfArray];
    for(int i = 0; i < sizeOfArray; i++){
        stream >> storedArray[i];
    return !stream.eof();

//.....other methods to do stuff

Class 2 contains the dynamic array that stores the above objects. Everything is working,except how I declared my dynamic array and the functions handling it. Therefore I will write this code below:

class storageArrayClass{    
    storageArrayClass *store;
    storageArrayClass *storptr;
    int numberOfstored;
    storageArrayClass(); //empty constructor
    void addElm(objToBeStored & o);
    //other functions to do stuff    
storageArrayClass::storageArrayClass(){ //constructor
    numberOfstored = 0;

void storageArrayClass(istream &stream) {
    objToBeStored o;
    if(numberOfstored == 0){ //check it this is the first element
        store = new objToBeStored[1];   //create a new array with length 1
        store[(numberOfstored] = o; //store object
        objToBeStored tmpStore = new objToBeStored[(numberOfstored+1];  //create a temp. array with 1 more position
        for(int i=0; i < numberOfstored; i++){
            tmpStore[i] = store[i]; //copy original array to the temp. array
            storptr = &tmpStore[i]; // increment a point
        storptr++; //increment pointer to last position
        *storptr = o; //store object in last position
        delete[] store; //delete the original array
        store = new objToBeStored[(numberOfstored+1]; //create a new original array
        store = tmpStore;//copy temp. array

I manage to add 3 objects to my dynamic array before I get the following error:

Process returned -1073741819 (0xC0000005) execution time : 5.059 s

Please help. I've read countless threads here, but I cannot get it to work.

asked on Stack Overflow Aug 19, 2020 by NoviceJava • edited Aug 19, 2020 by Remy Lebeau

0 Answers

Nobody has answered this question yet.

User contributions licensed under CC BY-SA 3.0