delete memory
-
Hi All, I am new to c++ and facing one prob. I have one base class Vehicle and two sub class Car and Motor. I have an array of pointer to sub class but i m not able to delete the memory for that. CVehicle CCar : CVehicle CMotor : CVehicle CVehicle** myVehicle; myVehicle = new CVehicle*[3]; myVehicle[0] = new CCar; myVehicle[1] = new CMotor; myVehicle[2] = new CCar; now when I tried to reallocate the memroy, it is giving an error. delete myVehicle ; Any idea how to delete the memory?? Any help Monark - I am learning
-
Hi All, I am new to c++ and facing one prob. I have one base class Vehicle and two sub class Car and Motor. I have an array of pointer to sub class but i m not able to delete the memory for that. CVehicle CCar : CVehicle CMotor : CVehicle CVehicle** myVehicle; myVehicle = new CVehicle*[3]; myVehicle[0] = new CCar; myVehicle[1] = new CMotor; myVehicle[2] = new CCar; now when I tried to reallocate the memroy, it is giving an error. delete myVehicle ; Any idea how to delete the memory?? Any help Monark - I am learning
-
Hi All, I am new to c++ and facing one prob. I have one base class Vehicle and two sub class Car and Motor. I have an array of pointer to sub class but i m not able to delete the memory for that. CVehicle CCar : CVehicle CMotor : CVehicle CVehicle** myVehicle; myVehicle = new CVehicle*[3]; myVehicle[0] = new CCar; myVehicle[1] = new CMotor; myVehicle[2] = new CCar; now when I tried to reallocate the memroy, it is giving an error. delete myVehicle ; Any idea how to delete the memory?? Any help Monark - I am learning
when you use new, you must use delete. when you use new[], you must use delete[]. BTW, i see a problem in your design. a Car is actually a Vehicle, so, it's ok to derive it from CVehicle. but a Motor is not a Vehicle. a Vehicle
HAS A
Motor... it seems that you confused with theIS A
relationship. also, are you sure to wanting to create an array of pointers rather than an array of objects ?
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
Hi All, I am new to c++ and facing one prob. I have one base class Vehicle and two sub class Car and Motor. I have an array of pointer to sub class but i m not able to delete the memory for that. CVehicle CCar : CVehicle CMotor : CVehicle CVehicle** myVehicle; myVehicle = new CVehicle*[3]; myVehicle[0] = new CCar; myVehicle[1] = new CMotor; myVehicle[2] = new CCar; now when I tried to reallocate the memroy, it is giving an error. delete myVehicle ; Any idea how to delete the memory?? Any help Monark - I am learning
-
Hi All, I am new to c++ and facing one prob. I have one base class Vehicle and two sub class Car and Motor. I have an array of pointer to sub class but i m not able to delete the memory for that. CVehicle CCar : CVehicle CMotor : CVehicle CVehicle** myVehicle; myVehicle = new CVehicle*[3]; myVehicle[0] = new CCar; myVehicle[1] = new CMotor; myVehicle[2] = new CCar; now when I tried to reallocate the memroy, it is giving an error. delete myVehicle ; Any idea how to delete the memory?? Any help Monark - I am learning
-
use the delete operator carefully. With delete you delete pointers: first delete myVehicle[0]; delete myVehicle[1]; delete myVehicle[2]; delete *myVehicle;//pointer to pointer Hammer in your head: to every new belongs a delete :-O
Greetings from Germany
KarstenK wrote:
delete *myVehicle;//pointer to pointer
Your syntax is a bit askew. Use:
delete [] myPointer;
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
KarstenK wrote:
delete *myVehicle;//pointer to pointer
Your syntax is a bit askew. Use:
delete [] myPointer;
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
I chose this way use the same syntax as the declararion: to make it easier readable
Greetings from Germany
KarstenK wrote:
I chose this way use the same syntax as the declararion: to make it easier readable
But it is wrong. As the address being deleted is not the same as the one allocated, you will receive an access violation.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
KarstenK wrote:
I chose this way use the same syntax as the declararion: to make it easier readable
But it is wrong. As the address being deleted is not the same as the one allocated, you will receive an access violation.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
it was (not my code as followed) allocated: CVehicle** myVehicle; myVehicle = new CVehicle*[3]; so I would delete *myVehicle; or would I get (a deservred) exception :~
Greetings from Germany
You're missing it entirely.
myVehicle
and*myVehicle
point to two different addresses, with the latter already having been freed (viadelete myVehicle[0]
). Rather than guess at what you are trying to do, why not just try it?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne