I'm currently learning about dynamic memory allocation and I am having a hard time understanding the benefit of this when it comes to class member variables.
Given the following scenario below of a normally declared class member variable VS a dynamically allocated class member variable, what are the benefits of dynamically allocating a class member variable and what problem(s) would this solve?
1 2 3 4 5 6 7 8 9
class Test
{
public:
Test();
~Test();
private:
std::list<int> list;
};
1 2 3 4 5 6 7 8 9 10
Test::Test()
: list()
{
}
Test::~Test()
{
}
VS
1 2 3 4 5 6 7 8 9 10 11
#include <list>
class Test
{
public:
Test();
~Test();
private:
std::list<int> *list;
};
class Test
{
public:
Test(int);
private:
std::list<int> list1[];// doesn't work we can't create an array without defining its size
std::list<int> list2[5]; //so when we do this it's working but now we have
// to live with the fixed size of 5
std::list<int>* list3; //this is just a fancy little pointer
};
Test::Test(int array_size)
{
// let's check what we could do here
// list1 doesn't exist because it wouldn't compile
// we can't change the size of list2 , once we said it's 5 it stays 5
// but with dynamic memory allocation we could now do the following:
list3 = new std::list<int>[array_size]; //this allows us to choose our array size
// differently for each object
// on top of that we can even increase the size of the Array later-on.
}
If you don't want to create an Array (that's what you're doing just creating 1 list object in each example) then there isn't really a huge difference for you on what you can do with them.
BUT
since: In your first example your variable is pushed on the stack and dynamic allocated memory is in the heap.
If you only need one List element or a fixed Array size use the first approach (since it's faster to access Stack-Variables than anything located in the heap)
Hope this half-explanation is helpful - just keep asking whatever is not clear
So from what you described, the only reason to have a class member variable dynamically allocated is when you are using arrays and want to define the size of that array for each individual object differently (via constructor) or declared fixed size during its definition (std::list<int> list2[5])
Am I understanding that correctly?
Is there any other scenario outside of arrays where I would definitely need to dynamically allocate the member variable?
If you want to go deeper with your answer, please do :)
Good morning first of all ;)
Have a nice sunday everybody !
or declared fixed size during its definition (std::list<int> list2[5])
This is not dynamic allocated this is just an Array with a fixed size you can't change anymore
Is there any other scenario outside of arrays where I would definitely need to dynamically allocate the member variable?
We seem to strictly be talking about member variables and nothing else so:
If I understand you right. You just want to know where the difference is when you create one List object.
That means there is no difference in usage if you just create one Object.
All there is are those different places in memory where they are stored and because of that you wanna use the first approach everytime. (you also always have to dereference to access the list when u use ur List-Pointer)
The advantages for an Array should be clear now.
(one note to that, if you don't know the size you need: check out container classes like vector or well List you probably know them already ^.^)
So, to finalize my understanding of the following two scenarios, #1 is the obvious choice because I gain nothing in case #2 from dynamically allocating the std::list, except for allocating it on the heap, having to deal with deallocation and writing custom copy constructor and assignment operator?