ERROR MESSAGE: Access violation reading location 0xccccccc8.

Hi i have been fighting with this for hours now and i can't seem to get it. i am getting the message Access violation reading location 0xccccccc8. when i try to print my dynamic array during the overloaded << function in my Flight class. where the error happens is surrounded by *s

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
#include<iostream>
#include<vector>
#include<string>

using namespace std;



class Airliner
{
public:
	Airliner();
	int getCrewSize();
	friend ostream& operator <<(ostream& outs, Airliner& output); 
private:
	string ID;
	string type;
	int crewSize;
};

class Person
{
public:
	Person();
	friend ostream& operator <<(ostream& outs, Person& output); 
private:
	string name;
	string phone;
};

class Flight
{
public:
	Flight();
	~Flight();
	friend ostream& operator <<(ostream& outs, Flight& output); 
private:
	Person * crew;
	Person pilot;
	Person copilot;
	vector<Person> souls;
	Airliner planeType;
};
//MAIN//
int main()
{
	Flight myFlight;
	cout << myFlight;

	system("PAUSE");
	return 0;
}

//PERSON CLASS//
Person::Person()
{
	cout << "Name: ";
	cin >> name;
	cout << "Cell Phone Number: ";
	cin >> phone;
}

ostream& operator <<(ostream& outs, Person& output)
{
	cout << "Name: " << output.name << endl;
	cout << "Phone: " << output.phone << endl;
	return outs;
}

//AIRLINER CLASS//
Airliner::Airliner()
{
	cout << "Flight ID: ";
	cin >> ID;
	cout << "Flight Type: ";
	cin >> type;
	cout << "Crew Size: ";
	cin >> crewSize;
}

int Airliner::getCrewSize()
{
	return crewSize;
}
ostream& operator <<(ostream& outs, Airliner& output)
{
	cout << "Flight ID: " << output.ID << endl;
	cout << "Flight Type: " << output.type << endl;
	cout << "Flight Crew Size: " << output.getCrewSize() << endl;
	return outs;
}

//FLIGHT CLASS//
Flight::Flight()
{
	cout << "Crew: " << endl;
	Person * crew = new Person[planeType.getCrewSize()];
	cout << crew[0];
	char c;
	int l = 1;
	do
	{
		cout << "Passanger #" << l << ": " << endl;
		Person i;
		souls.push_back(i);
		cout << "Are there more people?(y/n)" << endl;
		cin >> c;
		l++;

	}while(c != 'n' && c!= 'N');
}

Flight::~Flight()
{
	if(crew != NULL)
	{
		delete[] crew;
	}
}

ostream& operator <<(ostream& outs, Flight& output)
{
	int size = output.planeType.getCrewSize();
	outs << "SIZE: " << size << endl;
	outs << "Pilot: " << output.pilot << endl;
	outs << "Co-Pilot: " << output.copilot << endl;
	outs << "Passangers: " << endl;
	for(unsigned int i = 0; i < output.souls.size(); i++)
	{
		outs << "\t" << output.souls[i] << endl;
	}
	outs << "Crew:" << endl;
	//*******************************************
	for(int n = 0; n < size; n++)
	{
		outs << "\t" << output.crew[n] << endl;
	}
        //*******************************************
	return outs;
}


any help is greatly appreciated

Thank You.
Last edited on
Person * crew = new Person[planeType.getCrewSize()];
This creates a new pointer called "crew" in the scope of Flight::Flight(), which is gone at the end of that function. output.crew remains uninitialized, and since output was created on stack, it is 0xccccccc, the uninitialized stack memory in Windows debug builds.

Best way to go about it is to use vectors and initialize them before the opening brace of the constructor where possible.
Last edited on
This is for a class i would also rather use vectors but my teacher is requiring us to use this is there anyway i can do this using a dynamic array?
I hope they will explain why this is a bad thing to do (if they don't, read up on rule of three and exception safety)
The minimal fix is to not make a new crew in the constructor, but instead overwrite the existing one:
crew = new Person[planeType.getCrewSize()];
(it would be better to initialize it in the member initialization list, but to do that you'd have to restructure the class to move planeType before crew)
Wow i am so dumb. haha by changing the Person * crew = new [planeType.getCrewSize()] in the constructor to crew = new [planeType.getCrewSize()] it worked because i was overwriting the one in the class definition its self. Thank you so much!

working code is as follows:

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

#include<iostream>
#include<vector>
#include<string>

using namespace std;



class Airliner
{
public:
	Airliner();
	int getCrewSize();
	friend ostream& operator <<(ostream& outs, Airliner& output); 
private:
	string ID;
	string type;
	int crewSize;
};

class Person
{
public:
	Person();
	friend ostream& operator <<(ostream& outs, Person& output); 
private:
	string name;
	string phone;
};

class Flight
{
public:
	Flight();
	~Flight();
	friend ostream& operator <<(ostream& outs, Flight& output); 
private:
	Airliner planeType;
	Person * crew;
	Person pilot;
	Person copilot;
	vector<Person> souls;
	//Airliner planeType;
};
//MAIN//
int main()
{
	Flight myFlight;
	cout << myFlight;

	system("PAUSE");
	return 0;
}

//PERSON CLASS//
Person::Person()
{
	cout << "Name: ";
	cin >> name;
	cout << "Cell Phone Number: ";
	cin >> phone;
}

ostream& operator <<(ostream& outs, Person& output)
{
	cout << "Name: " << output.name << endl;
	cout << "Phone: " << output.phone << endl;
	return outs;
}

//AIRLINER CLASS//
Airliner::Airliner()
{
	cout << "Flight ID: ";
	cin >> ID;
	cout << "Flight Type: ";
	cin >> type;
	cout << "Crew Size: ";
	cin >> crewSize;
	cout << "Pilot and Copilot information please:" << endl;
}

int Airliner::getCrewSize()
{
	return crewSize;
}
ostream& operator <<(ostream& outs, Airliner& output)
{
	cout << "Flight ID: " << output.ID << endl;
	cout << "Flight Type: " << output.type << endl;
	cout << "Flight Crew Size: " << output.getCrewSize() << endl;
	return outs;
}

//FLIGHT CLASS//
Flight::Flight()
{
	cout << "Crew: " << endl;
	crew = new Person[planeType.getCrewSize()]; // here was my problem 
	char c;
	int l = 1;
	do
	{
		cout << "Passanger #" << l << ": " << endl;
		Person i;
		souls.push_back(i);
		cout << "Are there more people?(y/n)" << endl;
		cin >> c;
		l++;

	}while(c != 'n' && c!= 'N');
}

Flight::~Flight()
{
	if(crew != NULL)
	{
		delete[] crew;
	}
}

ostream& operator <<(ostream& outs, Flight& output)
{
	int size = output.planeType.getCrewSize();
	outs << "SIZE: " << size << endl;
	outs << "Pilot: " << output.pilot << endl;
	outs << "Co-Pilot: " << output.copilot << endl;
	outs << "Passangers: " << endl;
	for(unsigned int i = 0; i < output.souls.size(); i++)
	{
		outs << "\t" << output.souls[i] << endl;
	}
	outs << "Crew:" << endl;
	
	for(int n = 0; n < size; n++)
	{
		outs << "\t" << output.crew[n] << endl;
	}
	return outs;
}
Topic archived. No new replies allowed.