关于QDataStream::readBytes和QDataStream::read..
时间:2010-10-21 来源:小步前进
Qt的参考文档中关于这两个函数的说明如下(http://doc.trolltech.com/3.3/qdatastream.html#readBytes)
QDataStream & QDataStream::readBytes ( char *& s, uint & l )
Reads the buffer s from the stream and returns a reference to the stream.
The buffer s is allocated using new. Destroy it with the delete[] operator. If the length is zero or s cannot be allocated, s is set to 0.
The l parameter will be set to the length of the buffer.
The serialization format is a Q_UINT32 length specifier first, then l bytes of data. Note that the data is not encoded.
See also readRawBytes() and writeBytes().
QDataStream & QDataStream::readRawBytes ( char * s, uint len )
Reads len bytes from the stream into s and returns a reference to the stream.
The buffer s must be preallocated. The data is not encoded.
See also readBytes(), QIODevice::readBlock(), and writeRawBytes().
两个函数的区别:
readBytes函数的形参不必自己申请空间,函数内部会new出一个空间(注意形参是一个指针的引用),并且这个函数我估计会读出流的所有,并把读出的字符长度赋给l。
而readRawBytes函数则需要先申请一段空间,并指定好读出的字符长度len。
ps:之前误认为用readBytes函数要先自己申请一段空间给s,每次readBytes前我都申请一段空间,程序跑起来内存升的厉害,终于发现了这个问题。