I’m wondering if Qt puts things like “version 1” of the below code on the heap? In version 1, would dirStuff be placed on the stack or the heap by Qt? I’m asking because I have a feeling that Java puts all data structures on the heap… Not sure cause I don’t remember ever having to think about this with Java.
version 1
QFileInfoList dirStuff = dir.entryInfoList(QDir::Dirs | QDir::Files |
QDir::NoDotAndDotDot | QDir::System | QDir::Hidden);
version 2
QFileInfoList * dirStuff = new QFileInfoList();
*dirStuff = dir->entryInfoList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot |
QDir::System | QDir::Hidden);
Of course, I’ll assume that version 2 is on the heap, but I’m wondering if…
delete dirStuff;
would really clean up “version 2” of dirStuff because it is not a regular object but probably a linked list. The cleaning up of which I think could be problematic, so I’m wondering if there is a special Qt way I should be cleaning up a Qt data structure like a QFileInfoList on the heap… or if the regular old C++ delete is good enough.
In version 1, the object will be put on the stack, but it may internally point to data allocated in the heap (if it dynamically allocates memory inside its methods).
About version 2,
deletewould clean it up because it callsQFileInfoList‘s destructor, in which the Qt library does what’s necessary to remove every resource that won’t be used anymore.