Reputation: 51
Say I have a member variable called Result m_result
class Result{
QList<Group *> m_groups
}
and Group
class Group{
QList<Data> m_data
}
As the program continues, each groups' QList of Data keeps growing. All groups are allocated in the heap ( hence the pointer). In essence, each group gets allocated ONCE in the heap. Does a particular group get re-allocated each time its QList grows? Also, does m_testResult get recopied over and over again because of the growth of Data members?
Upvotes: 1
Views: 138
Reputation: 49321
Inside QList, there will be pointers to some other objects or arrays which actually hold the data. As the list grows, new objects for this backing data will be allocated and the existing ones deleted. The code which you've shown doesn't have to worry about it, but if you are implement your own collections then you would. The objects themselves don't ever grow once they are allocated.
The details for QList can be found here - it uses an array of pointers to <T>
, so isn't a linked list. As it uses pointers to the elements it does not have to copy the elements when the array is resized, just copying the pointers ( or possibly not if it's implemented in a similar fashion to a VList - I didn't see anything in the document to indicate which strategy it uses ), so a particular group will not get re-allocated each time its QList grows.
Upvotes: 7
Reputation: 20383
I don't know about QList
in particular, but, in general, I would expect the following:
Upon adding a new element, the QList
allocates (new
) more memory and links the last element to the new element.
Upon removing an (existing) element, the element's predecessor is linked to its successor, and the memory holding the element is delete
-ed.
This is the general principle on how any linked list works, such as std::list
or std::slist
Objects never grow, but memory can be claimed and released repeatedly.
Upvotes: 2