Deleting Dynamically Allocated Arrays

I am editing this code provided by my teacher. I have gotten it to work except that there is a runtime error because I cannot deallocate the memory for the pointer array labs[]. I have read the tutorial section on dynamic memory, and I thought that the command delete[] labs would do that. the problem is in the freeArrays function.


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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315

/*********************************************************************
 Lab4.cpp

   This program uses dynamic arrays to store login information for
four labs.  Each of the four labs is referenced by the labs[] array
which is indexed from 0-3.  A pointer in the labs[] array then
references a dynamic array that is of size for however many computers
are in that lab.

Written by: Luca Del Signore 
Last modified on: October 3rd
Known bugs: N/A
*********************************************************************/

#include <iostream>
#include <cstdlib>

using namespace std;

// Type definition
typedef int* IntPtr;

// Constants
const int NUMLABS = 4;

// Function prototypes

/*
 Creates the dynamic arrays for the labs.
 @param labs: the array of labs,
 @param labsizes: contains the size (or number of computers) of each lab
  This dictates the size of the dynamic array.
 @precondition: labsize[0] is the # of computers in lab1,
                labsize[1] is the # of computers in lab2, ...
 @postcondition: labs[0] points to lab1's array (of size given by labsize[0])
      labs[1] points to lab2's array (of size given by labsize[1])
        ...
*/
void createArrays(IntPtr labs[], int labsizes[]);


/* 
freeArrays:
Releases memory we allocated with "new"i.
@param labs: the array of labs to be deleted from memory
@precondition: lab contains data saved to it
@postcondition: lab no longer has memory allocated to it
*/
void freeArrays(IntPtr labs[]);


/*
showLabs:
Displays the status of all labs (who is logged into which computer).
@param labs: the array of labs
@param labsizes: the number of computers in each lab
@precondition: memory has been allocated for the labs
@postcondition: the data stored for each cell in the grid is displayed to the user
*/
void showLabs(IntPtr labs[], int labsizes[]);


// ======================
// login:
// Simulates a user login by asking for the login info from
// the console.
// ======================
void login(IntPtr labs[], int labsizes[]);


// ======================
// logoff:
// Searches through the arrays for the input user ID and if found
// logs that user out.
// ======================
void logoff(IntPtr labs[], int labsizes[]);


// ======================
// search:
// Searches through the arrays for the input user ID and if found
// outputs the station number.
// ======================
void search(IntPtr labs[], int labsizes[]);



// ======================
//     main function
// ======================
int main()
{
	IntPtr labs[NUMLABS]; 	// store the pointers to the dynamic array for each lab
	int labsizes[NUMLABS];	// Number of computers in each lab
	int choice = -1;
	
	cout <<"Welcome to the LabMonitorProgram!\n";

	// Prompt the user to enter labsizes 
	cout <<"Please enter the number of computer stations in each lab:\n"; 
	for (int i=0; i< NUMLABS; i++)
	{
		do
		{
			cout <<"How many computers in Lab "<< i+1<<"?";
			cin >> labsizes[i]; 
		} while (labsizes[i]<0); 
	}

	// Create ragged array structure
	createArrays(labs, labsizes);

	// Main Menu
	while (choice != 0)
	{
		cout << endl;
		showLabs(labs, labsizes);
		cout << "MAIN MENU" << endl;
		cout << "0) Quit" << endl;
		cout << "1) Simulate login" << endl;
		cout << "2) Simulate logoff" << endl;
		cout << "3) Search" << endl;
		cin >> choice;
		if (choice == 1)
		{
			login(labs, labsizes);
		}
		else if (choice == 2)
		{
			logoff(labs, labsizes);
		}
		else if (choice == 3)
		{
			search(labs, labsizes);
		}
	}

	freeArrays(labs);		// Free memory before exiting
	return 0;
}


/*
This function operates thusly:
(1) For each element of labs[], memory is allocated according to
the number of computer stations in each lab.
(2) Each element of labsizes[] corresponding to each element of labs[]
is initialized to -1.
 */
void createArrays(IntPtr labs[], int labsizes[])
{
	for (int i = 0; i < NUMLABS; i++)
	{
		labs[i] = new int[labsizes[i]];
		for (int j = 0; j < labsizes[j]; j++)
		{
			labs[i][j] = -1;		
		}
	}
}

