Chinaunix首页 | 论坛 | 博客
  • 博客访问: 171758
  • 博文数量: 37
  • 博客积分: 1690
  • 博客等级: 上尉
  • 技术积分: 468
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-13 21:30
文章分类

全部博文(37)

文章存档

2011年(19)

2010年(18)

我的朋友

分类: C/C++

2010-10-21 16:27:37

Qt的参考文档中关于这两个函数的说明如下()

 & 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 () and ().

 & 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 (), QIODevice::readBlock(), and ().

两个函数的区别:

     readBytes函数的形参不必自己申请空间,函数内部会new出一个空间(注意形参是一个指针的引用),并且这个函数我估计会读出流的所有,并把读出的字符长度赋给l。

而readRawBytes函数则需要先申请一段空间,并指定好读出的字符长度len。

ps:之前误认为用readBytes函数要先自己申请一段空间给s,每次readBytes前我都申请一段空间,程序跑起来内存升的厉害,终于发现了这个问题。

阅读(13048) | 评论(2) | 转发(0) |
给主人留下些什么吧!~~

Lynchrs2019-06-20 17:00:53

readBytes函数的形参不必自己申请空间,函数内部会new出一个空间
最重要的是函数内部new出来的空间在使用完成后一定要释放掉,不然就会内存泄漏哦;delete[] xxx;

chinaunix网友2010-10-22 11:13:14

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com