Reputation: 171
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
Upvotes: 1
Views: 120
Reputation: 32727
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.
Upvotes: 1