Small Clarification about Dynamic Memory and STL Containers...

Started by
2 comments, last by popsoftheyear 12 years, 6 months ago
Just something I've been wondering about...

If you're using something like an std::list, you can use push_back() to add objects to that list. So for example you could add a pointer to an Object like:
std::list< Object* > objList;
objlist.push_back( new Object() );
So you're allocating a new Object and putting a pointer to it in the list.

However, you can also do:
std::list< Object > objList;
objList.push_back( Object() );

Here, you're not using pointers yourself, but considering that memory is still being allocated for new objects at run-time... does that mean that in the background, pointers are still being used in basically just the same way as the first example, just that it's hidden from the user and handled by the implementations of the STL?

I assume that pointers are used similarly in both cases but I've just been wondering for too long and thought I'd have to ask and get a proper answer. Thanks.
Advertisement
Internally, a list will allocate memory for each element in its list. It will generally store a pointer to this block of memory in order to access it. Now, the size of the block allocated for an element depends on the type of data stored by the list element. It will need room for a pointer to the next element in the list, as well as the data type being stored itself.

So, if you have a linked list of Object*, each list element is actually an allocated block of memory for storing the Object*, as well as a pointer to the next element (and possibly a pointer to the previous element, depending on the type of list).

Thus, internally, I suppose you could think of it as a pointer to data which has another pointer (the Object*). But conceptually that's a bit vague.

If you instead store a linked list of Object, each list element will be large enough to hold an Object plus a pointer to the next element (and maybe previous). If you're really curious, maybe try to make your own list implementation! It will teach you much about how it works inside. Then afterwards, of course, probably best to go back to using stl::list simply because it is less likely to have obscure bugs.

I hope that was a reasonably clear explanation - maybe someone else will answer in a more concise manner.

I hope that was a reasonably clear explanation.


I think so...

Am I right in thinking that basically, a list of Object is a collection of pointers, one for each element, which point to a block of memory that holds the Object itself and then the pointer to the next element?

So that an std::list< Object > IS a list of pointers but just slightly more direct than std::list< Object* >?

[quote name='achild' timestamp='1318349285' post='4871486']
I hope that was a reasonably clear explanation.


I think so...

Am I right in thinking that basically, a list of Object is a collection of pointers, one for each element, which point to a block of memory that holds the Object itself and then the pointer to the next element?

So that an std::list< Object > IS a list of pointers but just slightly more direct than std::list< Object* >?
[/quote]
Yeah, that sounds close, though this whole way of thinking of it as pointers and pointers to pointers is kind of flawed. Honestly, if you want to understand what is going on in there, you should just implement your own list for learning purposes (just try not to get too attached to it as it will inevitably have issues that creep up one day, especially without much experience here).

Remember, a list is just a container. It holds a number of elements. Whether these elements are of type Object or of type Object * is irrelevant - the list certainly doesn't know the difference. Either way it allocates memory for these things in order to store them. It also has certain attributes which make it a list in the first place. One of those attributes is the fact that it has ways of linking the list together, in this case using pointers.

This topic is closed to new replies.

Advertisement