作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 QDataStream
将数据(使用流的 <<
运算符)序列化为 QByteArray
.但我一直在想:从底层读取是否安全 QByteArray
在 <<
之间操作(或者我可能会遇到缓冲问题)?
quint8 sumBytes(const QByteArray& byteArray)
{
//This will overflow quickly...
quint8 result = 0;
for (int i = 0; i < byteArray.size(); i++)
result += static_cast<quint8>(byteArray[i]);
return result;
}
QByteArrary createByteArray(qint32 foo, qint32 bar)
{
QByteArray array;
QDataStream stream(&array, QIODevice::ReadWrite);
stream<< foo;
stream<< bar;
// Safe to access array below?
const quint8 byteSum = sumBytes(array);
stream<< byteSum;
return array;
}
我已经检查了 QDataStream
的文档和 QByteArray
但没有看到任何相关内容,除了提到 QDataStream
使用 QBuffer
作为QByteArray
不是 QIoDevice
(这让我问了这个问题)。再一次,QDataStream
不提供 flush()
(或类似的方法),所以它可能是安全的......?
最佳答案
当然。请记住,您已将 QByteArray
的 const
版本传递给 sumBytes
,这保证了对象的内部状态不会被改变。
您也可以考虑使用 .at()
:
at()
can be faster thanoperator[]()
, because it never causes a deep copy to occur.
关于c++ - QByteArray : Is it safe to read from the array ('[ ]' ) between writes on the DataStream ('<<' )? 上的 QDataStream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56165990/
我是一名优秀的程序员,十分优秀!