开发者

Are data structures in Qt on the heap or on the stack?

开发者 https://www.devze.com 2023-03-06 11:04 出处:网络
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 p

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 开发者_JAVA技巧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.


Containers like QList, QVector etc. and data structures like QString, QByteArray, QFileInfo... can be created both on the stack and on the heap with new/delete. delete is always enough; Any special cleanup needed would be encapsulated in the destructor.

That said, all the classes I mentioned above should be created on the stack if there is not a strong reason for the heap (and there usually isn't). They are supposed to be passed by value/const reference instead of by reference or pointer. They are implicitely shared (copy-on-write), which means copies without modification are cheap. Opposed to java, where you pass almost everything by reference, here the objects are semantical values. Example:

 //getter returns copy
 QList<Something> Foo::somethings() const {
     return m_somethings;
 }

 //list is passed by const reference to the setter 
 void Foo::setSomethings( const QList<Something>& l ) {
     m_somethings = l;
 }
 ...

 //Get the value of m_somethings, creating a (yet unmodified) copy. 
 QList<Something> list = somethings();
 //modify the copy. m_somethings is _not_ modified (different object).  
 list.append( Something( "Something else" ) );
 //Write changes back to m_somethings
 setSomethings( list );

See also this answer to a similar question, explaining why QObjects are different.


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, delete would clean it up because it calls QFileInfoList's destructor, in which the Qt library does what's necessary to remove every resource that won't be used anymore.

0

精彩评论

暂无评论...
验证码 换一张
取 消