Array of pointers to objects

I need un urgent help with my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int OKCount=0;
int WaitingCount=0;
int ReservationCount=0;
Flight::Flight(int capacity, int waitingMax)
{
	seats=capacity;
	waitingListMax=waitingMax;
	reservations= new Reservation*[waitingListMax+seats]();
}
bool Flight::reserveSeat(Passenger psngr, bool smoker)
{
	string date;
	if(OKCount < seats)
	{
		OKCount++;
		cout << "Enter the reservation date: ";
		cin >> date;
		OKReservation ores(psngr,smoker,date,true);
		reservations[ReservationCount]=&ores;
		ReservationCount++;
		return true;
	}
}

reservations is a data member in the class flight as:
 
Reservation **reservations;


OKReservation is a derived class and its abstract base class is Reservation.

My problem is that the reservations array loses its value in other function
can anyone help me please!!!
Last edited on
1
2
		OKReservation ores(psngr,smoker,date,true);
		reservations[ReservationCount]=&ores;


ores is a temporary object. It will go out of scope and die as soon as the reserveSeat function exits.

You are taking a pointer to ores and putting it in your reservations array. As soon as ores dies... this becomes a bad pointer as the object it is pointing to no longer exists.


You probably will need to dynamically allocate the object so it has a sustained lifetime:

1
2
// OKReservation ores(psngr,smoker,date,true);  // <- get rid of this
reservations[ReservationCount] = new OKReservation(psngr,smoker,date,true); //<- do this instead 



Of course... this also means you'll have to delete all the objects since you're not using smart pointers. And of course... since you are dynamically allocating the reservations array as well, you should be delete[]-ing that as well. Failure to do these deletes will cause memory leaks in your program.
Dear Disch,
Thank you for your quick help.
I need to know one more thing where am I suppose to delete the objects ?
Access violations are typically caused by you dereferencing a bad pointer.

If the address it's giving you is very close to zero (like 0x00000005 or something) then you're probably dereferencing a null pointer somewhere.

Or... if the address is close to a fixed pattern... like 0xCDCDCDD4 or something... then you're probably dereferencing an uninitialized pointer somewhere.


In either case... when you get the error message... if you're running from a debugger (which you should be!) the debugger should snap and tell you exactly what line of code is causing the access violation. Post that code here. It doesn't look to me like the problem would be anywhere in the code you already posted.
I need to know one more thing where am I suppose to delete the objects ?

That's really a design decision you have to make. Whenever you dynamically allocate memory, you should think about which bit of your code is going to take ownership of that memory and be responsible for deleting it.

Since reservations is a data member of your Flight class, and the code within Flight is allocating the memory for it, it looks as though Flight is taking ownership of that memory. In which case, the destructor for Flight should probably be the place to delete that memory.

But you know the rest of your code, and we don't, so it may be that there's a more appropriate place.
Last edited on
Dear Disch,
Thank you for your quick help. Every thing now is resolved.
I need to know one more thing where am I suppose to delete the objects ?
I need to know one more thing where am I suppose to delete the objects ?

I've already answered that for you. What about my answer did you find unacceptable?
Dear MikeyBoy,
I'm so sorry I didn't see your previous reply, surly your answer is so acceptable and very appreciated.
Thanks' and sorry again.
No problem - you're welcome :)
Topic archived. No new replies allowed.