QBuffer.buffer().clear() doesn't affect to size
-
In my code I add Byte to
QBuffer buf
and then dobuf.buffer().clear()
QDataStream stream(&buf); stream << (quint8)0xAB; buf.buffer().clear();
Looks like
buf.size()
andbuf.buffer().size()
both not changed after clear... Why?P.S. For me also not clear what is diff between
buf.size()
andbuf.buffer().size()
?Test code in my repo.
-
@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 usewrite()
on the buffer with your own intended count. That seems strange and unnecessary. I would rather keep the buffer's count correct. -
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 -
@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.
-
@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 callingbuf.buffer().clear();
in B leaves the current file offset pointer where it is, at 2. For B you need to insertbuf.seek(0L);
either before or after thebuf.buffer().clear();
to achieve the same situation, then it will report1
instead of2
just like A does. -
@JonB if I want to use B) way then may be I no need
buf.buffer().clear()
? Looks like justbuf.seek(0L)
is enough. Isn't it?P.S. I improve my example to make it more clear for others beginners.
-
@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?
-
@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? -
@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?
-
@DungeonLords
As @Christian-Ehrlicher has written, you do need toclear()
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 bybuf.seek(0L)
, so what's the problem? You might argue that theclear()
ought do theseek()
automatically, but apparently it does not so you just need to do it yourself. -
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 filewrite(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?
-
@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?
-
@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 usewrite()
on the buffer with your own intended count. That seems strange and unnecessary. I would rather keep the buffer's count correct. -