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

QString's allocation behaviour is inconsistent for empty string constructions

    XMLWordPrintable

Details

    • Bug
    • Resolution: Won't Do
    • Not Evaluated
    • None
    • 5.13.1
    • None
    • All

    Description

      Consider the following cases in Qt 5.13.1:

      QString a;               // 1, isNull() == true
      QString b("");           // 2, isNull() == false
      QString::fromUtf8("");   // 3, isNull() == false
      QString::fromLatin1(""); // 4, isNull() == false

      It seems that QString does dynamic memory allocation in cases 2 and 3 (which are effectively the same cases given the specification of const char* constructor), but not in cases 1 and 4.

      • Is there reason to ever allocate for empty string?
      • Should fromUtf8() and fromLatin1() behave differently with respect to memory allocation when given empty string?

      Technical details:

      • fromLatin1(): calls fromLatin1_helper() which in case of zero-sized string calls
        d = Data::allocate(0);

        Data is a typedef of QStringData (a typedef forQTypedArrayData<ushort> defined in qstringliteral.h), and Data::allocate() returns qt_array_empty (or qt_array_unsharable_empty) given the zero-valued parameter.

      • fromUtf8(): calls fromUtf8_helper() -> QUtf8::convertToUnicode("", 0) -> QString result(0, Qt::Uninitialized); -> d = Data::allocate(0 + 1);

      Attachments

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

        Activity

          People

            thiago Thiago Macieira
            dldd dldd
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes