[Qt-interest] QList: Force a deep copy?

Thiago Macieira thiago.macieira at trolltech.com
Thu Jul 2 09:05:54 CEST 2009


Samuel Rødal wrote:
>Eric Clark wrote:
>> I don't typically compile Qt. Usually, I use the binaries that have
>> already been compiled. It may come to me having to do that though. I
>> did notice in the QList class that they are use the placement new
>> operator. Does anyone know what Qt expects this to do?
>>
>> template <typename T>
>> Q_INLINE_TEMPLATE void QList<T>::node_construct(Node *n, const T &t)
>> {
>>     if (QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic) n->v = new
>> T(t); else if (QTypeInfo<T>::isComplex) new (n) T(t);
>>     else *reinterpret_cast<T*>(n) = t;
>> }
>>
>> In the second line of the function:
>>
>> else if (QTypeInfo<T>::isComplex) new (n) T(t);
>>
>> Qt is using the placement new operator here. We do not overload this
>> operator in our stuff. What does Qt expect it to do? Does it expect
>> realloc to get called, or does it just expect "n" to be returned?
>>
>> Thanks,
>> Eric
>
>Placement new simply initializes an instance of T in the given memory
>area, calling the constructor in the process. No new memory is
>allocated. See
> http://www.parashift.com/c++-faq-lite/dtors.html#faq-11.10
>
>Also, when placement new is used delete isn't called, but the destructor
>is called explicitly, like this in QList's case:
>
>reinterpret_cast<T*>(n)->~T()
>
>So it doesn't sound like placement new should cause any problems in
>combination with nedmalloc, since it doesn't do any memory allocation by
>itself.

Correct. However, QList does allocate/deallocate memory for large (larger 
than sizeof(void*)) or static types:

template <typename T>
Q_INLINE_TEMPLATE void QList<T>::node_construct(Node *n, const T &t)
{
    if (QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic) n->v = new T(t);
    else if (QTypeInfo<T>::isComplex) new (n) T(t);
    else *reinterpret_cast<T*>(n) = t;
}

Eric was having problems with QModelIndexList, which is 
QList<QModelIndex>. QModelIndex is movable (non-static), but is larger 
than void*. So the first line of the node_construct function applies.

The problem is nedmalloc. Eric, on Saturday, you had already asked his 
question. And I replied on Sunday that he should rebuild all his 
dependencies using nedmalloc, or use it nowhere (see Message-id: 
<200906280039.40821.thiago.macieira at trolltech.com>).

Yet Monday you had ignored my advice and tried to find a workaround that is 
obviously not working.

-- 
Thiago Macieira - thiago.macieira (AT) nokia.com
  Senior Product Manager - Nokia, Qt Software
      Sandakerveien 116, NO-0402 Oslo, Norway
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090702/ecb0af6b/attachment.bin 


More information about the Qt-interest-old mailing list