1

I am trying to create my own array within a class, with functions to insert into it, delete etc.. My array has capacity - max array's size, size - how many elements it holds, *data - a pointer to data. So when the user tries to insert an element and the array is full, the capacity would double in my resize() function, i would create a temporary array newData[capacity] copy everything there, then delete my original data to get rid of that memory and then assign newData to data. Now I don't know if that is a stupid solution, but it works the first time, but when I resize the second time I am getting strange numbers. For the tests I put the starting capacity to 2. This is myArray.cpp file:

#include <iostream>
#include "myArray.h"

using namespace std;

myArray::myArray()
{
    size = 0;
    capacity = 2;
    data = new int[capacity];
}

void myArray::setData(int n, int idx) {
    data[idx] = n;
}
int myArray::getData(int idx) {
    return data[idx];
}

void myArray::insert(int num) {
    size++;
    if(size > capacity) resize();
    setData(num, size - 1);
}
void myArray::insert(int num, int idx) {
    if(idx == size + 1) insert(num);
    else {
        size++;
        if(size > capacity) resize();
        for(int i = size; i > idx; i--) {
            data[i] = data[i - 1];
            if(i - 1 == idx) data[idx] = num;
        }
    }
}
void myArray::remove(int idx) {
    if(idx == size) {
        delete &data[size];
        size--;
    }
    else {
        for(int i = idx; i < size; i++) {
            data[i] = data[i+1];
        }
        size--;
    }
}
void myArray::resize() {
    cout << "Resizing" << endl;
    capacity *= 2;
    int *newData = new int[capacity];
    for(int i = 0; i < size; i++) {
        newData[i] = data[i];
    }
    delete[] data;
    data = newData;
    delete[] newData;
}

int& myArray::operator[](int idx) {
    return data[idx];
}

void myArray::print() {
    for(int i = 0; i < size; i++) {
        cout << data[i] << " ";
    }
    cout << endl;
}

myArray::~myArray()
{
    //dtor
}

Just ignore all of the functions I guess, all of the circus must happen in the resize() function. This is the header file

#ifndef MYARRAY_H
#define MYARRAY_H


class myArray
{
    public:
        myArray();
        virtual ~myArray();

        void print();

        void setData(int n, int idx);
        int getData(int idx);

        void insert(int num);
        void insert(int num, int idx);
        void remove(int idx);
        void resize();

        int &operator[](int);

    protected:

    private:
        int size;
        int capacity;
        int *data;
};

#endif // MYARRAY_H

And this are my tests in main()

#include <iostream>
#include "myArray.h"

using namespace std;

int main()
{
    myArray array;
    array.insert(1);
    array.print();
    array.insert(4);
    array.print();
    array.insert(3);
    array.print();
    array.insert(5, 3);
    array.print();
    array.remove(1);
    array.print();
    array.insert(6);
    array.print();
    array[2] = 2;
    array.print();
    array.insert(3, 0);
    array.print();
    return 0;
}

And this is what I see in the ouput:

1
1 4
Resizing (everything worked fine)
1 4 3
1 4 3 5
1 3 5
1 3 5 6
1 3 2 6
Resizing (everything is not fine)
3 18248184 18219200 2 6
7

1 Answer 1

1

In resize, the delete[] newData; statement deletes the memory you just allocated, leaving data as a dangling pointer because it now points at memory that has been deallocated.

The solution is to remove the delete[] newData; statement from resize.

You should also add code to the destructor to free up the memory you've allocated.

Sign up to request clarification or add additional context in comments.

5 Comments

Oh, this actually did solve the problem, but it works for me one time and doesn't the another time. I deleted the delete[] newData, and I also added data = new int[capacity]; after the delete[] data; So now one time I compile it outputs 3 1 3 2 6 like it should, but the other time it is stuck on the word Resizing in the ouput and nothing happens. What is this magic, how can it work one time and it won't the another time?
@Norbertas Why are you assigning again to data? You've already did that with data = newData, so that the newly allocated memory will be retained and used.
Sorry my bad on that one. But still, if I leave only the delete[] data; and then data = newData; most of the time it prints the normal output, but one in 6 tries It says Resizing and doesn't print the array.
@Norbertas There are at least 2 issues in remove that might contribute to that.
I figured in my loop (int the remove function), when I am removing elements and pushing all of them to the left side, when I get to the last element I am trying to assign the last element's value to the value of the element that is on it's right, but there is nothing there so it gets assigned to some trash. But even if I fix that it still won't always print the output.. I guess I am just too confused

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.