#pragma once
#include <string>
#include <cassert> //Needed for assert, see report for more details on why I'm using this
class myMap
{
private:
int myLength; //Number of Array Member/Length of array
int *data; //Pointer to it's posistion in the array
public:
//Constructors
myMap() : myLength(0), data(nullptr)//Empty Array
{
}
myMap(int myLength) : myLength(myLength)
{
data = newint[myLength];
}
//Decontructor
~myMap()
{
delete[] data;
//Don't need to set to Null or 0 here as the object is destroyed after this function
}
//Delete Function
void erase()
{
delete[] data;
data = nullptr;//set to Null or otherwise will be pointing at deallocated memory
myLength = 0;
}
int& operator[](int index) //Overloading the [] Operator to access elements of the array, checking the bounds of the index to ensure it's a valid array using the assert function and returning the length of the array
{
assert(index >= 0 && index < myLength);
return data[index];
}
//Resizes the aray destroying all elements in the process
void reallocate(int newLength)
{
erase();//delete any exsisting elements
//If empty, return here
if (newLength <= 0)
return;
//Allocate new elements for the array
data = newint[newLength];
myLength = newLength;
}
//Resize the array, keeping the exsisting elements (Useful for modifying or editing arrays)
void resize(int newLength)
{
erase();
//if Array is already the right length we needed
if (newLength == myLength)
return;
//Resiziing to an empty array
if (newLength <= 0)
{
erase();
return;
}
//Allocate a new array, copy the elements one by one to the new array, destroy the old array and make data point to the new array
//New Array
int *myData = newint[newLength];
//Copy the elements to the new array
if (myLength > 0)
{
int copying = (newLength > myLength) ? myLength : newLength;
for (int index = 0; index < copying; index++)
{
myData[index] = data[index];
}
//destroy the old array, clearing memory
delete[] myData;
//Point at the new array just created
data = myData;
myLength = newLength;
}
}
//Insert
void insert(int value, int index)
{
//Create an array larger than the old array
int *myData = newint[myLength + 1];
//copy all to the new array
for (int i = 0; i < index; i++)
{
myData[i] = data[i];
}
//insert new value into the array
myData[index] = value;
delete[] data;
data = myData;
myLength++;
}
//Delete Function
void remove(int index)
{
//If this is the last elemnt in the array, set to empty and return, preventing a memory leak and/or crash
if (myLength == 1)
{
erase();
return;
}
//Create Array smaller than the original
int *myData = newint[myLength - 1];
//Copy all from the old array
for (int i = 0; i < index; i++)
{
myData[i] = data[i];
}
//Copy all values afer the removed one
for (int j = index + 1; j < myLength; j++)
{
myData[j + 1] = data[j];
}
delete[] data;
data = myData;
myLength--;
}
int getLength()
{
return myLength;
}
};
It's on this line:
1 2 3 4 5
//copy all to the new array
for (int i = 0; i < index; i++)
{
myData[i] = data[i];
}
But as far as I can see, it should be working fine
The problem is on line 87/90. You delete myData and then set the now invalid pointer to the member data which becomes invalid as well. So line 87 should be delete[] data;
you are missing a proper copy constructor and you access out of bounds in .remove()
1 2 3 4 5 6 7
int *myData = newint[myLength - 1];
//...
// Copy all values afer the removed one
for(int j = index + 1; j < myLength; j++) {
myData[j + 1] = data[j];
}
suppose that myLength=5, then myData may hold 5-1=4 elements
in the last iteration of the loop, j reaches myLength-1, that is 4
so you are accessing myData[5], but the last valid index is 3
/* my comments are written in this style */
// Resize the array, keeping the exsisting elements (Useful for modifying or
// editing arrays)
void resize(int newLength) {
erase(); /* you won't keep the existing elements */
//...
// Allocate a new array, copy the elements one by one to the new array,
// destroy the old array and make data point to the new array New Array
int *myData = newint[newLength];
// Copy the elements to the new array
if(myLength > 0) { /* because you called .erase(), at this point myLenght is 0 */
int copying = (newLength > myLength) ? myLength : newLength;
for(int index = 0; index < copying; index++) {
myData[index] = data[index]; /* because you called .erase(), here data is nullptr */
}
// destroy the old array, clearing memory
delete[] myData; /* another time the same error, you invalidate `myData' */
// Point at the new array just created
data = myData; /* and then make `data' invalid */
myLength = newLength;
}
}
dont edit the OP, make a new post with your updated code.