/*
This function deallocates the memory of labs[].
 */
void freeArrays(IntPtr labs[])
{
	for (int i = 0; i < NUMLABS; i++)
	{
		delete[] labs;
	}
}


/* COMMENTS NEEDED HERE:
	Describe how your function does its job, 
	as required by the comments given for the declaration of the function.  
 */
void showLabs(IntPtr labs[], int labsizes[])
{

	int i;
	int j;

	cout << "LAB STATUS" << endl;
	cout << "Lab #     Computer Stations" << endl;
	for (i=0; i < NUMLABS; i++)
	{
		cout << i+1 << "         ";
		for (j=0; j < labsizes[i]; j++)
		{
			cout << (j+1) << ": ";
			if (labs[i][j] == -1)
			{
				cout << "empty ";
			}
			else
			{
				cout << labs[i][j] << " ";
			}
		}
		cout << endl;
	}
	cout << endl;
	return;
}

/* COMMENTS NEEDED HERE:
	Describe how your function does its job, 
	as required by the comments given for the declaration of the function.  
 */
void login(IntPtr labs[], int labsizes[])
{
	int id, lab, num = -1;

	// read user id 
	do
	{
  		cout << "Enter the 5 digit ID number of the user logging in:" << endl;
		cin >> id;
	} while ((id < 0) || (id > 99999));

	// read the lab number 
	do 
	{
		cout << "Enter the lab number the user is logging in from (1-" <<
			NUMLABS << "):" << endl;
		cin >> lab;
	} while ((lab < 0) || (lab > NUMLABS));

	//read computer number 
	do
	{
		cout << "Enter computer station number the user is logging in to " <<
			"(1-" << labsizes[lab-1] << "):" << endl;
		cin >> num;
	} while ((num < 0) || (num > labsizes[lab-1]));

	// Check to see if this station is free
	if (labs[lab-1][num-1]!=-1)
	{
		cout << "ERROR, user " << labs[lab-1][num-1] <<
			" is already logged into that station." << endl;
		return;
	}
	// Assign this station to the user
	labs[lab-1][num-1] = id;
	return;
}


/* COMMENTS NEEDED HERE:
	Describe how your function does its job, 
	as required by the comments given for the declaration of the function.  
 */
void logoff(IntPtr labs[], int labsizes[])
{
	int id, i,j;

	// Get input from the keyboard, validating data ranges
	do
	{
  		cout << "Enter the 5 digit ID number of the user to find:" << endl;
		cin >> id;
	} while ((id < 0) || (id > 99999));

	
	for (i=0; i<NUMLABS; i++) // check for each lab 
	{
		for (j=0; j<labsizes[i]; j++) //if the user is using any computer in the lab
		{
			if (labs[i][j]==id) //if so, log the user off... 
			{
				// Log the user off by setting the entry to -1
				labs[i][j] = -1;
				cout << "User " << id << " is logged off." << endl;
				return;
			}
		}
	}
	cout << "That user is not logged in." << endl;
	return;
}

/* COMMENTS NEEDED HERE:
	Describe how your function does its job, 
	as required by the comments given for the declaration of the function.  
 */
void search(IntPtr labs[], int labsizes[])
{
	int id;

	do
        {
                cout << "Enter the 5 digit ID number of the user to find:" << endl;
                cin >> id;
        } while ((id < 0) || (id > 99999));


	for (int i = 0; i < NUMLABS; i++)
	{
		for (int j = 0; j < labsizes[i]; j++)
		{
			if (labs[i][j] == id)
			{
				cout << "Lab: " << i + 1 << "  ";
				cout << "Station: " << j + 1 << endl;	
			}	
		}
	}

}


Last edited on
 
IntPtr labs[NUMLABS];

labs is not dynamically allocated so you don't have to do anything special to deallocate it. The memory will be deallocated automatically when the it goes out of scope.


If you had created labs like this:

 
IntPtr* labs = new IntPtr[NUMLABS];

then it would be dynamically allocated and you would have to use delete[] to free the memory.
Last edited on
a) labs werent created with new[], so attempt to delete them is an error.
b) even if they were, you are trying to delete them several times in a loop. That is a problem too.

You do not need to delete labs, instead you need to delete memory pointers lab contains point to:
1
2
3
delete[] labs;
delete[] labs[i];
labs[i] = nullptr;
Topic archived. No new replies allowed.