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. QBuffer.buffer().clear() doesn't affect to size
Forum Updated to NodeBB v4.3 + New Features

QBuffer.buffer().clear() doesn't affect to size

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 4 Posters 684 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
    DungeonLords
    wrote on last edited by DungeonLords
    #1

    In my code I add Byte to QBuffer buf and then do buf.buffer().clear()

    QDataStream stream(&buf);
    stream << (quint8)0xAB;
    buf.buffer().clear();
    

    Looks like buf.size() and buf.buffer().size() both not changed after clear... Why?

    P.S. For me also not clear what is diff between buf.size() and buf.buffer().size()?

    Test code in my repo.

    jsulmJ JonBJ 2 Replies Last reply
    0
    • D DungeonLords

      I want to reuse memory which is already allocated. In my example I use my own
      counter variable buf_cnt to check how many Bytes in my buf, I don't use buf.size()
      For example here I am writing to file

      write(buf.buffer().constData(), buf_cnt);
      

      Even if buf.size()=2 I will write correct buf_cnt Bytes. Is it good idea to reuse memory which is already allocated?

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

      @DungeonLords
      So you (a) maintain your own count in addition to the (different) value in the buffer's count and (b) must therefore only ever use write() on the buffer with your own intended count. That seems strange and unnecessary. I would rather keep the buffer's count correct.

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

        This works fine for me:

        QBuffer buf;
        buf.open(QBuffer::WriteOnly);
        QDataStream stream(&buf);
        stream << (quint8)0xAB;
        qDebug() << buf.size() << buf.buffer().size();
        buf.buffer().clear();
        qDebug() << buf.size() << buf.buffer().size();
        

        --> returns
        1 1
        0 0

        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
        0
        • D DungeonLords

          In my code I add Byte to QBuffer buf and then do buf.buffer().clear()

          QDataStream stream(&buf);
          stream << (quint8)0xAB;
          buf.buffer().clear();
          

          Looks like buf.size() and buf.buffer().size() both not changed after clear... Why?

          P.S. For me also not clear what is diff between buf.size() and buf.buffer().size()?

          Test code in my repo.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #3

          @DungeonLords said in QBuffer.buffer().clear() doesn't affect to size:

          For me also not clear what is diff between buf.size() and buf.buffer().size()?

          There is no difference. QBuffer::size is there because QBuffer is an IODevice which has a size() method.

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

          1 Reply Last reply
          0
          • D DungeonLords

            In my code I add Byte to QBuffer buf and then do buf.buffer().clear()

            QDataStream stream(&buf);
            stream << (quint8)0xAB;
            buf.buffer().clear();
            

            Looks like buf.size() and buf.buffer().size() both not changed after clear... Why?

            P.S. For me also not clear what is diff between buf.size() and buf.buffer().size()?

            Test code in my repo.

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

            @DungeonLords
            In your original code you have

                //Write to file
                write(buf.buffer().constData(), buf_cnt);
            ...
                //Choose A) or B)
                //A) Open-close way
                // buf.close();
                // buf.open(QBuffer::WriteOnly);
                //B) Clear way
                buf.buffer().clear();
            

            Because at start you have already written into the buf.buffer() B does not leave you in same state as A. Opening and closing in A resets the current file offset pointer to 0/beginning of file. Just calling buf.buffer().clear(); in B leaves the current file offset pointer where it is, at 2. For B you need to insert buf.seek(0L); either before or after the buf.buffer().clear(); to achieve the same situation, then it will report 1 instead of 2 just like A does.

            D 1 Reply Last reply
            2
            • JonBJ JonB

              @DungeonLords
              In your original code you have

                  //Write to file
                  write(buf.buffer().constData(), buf_cnt);
              ...
                  //Choose A) or B)
                  //A) Open-close way
                  // buf.close();
                  // buf.open(QBuffer::WriteOnly);
                  //B) Clear way
                  buf.buffer().clear();
              

              Because at start you have already written into the buf.buffer() B does not leave you in same state as A. Opening and closing in A resets the current file offset pointer to 0/beginning of file. Just calling buf.buffer().clear(); in B leaves the current file offset pointer where it is, at 2. For B you need to insert buf.seek(0L); either before or after the buf.buffer().clear(); to achieve the same situation, then it will report 1 instead of 2 just like A does.

              D Offline
              D Offline
              DungeonLords
              wrote on last edited by DungeonLords
              #5

              @JonB if I want to use B) way then may be I no need buf.buffer().clear()? Looks like just buf.seek(0L) is enough. Isn't it?

              P.S. I improve my example to make it more clear for others beginners.

              Christian EhrlicherC 1 Reply Last reply
              0
              • D DungeonLords

                @JonB if I want to use B) way then may be I no need buf.buffer().clear()? Looks like just buf.seek(0L) is enough. Isn't it?

                P.S. I improve my example to make it more clear for others beginners.

                Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #6

                @DungeonLords said in QBuffer.buffer().clear() doesn't affect to size:

                Looks like just buf.seek(0L) is enough. Isn't it?

                This just sets the pointer to the buffer at the front - why should this be the same?

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

                D 1 Reply Last reply
                2
                • Christian EhrlicherC Christian Ehrlicher

                  @DungeonLords said in QBuffer.buffer().clear() doesn't affect to size:

                  Looks like just buf.seek(0L) is enough. Isn't it?

                  This just sets the pointer to the buffer at the front - why should this be the same?

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

                  @Christian-Ehrlicher looks like I should describe task. I need to use QBuffer to temporary save data before I decide to write it to file; after data will be written to file I need to collect again... Is buf.seek(0L) enough for my task?

                  Christian EhrlicherC JonBJ 2 Replies Last reply
                  0
                  • D DungeonLords

                    @Christian-Ehrlicher looks like I should describe task. I need to use QBuffer to temporary save data before I decide to write it to file; after data will be written to file I need to collect again... Is buf.seek(0L) enough for my task?

                    Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #8

                    @DungeonLords said in QBuffer.buffer().clear() doesn't affect to size:

                    Is buf.seek(0L) enough for my task?

                    Why should it be? As I wrote it only moves the pointer to the first byte - nothing more. So your old data is still there and might be overriden (or not, depending on the size you're writing) - why not simply use clear() as you wanted in your very first post?

                    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
                    1
                    • D DungeonLords

                      @Christian-Ehrlicher looks like I should describe task. I need to use QBuffer to temporary save data before I decide to write it to file; after data will be written to file I need to collect again... Is buf.seek(0L) enough for my task?

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

                      @DungeonLords
                      As @Christian-Ehrlicher has written, you do need to clear() the buffer in order to reset/empty it. This is true for a file or a buffer. You had written 2 bytes to it. If you simply seek to the beginning it still has 2 bytes, you have just changed where the next byte will be written. If you then write 1 byte that does not make the content just 1 byte long, rather it means it has the new byte at the start followed by the earlier second byte in second position and is still 2 bytes long.

                      All you have to do is use buf.buffer().clear() immediately followed by buf.seek(0L), so what's the problem? You might argue that the clear() ought do the seek() automatically, but apparently it does not so you just need to do it yourself.

                      1 Reply Last reply
                      2
                      • D Offline
                        D Offline
                        DungeonLords
                        wrote on last edited by
                        #10

                        I want to reuse memory which is already allocated. In my example I use my own
                        counter variable buf_cnt to check how many Bytes in my buf, I don't use buf.size()
                        For example here I am writing to file

                        write(buf.buffer().constData(), buf_cnt);
                        

                        Even if buf.size()=2 I will write correct buf_cnt Bytes. Is it good idea to reuse memory which is already allocated?

                        Christian EhrlicherC JonBJ 2 Replies Last reply
                        0
                        • D DungeonLords

                          I want to reuse memory which is already allocated. In my example I use my own
                          counter variable buf_cnt to check how many Bytes in my buf, I don't use buf.size()
                          For example here I am writing to file

                          write(buf.buffer().constData(), buf_cnt);
                          

                          Even if buf.size()=2 I will write correct buf_cnt Bytes. Is it good idea to reuse memory which is already allocated?

                          Christian EhrlicherC Offline
                          Christian EhrlicherC Offline
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on last edited by
                          #11

                          @DungeonLords said in QBuffer.buffer().clear() doesn't affect to size:

                          I want to reuse memory which is already allocated.

                          Why do you take care of such low-level stuff. Do you really think QByteArray::clear() deletes the memory when you call clear()? Did you take a look if it is the case?

                          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
                          2
                          • D DungeonLords

                            I want to reuse memory which is already allocated. In my example I use my own
                            counter variable buf_cnt to check how many Bytes in my buf, I don't use buf.size()
                            For example here I am writing to file

                            write(buf.buffer().constData(), buf_cnt);
                            

                            Even if buf.size()=2 I will write correct buf_cnt Bytes. Is it good idea to reuse memory which is already allocated?

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

                            @DungeonLords
                            So you (a) maintain your own count in addition to the (different) value in the buffer's count and (b) must therefore only ever use write() on the buffer with your own intended count. That seems strange and unnecessary. I would rather keep the buffer's count correct.

                            1 Reply Last reply
                            1
                            • D DungeonLords has marked this topic as solved on

                            • Login

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