[Qt-interest] QList, copy problems
Thiago Macieira
thiago at kde.org
Sat Oct 15 16:17:48 CEST 2011
On Saturday, 15 de October de 2011 15:23:22 Till Oliver Knoll wrote:
> Am 15.10.11 12:35, schrieb Thiago Macieira:
> > On Saturday, 15 de October de 2011 12:06:44 Till Oliver Knoll wrote:
> >> Am 15.10.2011 um 07:07 schrieb Sahana
Bhaskar<Sahana_Bhaskar at infosys.com>:
> >>> Any inputs will be appreciated. If I use operator= any modifications to
> >>> the original list( after initializing ) is causing a crash!!!
> >>
> >> Ouch! = does only a shallow copy! In your case you might need a deep copy
> >> however!
> >
> > Deep copies should never be necessary.
>
> That statement totally doesn't make sense. It all depends on your
> use-case off course: if you want to have two completely separate
> "instances" of your list which you can modify without affecting the
> other instance, then you need a deep copy.
Not really. QList will make a deep copy the moment that you try to modify it.
So you don't need to make a deep copy before QList makes a deep copy for you.
> If on the other hand you want the items in the list to be unique, then
> you make a shallow copy, but you have to be aware that when you delete
> those instances your other copy of the list becomes invalid.
That has nothing to do with QList and copying. If you want unique elements,
you can't copy the list, you need to create a new one and create new items.
> But you can't say the one usage makes more sense than the other usage.
>
> And according to the description of the OP it just appeared to me that
> this is what happens ("lists of pointers to some objects in memory,
> shallow copy being done, you modify one list, *bang!*" sounds pretty
> much like "the usual suspects").
Sounds to me like a bad question. If you do this:
QList<QObject *> list;
list << new QObject;
// copy the list:
QList<QObject *> list2 = list;
// "modify the list"
delete list[0];
// access the copy
qDebug() << list2[0];
The above might crash. But it's not because of deep or shallow copies. It's
also not because the original list was modified -- it wasn't! The problem was
that the object pointed to by *both* lists was deleted.
If we change the copying of the list by a deep copy:
QList<Object *> list2;
list2 << list.at(0);
The problem is still present.
> Hence my formulation "you *might* need a deep copy", as we simply have
> not enough information about the particular use-case. And we don't know
> how and when the list items are deallocated.
And my formulation of "deep copies are never necessary" is also correct. It's
not about the list's contents, but objects pointed to.
Either that or the list itself was already deleted.
I agree with you that we simply don't have enough information.
--
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
Software Architect - Intel Open Source Technology Center
PGP/GPG: 0x6EF45358; fingerprint:
E067 918B B660 DBD1 105C 966C 33F5 F005 6EF4 5358
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 190 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20111015/94e0796e/attachment.bin
More information about the Qt-interest-old
mailing list