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. QString append vs insert inside loop
Forum Updated to NodeBB v4.3 + New Features

QString append vs insert inside loop

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 4 Posters 824 Views 1 Watching
  • 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.
  • PsnarfP Offline
    PsnarfP Offline
    Psnarf
    wrote on last edited by Psnarf
    #1

    char buf[80]; QString str; QTextstream ts; volatile int pos=0;

    Inside a loop, get 80 chars from ts into buf, test for the end condition, if not end, str.append(buf). I noticed strange chars getting into str. The doc on QString::append says append "preallocates extra space at the end of the string data so it can grow without reallocating the entire string each time." Do subsequent appends place the new data at the end of the preallocated extra space? That would explain the garbage every mod(80) of the final QString.
    I changed str.append to str.insert(pos, buf) to get around this unexpected behavior.

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Please show us your code. The internal pre-allocation has for sure nothing to do with your problems.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      4
      • PsnarfP Offline
        PsnarfP Offline
        Psnarf
        wrote on last edited by
        #3

        Thank you for your reply. I re-designed the function with a different algorithm. Thank for the clarification. Append joins existing and new strings.

        aha_1980A 1 Reply Last reply
        0
        • PsnarfP Psnarf

          Thank you for your reply. I re-designed the function with a different algorithm. Thank for the clarification. Append joins existing and new strings.

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

          @psnarf if you append from a char[] array, that must be properly null-terminated. otherwise you get jndefined behavior.

          Regards

          Qt has to stay free or it will die.

          1 Reply Last reply
          4
          • PsnarfP Offline
            PsnarfP Offline
            Psnarf
            wrote on last edited by
            #5

            Thank you, Aha. The FITS header consists of 80-char 'cards' probably from days of Hollerith punched cards. The header has no delimiters, a 200-card header is a 16,000 char string. Will go back and try null-terminated strings in a ListWidget, but now using QStringListModel with QListView. I'm sure the missing null termination produced the undefined behavior, with appended strings beginning half a dozen chars past the previous string. Appreciate your kind explanation!

            JonBJ 1 Reply Last reply
            1
            • PsnarfP Psnarf

              Thank you, Aha. The FITS header consists of 80-char 'cards' probably from days of Hollerith punched cards. The header has no delimiters, a 200-card header is a 16,000 char string. Will go back and try null-terminated strings in a ListWidget, but now using QStringListModel with QListView. I'm sure the missing null termination produced the undefined behavior, with appended strings beginning half a dozen chars past the previous string. Appreciate your kind explanation!

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @Psnarf
              "nul terminated strings" emerged as a convention with the definition of the C programming languages in the 1970s. Your "Hollerith punched cards" emerged in the 1890s, as in 1890s not 1990s! :)

              I presume you cannot assume any nul-termination. That means you cannot treat the 80 characters via functions which take a C-style-terminated string. At a guess, to produce a QString try https://doc.qt.io/qt-5/qstring.html#fromLatin1 or https://doc.qt.io/qt-5/qstring.html#fromLocal8Bit, passing 80 for the size parameter.

              1 Reply Last reply
              1

              • Login

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