Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. About QString's empty string allocation inconsistency
Forum Update on Monday, May 27th 2025

About QString's empty string allocation inconsistency

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 5 Posters 988 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dldd
    wrote on last edited by
    #1

    Consider the following cases for Qt 5.13.1:

    QString a;               // 1
    QString b("");           // 2
    QString::fromUtf8("");   // 3
    QString::fromLatin1(""); // 4
    

    It seems that QString does dynamic memory allocation in cases 2 and 3, but not in cases 1 and 4. This seems peculiar:

    • Why to allocate for empty string in the first place?
    • Why to allocate only for some kind of empty strings?

    As for the rationale for this question, was going through some code which had something like this:
    QString::fromUtf8(someFunctionReturningCharPtrToUtf8Data());
    This could get called quite often and the function may frequently return empty string, so it would seem reasonable that QString always handled the empty string case without redundant allocations so that user wouldn't need to work around it manually.

    References:
    qstring.h (5.13.1)
    qstring.cpp (5.13.1)
    qutfcoded.cpp (5.13.1)
    qarraydata.cpp (5.13.1)

    VRoninV 1 Reply Last reply
    0
    • D dldd

      Consider the following cases for Qt 5.13.1:

      QString a;               // 1
      QString b("");           // 2
      QString::fromUtf8("");   // 3
      QString::fromLatin1(""); // 4
      

      It seems that QString does dynamic memory allocation in cases 2 and 3, but not in cases 1 and 4. This seems peculiar:

      • Why to allocate for empty string in the first place?
      • Why to allocate only for some kind of empty strings?

      As for the rationale for this question, was going through some code which had something like this:
      QString::fromUtf8(someFunctionReturningCharPtrToUtf8Data());
      This could get called quite often and the function may frequently return empty string, so it would seem reasonable that QString always handled the empty string case without redundant allocations so that user wouldn't need to work around it manually.

      References:
      qstring.h (5.13.1)
      qstring.cpp (5.13.1)
      qutfcoded.cpp (5.13.1)
      qarraydata.cpp (5.13.1)

      VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #4

      @dldd said in About QString's empty string allocation inconsistency:

      does dynamic memory allocation in cases 2 and 3, but not in cases 1 and 4

      Case 2 calls Case 3 internally so they are the same. But you are right. fromUtf8 calls QString::QString(int size, Qt::Initialization) that misses the check for lenght size that is done in other constructors. We can call this a bug. Feel free to submit a patch.

      What it is vs what it should be:

      QString::QString(int size, Qt::Initialization)
      {
          if (size <= 0) {
              d = Data::allocate(0);
          } else {
              d = Data::allocate(size + 1);
              Q_CHECK_PTR(d);
              d->size = size;
              d->data()[size] = '\0';
          }
      }
      

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      D 1 Reply Last reply
      3
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi,

        For that kind of low level details, you better ask on the interest mailing list. You'll find there Qt's developers/maintainers. This forum is more user oriented.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        2
        • JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by
          #3

          I don't know the reasons for the internal designs, but note that Case #1 is a null string, while cases #2-#4 are empty strings. See QString::isNull() and QString::isEmpty()

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          1 Reply Last reply
          2
          • D dldd

            Consider the following cases for Qt 5.13.1:

            QString a;               // 1
            QString b("");           // 2
            QString::fromUtf8("");   // 3
            QString::fromLatin1(""); // 4
            

            It seems that QString does dynamic memory allocation in cases 2 and 3, but not in cases 1 and 4. This seems peculiar:

            • Why to allocate for empty string in the first place?
            • Why to allocate only for some kind of empty strings?

            As for the rationale for this question, was going through some code which had something like this:
            QString::fromUtf8(someFunctionReturningCharPtrToUtf8Data());
            This could get called quite often and the function may frequently return empty string, so it would seem reasonable that QString always handled the empty string case without redundant allocations so that user wouldn't need to work around it manually.

            References:
            qstring.h (5.13.1)
            qstring.cpp (5.13.1)
            qutfcoded.cpp (5.13.1)
            qarraydata.cpp (5.13.1)

            VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #4

            @dldd said in About QString's empty string allocation inconsistency:

            does dynamic memory allocation in cases 2 and 3, but not in cases 1 and 4

            Case 2 calls Case 3 internally so they are the same. But you are right. fromUtf8 calls QString::QString(int size, Qt::Initialization) that misses the check for lenght size that is done in other constructors. We can call this a bug. Feel free to submit a patch.

            What it is vs what it should be:

            QString::QString(int size, Qt::Initialization)
            {
                if (size <= 0) {
                    d = Data::allocate(0);
                } else {
                    d = Data::allocate(size + 1);
                    Q_CHECK_PTR(d);
                    d->size = size;
                    d->data()[size] = '\0';
                }
            }
            

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            D 1 Reply Last reply
            3
            • VRoninV VRonin

              @dldd said in About QString's empty string allocation inconsistency:

              does dynamic memory allocation in cases 2 and 3, but not in cases 1 and 4

              Case 2 calls Case 3 internally so they are the same. But you are right. fromUtf8 calls QString::QString(int size, Qt::Initialization) that misses the check for lenght size that is done in other constructors. We can call this a bug. Feel free to submit a patch.

              What it is vs what it should be:

              QString::QString(int size, Qt::Initialization)
              {
                  if (size <= 0) {
                      d = Data::allocate(0);
                  } else {
                      d = Data::allocate(size + 1);
                      Q_CHECK_PTR(d);
                      d->size = size;
                      d->data()[size] = '\0';
                  }
              }
              
              D Offline
              D Offline
              dldd
              wrote on last edited by
              #5

              Thanks for the responses. While the adjustment itself is expected to be short, editing such a core part of Qt as QString's constructor is something I'll leave to real Qt developers. But I'll considered creating a ticket to Qt bug tracker.

              SGaistS 1 Reply Last reply
              0
              • D dldd

                Thanks for the responses. While the adjustment itself is expected to be short, editing such a core part of Qt as QString's constructor is something I'll leave to real Qt developers. But I'll considered creating a ticket to Qt bug tracker.

                SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #6

                @dldd said in About QString's empty string allocation inconsistency:

                Thanks for the responses. While the adjustment itself is expected to be short, editing such a core part of Qt as QString's constructor is something I'll leave to real Qt developers.

                Why ? As you said, it's a small modification. This is a good way to get started contributing :-)

                You'll be a "real Qt developers" as other contributors. The fact that this is a core class shouldn't make you afraid of fixing it. On the contrary, you'll have a nice impact for other Qt users.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                D 1 Reply Last reply
                3
                • SGaistS SGaist

                  @dldd said in About QString's empty string allocation inconsistency:

                  Thanks for the responses. While the adjustment itself is expected to be short, editing such a core part of Qt as QString's constructor is something I'll leave to real Qt developers.

                  Why ? As you said, it's a small modification. This is a good way to get started contributing :-)

                  You'll be a "real Qt developers" as other contributors. The fact that this is a core class shouldn't make you afraid of fixing it. On the contrary, you'll have a nice impact for other Qt users.

                  D Offline
                  D Offline
                  dldd
                  wrote on last edited by
                  #7

                  @SGaist said in About QString's empty string allocation inconsistency:

                  Why ? As you said, it's a small modification. This is a good way to get started contributing :-)

                  You'll be a "real Qt developers" as other contributors. The fact that this is a core class shouldn't make you afraid of fixing it. On the contrary, you'll have a nice impact for other Qt users.

                  Because "real Qt developers" have better insight on matters :) As demonstrated in this case: the bug report QTBUG-80808 was closed as "Won't Do" and I don't think the resolution would have been different had there been a patch. While I don't quite understand why one would like to allocate for empty string when there already are mechanisms used to avoid it, I'm not in the position to start questioning it.

                  aha_1980A 1 Reply Last reply
                  0
                  • D dldd

                    @SGaist said in About QString's empty string allocation inconsistency:

                    Why ? As you said, it's a small modification. This is a good way to get started contributing :-)

                    You'll be a "real Qt developers" as other contributors. The fact that this is a core class shouldn't make you afraid of fixing it. On the contrary, you'll have a nice impact for other Qt users.

                    Because "real Qt developers" have better insight on matters :) As demonstrated in this case: the bug report QTBUG-80808 was closed as "Won't Do" and I don't think the resolution would have been different had there been a patch. While I don't quite understand why one would like to allocate for empty string when there already are mechanisms used to avoid it, I'm not in the position to start questioning it.

                    aha_1980A Offline
                    aha_1980A Offline
                    aha_1980
                    Lifetime Qt Champion
                    wrote on last edited by
                    #8

                    @dldd said in About QString's empty string allocation inconsistency:

                    While I don't quite understand why one would like to allocate for empty string when there already are mechanisms used to avoid it, I'm not in the position to start questioning it.

                    Most often the answer is: binary compatibility, which is guaranteed for all 5.x versions and can be broken by 6.0.

                    As Thiago said, there are plans to change QString within the next year, so stay tuned, as I do.

                    Regards

                    Qt has to stay free or it will die.

                    D 1 Reply Last reply
                    0
                    • aha_1980A aha_1980

                      @dldd said in About QString's empty string allocation inconsistency:

                      While I don't quite understand why one would like to allocate for empty string when there already are mechanisms used to avoid it, I'm not in the position to start questioning it.

                      Most often the answer is: binary compatibility, which is guaranteed for all 5.x versions and can be broken by 6.0.

                      As Thiago said, there are plans to change QString within the next year, so stay tuned, as I do.

                      Regards

                      D Offline
                      D Offline
                      dldd
                      wrote on last edited by
                      #9

                      @aha_1980 said in About QString's empty string allocation inconsistency:

                      Most often the answer is: binary compatibility, which is guaranteed for all 5.x versions and can be broken by 6.0.

                      As far as I understand, the fix (e.g. the one demonstrated by VRonin) would not break binary compatibility

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved