Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-26483

QVector can only allocate int bytes of memory

    XMLWordPrintable

Details

    • Bug
    • Resolution: Out of scope
    • P4: Low
    • None
    • 4.8.1, 4.8.4, 5.0.2
    • None
    • Windows 64-bit

    Description

      QVector can not allocate more than an int number of bytes. Even though qmalloc can handle a size_t allocation size, QVector's QVectorData uses int parameters in function signatures. This limitation means that QVector can only hold std::numeric_limits< int >::max() / sizeof( T ) number of elements in a QVector< T >.

      This limitation causes allocation crashes and is not documented as a limitation. Below is a sample application and the associated output:

      #include <QtCore>
      #include <vector>
      
      int main( int argc, char *argv[] )
      {
          Q_UNUSED( argc );
          Q_UNUSED( argv );
      
      	if( 8 != sizeof( void * ) )
      	{
      		QTextStream( stdout ) << "Test valid only on 64-bit architecture, exiting." << endl;
      	}
      
      	class LargeClass
      	{
      	public:
      		long long x[ 1000 ];
      	};
      
      	QTextStream( stdout ) << "Size of LargeClass:" << sizeof( LargeClass ) << endl;
      
      	QTextStream( stdout ) << "Attempting to allocate 1,000,000 LargeClass elements in a std::vector" << endl;
      
      	{
      		std::vector< LargeClass > v;
      		v.resize( 1000000 );
      		QTextStream( stdout ) << "Passed" << endl;
      
      	}
      
      	QTextStream( stdout ) << "Attempting to allocate 1,000,000 LargeClass elements in a QVector" << endl;
      
      	{
      		QVector< LargeClass > v;
      		v.resize( 1000000 );
      		QTextStream( stdout ) << "Passed" << endl;
      
      	}
      
          return 0;
      }
      
      QVectorFailedAllocate.exe
      Size of LargeClass:8000
      Attempting to allocate 1,000,000 LargeClass elements in a std::vector
      Passed
      Attempting to allocate 1,000,000 LargeClass elements in a QVector
      
      (Crash)
      

      QVector should be able to handle an int number of elements given the interface and documentation. Ideally it would be able to handle a size_t number of elements.

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            Unassigned Unassigned
            srs232 Samuel Seidel
            Votes:
            1 Vote for this issue
            Watchers:
            6 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes