Is it a bad idea not to use destructors? Will it lead to memory leaks and stuff in the long run? Also I am assuming it is more crucial to have better memory management in embedded systems right? What are some very good memory saving techniques?
All I use are constructors, now that I think about it, doesn't seem "correct" since destructors exist and I am not using them...
3 replies to this topic
#1
Posted 02 May 2010 - 08:31 PM
|
|
|
#2
Posted 03 May 2010 - 12:16 AM
Change the language to some using automatic memory management and you are done. Destructors are a C++ only thing.
And in embedded systems it is better to use C, not C++, because C++ incurs some code size overhead.
And in embedded systems it is better to use C, not C++, because C++ incurs some code size overhead.
#3
Posted 03 May 2010 - 05:27 PM
In the book "Effective C++" by Scott Meyer, he recommends creating destructors whenever you have dynamically allocated memory. Thus, if you ever use "new" in your constructor, you should call delete in your destructor.
Another nifty trick is to use smart pointers. Include memory.h. Then use a smart pointer instead of an ordinary pointer. For instance, to declare a pointer to "MyClass", do this
Smart pointers are cool because they can be treated as regular pointers but they delete themselves. Cool!
Another nifty trick is to use smart pointers. Include memory.h. Then use a smart pointer instead of an ordinary pointer. For instance, to declare a pointer to "MyClass", do this
auto_ptr<MyClass> myPtr(new MyClass());
Smart pointers are cool because they can be treated as regular pointers but they delete themselves. Cool!
I don't document code. If it was hard to write, it should be hard to read ;)
#4
Posted 03 May 2010 - 05:37 PM
It depends on what's in your class. If you have resources that need to be gracefully released (pointers, open files, etc) then you should have a destructor. If you have resources that can simply be released, then a destructor isn't needed.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









