Results 1 to 6 of 6

Thread: Qt 4.6.3 QByteArray reserve does't influence resize behavior

  1. #1
    Join Date
    Dec 2009
    Location
    Saint-Peterburg
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Qt 4.6.3 QByteArray reserve does't influence resize behavior

    Hi!
    According to this part of documentation:
    void QByteArray::reserve ( int size )
    Attempts to allocate memory for at least size bytes. If you know in advance how large the byte array will be, you can call this function, and if you call resize() often you are likely to get better performance. If size is an underestimate, the worst that will happen is that the QByteArray will be a bit slower.

    The sole purpose of this function is to provide a means of fine tuning QByteArray's memory usage. In general, you will rarely ever need to call this function. If you want to change the size of the byte array, call resize().
    the following code mustn't reallocate memory:

    Qt Code:
    1. QByteArray array;
    2. array.reserve(1024);
    3. array.resize(1024);
    4. assert(array.capacity() == 1024); // ok
    5. array.resize(120);
    6. assert(array.capacity() == 1024); // fail
    To copy to clipboard, switch view to plain text mode 
    What is wrong?

    Thank you.
    Last edited by AnatolyS; 21st August 2010 at 11:09. Reason: fixed code decoration

  2. #2
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Qt 4.6.3 QByteArray reserve does't influence resize behavior

    What does array.capacity() return? Print the value to the console.

    I suspect, though, that reserve() is meant as an aid for declaring arrays of larger than default initial capacity. resize(), then, still does what it is documented to do - changes the size of the array, shrinking it if required.

    If you need a constant block of memory with a variable "end" to it, you'll probably have to manage that yourself.

  3. #3
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qt 4.6.3 QByteArray reserve does't influence resize behavior

    I seem to recall that resize (and of course all the functions which inherently call resize, like chop) actually shrink the allocation, therefore reserve is meant for if you are going to be constantly growing the array, not if your going to be shrinking it.

    Checking the source, it seems to confirm this:
    "Qt 5.0: We need to add the missing capacity bit (like other tool classes have), to maintain the reserved memory on resize."

  4. The following user says thank you to squidge for this useful post:

    AnatolyS (21st August 2010)

  5. #4
    Join Date
    Dec 2009
    Location
    Saint-Peterburg
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt 4.6.3 QByteArray reserve does't influence resize behavior

    array.capacity() returns 108, but it does not matter. The core of this problem that resize reallocates memory unexpectedly. Look at QVector. QVector::reserve has the same documentation but one doesn't reallocate memory in this case:

    Qt Code:
    1. QVector<int> v;
    2. v.reserve(1024);
    3. v.resize(1024);
    4. assert(v.capacity() == 1024); // ok
    5. v.resize(100);
    6. assert(v.capacity() == 1024); // ok
    To copy to clipboard, switch view to plain text mode 
    Any comments?
    Last edited by AnatolyS; 21st August 2010 at 11:10. Reason: fixed code decoration

  6. #5
    Join Date
    Dec 2009
    Location
    Saint-Peterburg
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt 4.6.3 QByteArray reserve does't influence resize behavior

    I have looked at sources. QVectoralready has the capacity bit, but QByteArrayhas not one:
    Qt Code:
    1. void QVector<T>::reserve(int asize)
    2. { if (asize > d->alloc) realloc(d->size, asize); if (d->ref == 1) d->capacity = 1; }
    3. template <typename T>
    4. void QVector<T>::resize(int asize)
    5. { realloc(asize, (asize > d->alloc || (!d->capacity && asize < d->size && asize < (d->alloc >> 1))) ?
    6. QVectorData::grow(sizeOfTypedData(), asize, sizeof(T), QTypeInfo<T>::isStatic)
    7. : d->alloc); }
    To copy to clipboard, switch view to plain text mode 
    Last edited by AnatolyS; 21st August 2010 at 11:10.

  7. #6
    Join Date
    Mar 2012
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: Qt 4.6.3 QByteArray reserve does't influence resize behavior

    You can use simple code:

    Qt Code:
    1. class QExtByteArray : public QByteArray
    2. {
    3. public:
    4. QExtByteArray() {}
    5. ~QExtByteArray() {}
    6.  
    7. public:
    8. void clear();
    9. };
    10.  
    11. void QExtByteArray::clear()
    12. {
    13. data_ptr()->size = 0;
    14. }
    To copy to clipboard, switch view to plain text mode 

    clear() function set internal QByteArray size variable to zero, but alloc size will stay the same.

Similar Threads

  1. QVector::reserve() bug?
    By Caius Aérobus in forum Qt Programming
    Replies: 3
    Last Post: 6th June 2019, 11:34
  2. reserve a space of a list
    By maider in forum Qt Programming
    Replies: 6
    Last Post: 27th November 2009, 10:25
  3. Replies: 9
    Last Post: 25th July 2009, 13:27
  4. Replies: 5
    Last Post: 12th September 2008, 23:13
  5. Strange resize behavior
    By Lykurg in forum Newbie
    Replies: 3
    Last Post: 9th January 2007, 13:56

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.