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 array: easy way to clean
Forum Updated to NodeBB v4.3 + New Features

QString array: easy way to clean

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 5 Posters 1.3k Views 2 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.
  • S Offline
    S Offline
    sitesv
    wrote on 10 Jul 2020, 13:15 last edited by
    #1

    Hello!
    What is the easiest way to clear QString[] array?

    J J 2 Replies Last reply 10 Jul 2020, 13:18
    0
    • S sitesv
      10 Jul 2020, 13:15

      Hello!
      What is the easiest way to clear QString[] array?

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 10 Jul 2020, 13:18 last edited by
      #2

      @sitesv Do you mean to empty each QString in the array?
      If so https://en.cppreference.com/w/cpp/algorithm/fill comes to my mind.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      S 1 Reply Last reply 10 Jul 2020, 13:34
      1
      • J jsulm
        10 Jul 2020, 13:18

        @sitesv Do you mean to empty each QString in the array?
        If so https://en.cppreference.com/w/cpp/algorithm/fill comes to my mind.

        S Offline
        S Offline
        sitesv
        wrote on 10 Jul 2020, 13:34 last edited by
        #3

        @jsulm
        When I have worked with AnsiString / UnicodeString, I could use a "memset" function for cleaning array.
        But for QString objects, this is not a solution.

        AnsiString myarray[1000];
        ...
        memset(myarray,0,sizeof(myarray));
        

        Is it possible to clean a QString array without 'for' statement?

        V 1 Reply Last reply 10 Jul 2020, 13:45
        0
        • S sitesv
          10 Jul 2020, 13:34

          @jsulm
          When I have worked with AnsiString / UnicodeString, I could use a "memset" function for cleaning array.
          But for QString objects, this is not a solution.

          AnsiString myarray[1000];
          ...
          memset(myarray,0,sizeof(myarray));
          

          Is it possible to clean a QString array without 'for' statement?

          V Offline
          V Offline
          VRonin
          wrote on 10 Jul 2020, 13:45 last edited by VRonin 7 Oct 2020, 13:47
          #4

          @sitesv said in QString array: easy way to clean:

          AnsiString myarray[1000];
          ...
          memset(myarray,0,sizeof(myarray));

          this is bad, real bad. The destructor for the strings doesn't get called.

          I don't think you can do better than for(auto& i : myarray) i->clear();

          "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

          S 1 Reply Last reply 10 Jul 2020, 13:52
          4
          • V VRonin
            10 Jul 2020, 13:45

            @sitesv said in QString array: easy way to clean:

            AnsiString myarray[1000];
            ...
            memset(myarray,0,sizeof(myarray));

            this is bad, real bad. The destructor for the strings doesn't get called.

            I don't think you can do better than for(auto& i : myarray) i->clear();

            S Offline
            S Offline
            sitesv
            wrote on 10 Jul 2020, 13:52 last edited by
            #5

            @VRonin Thanks! This is it!

            @VRonin said in QString array: easy way to clean:

            this is bad, real bad. The destructor for the strings doesn't get called.

            Why it is bad? It's working!

            J 1 Reply Last reply 10 Jul 2020, 13:56
            0
            • S sitesv
              10 Jul 2020, 13:52

              @VRonin Thanks! This is it!

              @VRonin said in QString array: easy way to clean:

              this is bad, real bad. The destructor for the strings doesn't get called.

              Why it is bad? It's working!

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 10 Jul 2020, 13:56 last edited by
              #6

              @sitesv said in QString array: easy way to clean:

              Why it is bad?

              Because of "The destructor for the strings doesn't get called". In worst case not all resources are freed because destructor is not called (depends on what AnsiString is doing internally).

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              2
              • V Offline
                V Offline
                VRonin
                wrote on 10 Jul 2020, 15:37 last edited by
                #7

                From the docs:

                An AnsiString variable is a structure containing string information. When the variable is empty (when it contains a zero-length string), the pointer is nil and the string uses no additional storage. When the variable is nonempty, it points to a dynamically allocated block of memory that contains the string value.

                You are just overwriting your array memory with all 0 so the memory allocated by AnsiString is leaked

                "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

                1 Reply Last reply
                3
                • S sitesv
                  10 Jul 2020, 13:15

                  Hello!
                  What is the easiest way to clear QString[] array?

                  J Offline
                  J Offline
                  JKSH
                  Moderators
                  wrote on 11 Jul 2020, 07:57 last edited by JKSH 7 Nov 2020, 09:52
                  #8

                  I agree with @VRonin and @jsulm -- using memset() causes a memory leak here because it doesn't release the heap-allocated memory.

                  @sitesv said in QString array: easy way to clean:

                  QString[] array

                  Also, you should create a QStringList instead of a QString[] array.

                  See https://doc.qt.io/qt-5/qstringlist.html

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

                  1 Reply Last reply
                  2
                  • mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 11 Jul 2020, 08:05 last edited by
                    #9

                    Hi
                    Just as a note for anyone landing here from google.

                    Using memset() on any kind of Object instances is as good an idea as using torches as
                    means of lighting at the gasoline depot....

                    1 Reply Last reply
                    2

                    1/9

                    10 Jul 2020, 13:15

                    • Login

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