Raw Pointer Primer
There are two basic ways to create a variable in C++: stack variables and heap allocations. Stack variables are defined inside a certain scope and as long as you're in that scope, the variable will exist. Heap allocations (dynamically allocated memory), variables typically declared with new or malloc, aren't not defined within a scope and will exist until the memory is freed.
Here's an example:
void foo() { // Object A of class CSomeClass has been declared inside the scope of foo CSomeClass A; // do some stuff .... // you can even call other functions and use A as a parameter Func1(A); // This could be pass by value or pass by reference depending on Func1's declaration Func2(&A); // Passes a pointer to A // at the end of this function, the scope will end and A will automatically be destroyed }Now with this function, every time another function calls foo, A will be created and then destroyed when the function exits. Not bad right. What about this?
void foo() { // Object A of class CSomeClass has been declared inside the scope of foo CSomeClass *A = new CSomeClass; // do some stuff .... // you can even call other functions and use A as a parameter Func1(*A); // This could be pass by value or pass by reference depending on Func1's declaration Func2(A); // Passes pointer A of CSomeClass (Edited) // MEMORY LEAK // at the end of this function, the scope will end, but A was created on the heap // delete should be called here }So with dynamic memory allocations, you must free the memory. So why you might ask do we even need dynamic memory allocations? Well one, to declare variables on the stack, you need to know exactly what you'll need at compile time. If you want to be able to create arrays of various sizes depending on user input, or if you're making a game and want to load variable amount of resources, you'll need to use dynamic memory allocations.
Take this example:
int num_students; // First get the number of students in the class std::cout << "How many students are in the class?" std::cin >> num_students; // Create a dynamic students array CStudent *student_array = new CStudent[num_students]; // Do some stuff with the data .... // call the array version of delete to free memory delete [] student_array;
In the previous situation, you must use dynamic memory because the size of the array is determined by the user. How can smart pointers help?
Smart Pointers
(chief reference: Smart Pointers (Modern C++) on MSDN)
Smart pointers allow you to create dynamic memory allocations but defined them inside a scope or an owner. This way, when the owner goes out of focus, the data will be automatically deleted. Smart pointers have been implemented using templates and to use them, you must include the header
std::unique_ptrapples(new Base(L"apples")); // Where Base is the class type
You create a unique_ptr like a typical template and then pass the raw pointer to initialize the variable. After that, you can use the unique_ptr, just as you would any other pointer.
class Base { public: Base(const std::wstring &string) :m_string(string) { } virtual void Display() const { std::wcout << L"Base:" << m_string << std::endl; } private: std::wstring m_string; }; int main() { // declare some unique_ptrs. This pointers can have only one owner and cannot be copied. Only moved. std::unique_ptr<Base> apples(new Base(L"apples")); apples->Display(); }
class Base { public: Base(const std::wstring &string) :m_string(string) { } virtual void Display() const { std::wcout << L"Base:" << m_string << std::endl; } private: std::wstring m_string; }; class Derived : public Base { public: Derived(const std::wstring &string) :Base(string) { } virtual void Display() const { std::wcout << L"Derived:::"; __super::Display(); // __super is MS specific. Others should use Base::Display(); } }; int main() { // declare some unique_ptrs. This pointers can have only one owner and cannot be copied. Only moved. std::unique_ptr<Base> apples(new Base(L"apples")); std::unique_ptr<Base> oranges(new Derived(L"oranges")); apples->Display(); oranges->Display(); }
std::vector <std::unique_ptr<Base>> test_vector; test_vector.push_back(std::unique_ptr<Base>(new Base(L"apples"))); test_vector.push_back(std::unique_ptr<Base>(new Derived(L"oranges")));
Conclusion
I hope this information was helpful. I am in no way an expert so if you anyone sees something glaring that I missed, feel free to leave a "kind" comment. If you would like to know more about smart pointers, I suggest checking out the like on msdn.
This article was originally written as a blog post on Gamedev.net
http://www.gamedev.net/blog/1670/entry-2256432-fun-with-modern-c-and-smart-pointers/
http://www.gamedev.net/blog/1670/entry-2256432-fun-with-modern-c-and-smart-pointers/
No comments:
Post a Comment