Writing a container class

I Keep getting this error:

1
2
Unhandled exception thrown: write access violation.
this->**data** was 0x1110112. occurred


but unsure why,

Header File:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#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 = new int[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 = new int[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 = new int[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 = new int[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 = new int[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
Last edited on
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;
Thanks, I changed it now, but still getting the same error
¿main()?

you are missing a proper copy constructor and you access out of bounds in .remove()
1
2
3
4
5
6
7
		int *myData = new int[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
This is Main

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include "stdafx.h"
#include "Map.h"
#include <iostream>
using namespace std;


int main()
{
	//testing - array with 10 elements
	myMap array(10);

	for (int i = 0; i < 10; i++)
	{
		array[i] = i + 1;
	}
	//Starting Array
	cout << "Starting Array: " << endl;
	for (int j = 0; j < array.getLength(); j++)
	{
		cout << array[j] << " " << endl;
	}
	cout << "Resizing the Array: " << endl;
	//Resize the array
	array.resize(8);
	for (int j = 0; j < array.getLength(); j++)
	{
		cout << array[j] << " " << endl;
	}

	//Insert into array
	cout << "Inserting into the Array: " << endl;
	array.insert(20, 5);

	for (int j = 0; j < array.getLength(); j++)
	{
		cout << array[j] << " " << endl;
	}

	//Delete from the Array
	cout << "Deleting from the array: " << endl;
	array.remove(4);
	for (int j = 0; j < array.getLength(); j++)
	{
		cout << array[j] << " " << endl;
	}

    return 0;
}



Where am I going out od bounds btw, I cant see where I fix that part

Nevermind, fixed that part, was obvious when I looked back at it
Last edited on
Line 62
 
     erase();

does not seem necessary - it gets rid of all the data before it can be copied.
Ah yes, I completely forgot to delete that line, that's worked somewhat, thanks :)
> Where am I going out od bounds btw, I cant see where I fix that part
in remove() function, line 140.


another problem in .resize()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/* 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 = new int[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.
Last edited on
Topic archived. No new replies allowed.