Deleting Dynamically Allocated Arrays

I am trying to delete these arrays NumArray1.array and NumArray2.array at the end of my program so that there is not a memory leak. However I always get a double free or corruption error in runtime error with delete[] array_name. I have tried many times to use nullptr, but no matter what my compiler does not recognize it as being declared in the scope of my delete function. (I have commented out the that function for now.) What is also strange about this is that I can perfectly use delete[] a1.array, which corresponds to NumArray1.array, but any of the other times I have tried to use the command, it has always resulted in syntax or runtime errors. Online resources do not adequately explain this scenario, though I am sure it exists elsewhere. all of my attempted solutions have failed, and I am completely at a loss. Can somebody please explicitly tell me how to free the dynamically allocated arrays at the end of my program? Thank you in advance.



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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217

#include <iostream>
#include <assert.h>
using namespace std;

// Define a struct type, each struct type variable has three
// member variables: array, capacity, and length. Togther these
// three variable represent a partially filled array.

struct Array
{
	int * array; // point to the dynamically allocated array
	int capacity; // the capacity of the array
	int length; // the number of elements in the array
		    // the array is filed from the 0th to the
		    // (length - 1)th element
};

/*
 Initialize the array structure with the given numbers.
 the array's capacity will be twice the length of numbers array 
 @param array: the array struct to be initialized
 @param numbers: the array of values to be stored in the array struct
 @param numbers_len: how many numbers are there in the array numbers
 precondition: "numbers" has been filled with "numbers_len" number of ints
 post condition: a.length == numbers_len
                 a.array[0]==numbers[0],..., 
		 a.capacity = 2*numbers_len
 */
void InitArray (Array & a, int numbers[], int numbers_len);

/*
Displays the content of an int array, both the array and 
the length of array will be passed as parameters to the function 
@param array: gives the array to be displayed
*/
void DisplayArray (const Array & a);

/*
 Merge the contents of two arrays into one
 @param array1: the target array
 @param array2: the source array
 @precondition: array1 and array2 have been set up 
 @postcondition: array1.length = array1.length+array2.length;
                 array1 contains its previous numbers, followed by numbers
		from array2 
 Note: if array1's capacity is not enough to hold all numbers, 
   you need to allocate a new int array, and copy old data over to the 
   new array... 
 */
void MergeArray (Array & a1, const Array & a2);



/*
 Deallocate the memory of both arrays after the end of the program.
 @param a1.array: first array to be deleted
 @param a2.array: second array to be deleted
 @precondition: a1.array and a2.array have been allocated
 @postcondition: both dynamic arrays are deallocated
 */
//void DeleteArrays (Array & a1, Array & a2, nullptr_t nullptr);



int main()
{
	int numbers1[5] = {12, 23, 34, 56, 78};
 	int numbers2[11] = {89, 7, 14, 22, 98, 101, 112, 34, 11, 5, 99};

 	Array NumArray1 = {NULL, 0, 0}; // Declare an Array object and initialize
				      // its member variables

	Array NumArray2 = {NULL, 0, 0}; // Declare another Array object and initialize
					// its member variables

	cout <<"***** NumArray1:\n";
	DisplayArray(NumArray1);
 	cout <<"***** NumArray2:\n";
 	DisplayArray(NumArray2);

	InitArray (NumArray1, numbers1, 5); // now initialize the two Array objects

	InitArray (NumArray2, numbers2, 11);

	cout <<"***** NumArray1:\n";
	DisplayArray(NumArray1);

 	cout <<"***** NumArray2:\n";
 	DisplayArray(NumArray2);
	cout << endl << endl;

 	MergeArray (NumArray1, NumArray2);






 	cout <<"***** After merge:\n";
 	cout <<"***** NumArray1:\n";
 	//DisplayArray(NumArray1);
	//DeleteArrays(NumArray1, NumArray2);
	delete[]  NumArray1.array;
	delete[] NumArray2.array;
}


void InitArray (Array & a, int numbers[], int numbers_len)
{
	a.length = numbers_len;
	a.capacity = (2 * numbers_len);	

	a.array = new int[a.capacity];

	for (int i = 0; i < a.capacity; i++)
	{
		a.array[i] = numbers[i];
 	}
}


void MergeArray (Array & a1, const Array & a2)
{
	int * IntPtr;
	int newCapacity;


	if (a1.length + a2.length > a1.capacity)
	// if a1.array is not big enough to hold all of the data
	{	
		a1.capacity = a1.length + a2.length + 10;
		

		newCapacity = a1.length + a2.length + 10;
		IntPtr = new int[a1.length + a2.length + 10];
		
		for (int i = 0; i < a1.length; i++)
			IntPtr[i] = a1.array[i];

		cout << "displaying IntPtr: " << endl;
		for (int i = 0; i < a1.length; i++)
		{
			cout << IntPtr[i] << endl;
		}


		cout << "displaying a1.array: ";
		for (int i = 0; i < a1.length; i++)
		{
			cout << a1.array[i] << endl;
		}


		cout << "deallocating a1.array: " << endl;
		delete[] a1.array;


		cout << "a1.array after deallocation: "  << endl;
		for (int i = 0; i < a1.length; i++)
		{
			cout << a1.array[i] << endl;
		}

		*a1.array = *IntPtr;		


		cout << "a1.array after pointer assignment: " << endl;
		for (int i = 0; i < a1.length; i++)
                {
                        cout << a1.array[i] << endl;
                }
		cout << endl << endl;		

		a1.capacity = newCapacity;		

		// allocate a new dynamic int array that is big enough
		// so that you can make its size be a1.length+a2.length+10 
		
		// copy numbers from a1.array to this new array
		
		// free the current a1.array
		
		// make a1.array point to the new array's capacity

		// set a1.capacity to the new array's capacity
	}

	for (int i = a1.length; i < a1.length + a2.length; i++)
	{
		IntPtr[i] = a2.array[i];			
	}

	a1.length = a1.length + a2.length;	
	// Todo: append a2.array's data into a1.array
	
	// Todo: update a1.length
}

void DisplayArray (const Array & a)
{
	for (int i = 0; i < a.length; i++)
		cout << a.array[i] << endl;
}

/*
void DeleteArrays (Array & a1, Array & a2, nullptr_t nullptr)
{
	for (int i = 0; i < a1.length; i++)
		a1.array[i] = nullptr;

	for (int i = 0; i < a2.length; i++)
		a2.array[i] = nullptr;

}
*/
At line 165:

*a1.array = *IntPtr;

These dereferences look wrong to me. You're not assigning the pointer a1.array to point to the the newly allocated memory whose address is stored in IntPtr. Instead, you're assigning the value of the first element in a1.array to have the same value as the first element of the array pointed to by IntPtr. But at this point, the memory for a1.array is unallocated, because you freed it in line 156.


Topic archived. No new replies allowed.