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.
  • sitesvS Offline
    sitesvS Offline
    sitesv
    wrote on last edited by
    #1

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

    jsulmJ JKSHJ 2 Replies Last reply
    0
    • sitesvS sitesv

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

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on 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

      sitesvS 1 Reply Last reply
      1
      • jsulmJ jsulm

        @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.

        sitesvS Offline
        sitesvS Offline
        sitesv
        wrote on 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?

        VRoninV 1 Reply Last reply
        0
        • sitesvS sitesv

          @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?

          VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by VRonin
          #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

          sitesvS 1 Reply Last reply
          4
          • VRoninV VRonin

            @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();

            sitesvS Offline
            sitesvS Offline
            sitesv
            wrote on 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!

            jsulmJ 1 Reply Last reply
            0
            • sitesvS sitesv

              @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!

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on 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
              • VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on 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
                • sitesvS sitesv

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

                  JKSHJ Offline
                  JKSHJ Offline
                  JKSH
                  Moderators
                  wrote on last edited by JKSH
                  #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 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

                    • Login

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