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.
  • P Offline
    P Offline
    Psnarf
    wrote on 19 Sept 2019, 16:23 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
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 19 Sept 2019, 17:55 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
      • P Offline
        P Offline
        Psnarf
        wrote on 19 Sept 2019, 23:40 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.

        A 1 Reply Last reply 20 Sept 2019, 04:35
        0
        • P Psnarf
          19 Sept 2019, 23:40

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

          A Offline
          A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on 20 Sept 2019, 04:35 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
          • P Offline
            P Offline
            Psnarf
            wrote on 24 Sept 2019, 17:45 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!

            J 1 Reply Last reply 24 Sept 2019, 21:53
            1
            • P Psnarf
              24 Sept 2019, 17:45

              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!

              J Offline
              J Offline
              JonB
              wrote on 24 Sept 2019, 21:53 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

              1/6

              19 Sept 2019, 16:23

              • Login

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