Do I need to use delete [] in this program?

Just as the title says. Does my parameter that is a pointer to the Car struct array that is passed as an argument into the function have to be deleted to free memory? I'm pretty sure I don't have to do anything because I didn't dynamically allocate any memory. But I want to check here just to be safe. Specifically I am referring to the displayCar function.

Thanks,
MisterTams

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
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
// enumerated type
enum Purpose { BUSINESS, PERSONAL };

struct Car
{
	string carMake;
	string carModel;
	int yearModel;
	double cost;
	Purpose prps;

};
//function prototype
void displayCar(Car *, int);
void UserInput(Car[], int &, const int);

int main()
{
	const int SIZE = 10;
	int actualSize = 3;

	Car carArray[SIZE] = {
		{ "Ford", "Taurus", 1997, 21000, BUSINESS },
		{ "Honda", "Accord", 1992, 11000, BUSINESS },
		{ "Lamborghini", "Aventador", 2011, 390000, PERSONAL }
	};

		UserInput(carArray, actualSize, SIZE);
		cout << endl;
	for (int index = 0; index < actualSize; index++)
	{
		cout << "Car #" << index + 1 << endl;
		displayCar(carArray, index);
	}
	return 0;
}

void displayCar(Car *vehicle, int element)
{
	cout << "Make: " << setw(3) << " " << vehicle[element].carMake << endl;
	cout << "Model: " << setw(2) << " " << vehicle[element].carModel << endl;
	cout << "Year: " << setw(3) << " " << vehicle[element].yearModel << endl;
	cout << fixed << setprecision(2);
	cout << "Cost: " << setw(3) << " " << vehicle[element].cost << endl;
	cout << "Purpose: ";

	switch (vehicle[element].prps)
	{
	case BUSINESS:
		cout << "Business" << endl;
		break;
	case PERSONAL:
		cout << "Personal" << endl;
		break;
	}
	cout << endl;
}

void UserInput(Car carArray[], int &actualArrSize, const int Size)
{
	int purpose;

			cout << "Car #" << actualArrSize + 1 << endl;
			cout << "Please enter a Car Make: ";
			getline(cin, carArray[actualArrSize].carMake);
			cout << "Please enter Car Model:  ";
			getline(cin, carArray[actualArrSize].carModel);
			cout << "Please enter Car Year:   ";
			cin >> carArray[actualArrSize].yearModel;
			cin.ignore(10000000, '\n');
			cout << "Please enter Car Cost:   ";
			cin >> carArray[actualArrSize].cost;
			cin.ignore(10000000, '\n');
			cout << "Please enter Car Purpose (1=BUSINESS, 2=PERSONAL) : ";
			cin >> purpose;
			purpose -= 1;
			cin.ignore(1000000, '\n');
			carArray[actualArrSize].prps = static_cast <Purpose>(purpose); //static cast to input the purpose of the vehicle
			actualArrSize++;
}
closed account (48T7M4Gy)
delete and delete[] apply where corresponding new's are used.
kemort is correct, but just to answer your question
I'm pretty sure I don't have to do anything because I didn't dynamically allocate any memory

Correct, everything you've done in your program is on the stack, no dynamic deletion needed.

However, if this is actual code and not just an exercise, I would highly suggest using references instead of pointers, because it seems like under proper program execution, displayCar() will never take in a null pointer.
Topic archived. No new replies allowed.