C++ Question
If I have a pointer to a class, then in that class there is a pointer to another class, when I delete the first class, is the second pointer deleted too?
example:
class class1 {
class2 *other;
void DeleteSelf();
}
class 2 {
char *whatever;
}
void class1::DeleteSelf()
{
delete other;
}
Would this free the memory whatever is using, or do I have to delete it manually.
thanks,
u mean freeing char *whatever? you have to handle that in the destructor for class2. c++ doesn''t have garbage collection
-----------------
- Pouya / FOO!!!
One of the initiators of the "uh, no" crisis and entitled to be the king of crap posters.
-----------------
- Pouya / FOO!!!
One of the initiators of the "uh, no" crisis and entitled to be the king of crap posters.
i guess using the pointer to that object after u have already freed it
-----------------
- Pouya / FOO!!!
One of the initiators of the "uh, no" crisis and entitled to be the king of crap posters.
-----------------
- Pouya / FOO!!!
One of the initiators of the "uh, no" crisis and entitled to be the king of crap posters.
it depends on what it is.
if it''s just an string or a dynamically generated array or something like that (even a directdrawsurface object or an allocated sound buffer) you just lose some o the memory because your systems thinks that the memory is still in use (no garbage collection)
but if it''s something more serious. let''s say the directdraw object that has gone fullscreen and then you exit the program without telling the directdraw to destroy or to change back to normal and neither you free the memory for it..... ummm i guess you''ll need to restart your puter.
-----------------
- Pouya / FOO!!!
One of the initiators of the "uh, no" crisis and entitled to be the king of crap posters.
if it''s just an string or a dynamically generated array or something like that (even a directdrawsurface object or an allocated sound buffer) you just lose some o the memory because your systems thinks that the memory is still in use (no garbage collection)
but if it''s something more serious. let''s say the directdraw object that has gone fullscreen and then you exit the program without telling the directdraw to destroy or to change back to normal and neither you free the memory for it..... ummm i guess you''ll need to restart your puter.
-----------------
- Pouya / FOO!!!
One of the initiators of the "uh, no" crisis and entitled to be the king of crap posters.
Sorry for all the questions but I got another one
When You delete a Pointer to an array of a certain class. Each destructor in each class in the array is called. Then it free''s the static variables? Correct?
When You delete a Pointer to an array of a certain class. Each destructor in each class in the array is called. Then it free''s the static variables? Correct?
the static variables never get freed unless the application is finished (i dont think they would eve go outta scope, unless you can somehow undefine the class definition? )
btw: don''t say a pointer to a class. the correct thing is to say the instance of that class. or simply an object
-----------------
- Pouya / FOO!!!
One of the initiators of the "uh, no" crisis and entitled to be the king of crap posters.
btw: don''t say a pointer to a class. the correct thing is to say the instance of that class. or simply an object
-----------------
- Pouya / FOO!!!
One of the initiators of the "uh, no" crisis and entitled to be the king of crap posters.
How Would I Free this Memory?
Now How would I free all this Memory correctly. Without getting an Assertion Error
Edited by - Esap1 on July 9, 2000 7:57:30 PM
class Button_class{ public: int stuff; int x,y,z;};class Menu_class{ public: Button_class *Button; int currSelection;};class MenuManager_class { Menu_class *Menu; int other stuff; void LoadStuff;};void MenuManager_class::LoadStuff(){ Menu = new Menu_class[1]; Menu_class[0].Button = new Button_class[1]; Menu_class[0].Button[0].stuff = 1;}
Now How would I free all this Memory correctly. Without getting an Assertion Error
Edited by - Esap1 on July 9, 2000 7:57:30 PM
class Button_class{public: int stuff; int x,y,z;};class Menu_class{public: Button_class *Button; int currSelection; MenuClass(); ~MenuClass();};class MenuManager_class{ Menu_class *Menu; int other_stuff; MenuManager(); ~MenuManager();};Menu_class::Menu_class(){ Button = new Button_class(); Button->stuff = 1;}Menu_class::~Menu_class(){ delete Button;}MenuManager_class::MenuManager_class(){ Menu = new Menu_class();}MenuManager_class::~MenuManager_class(){ delete Menu;}
if you want it to ba an array of buttons and menus you have to define them as Button_class **Button and Menu_class **Menu, then allocate memory for *Button and *Menu as arrays and then start allocating Button[x] and Menu[x]
dont forget to delete them at the end
-----------------
- Pouya / FOO!!!
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement