How to use the heap descriptor — HBufC

原创 2007年10月09日 11:38:00

下面的内容摘自Symbian S60 3rd Edition SDK,先把英文原版贴在这里,等时间充足的时候我再把它翻译出来。How to use the heap descriptor — HBufC《如何使用堆描述符HBufC

How to use the heap descriptor — HBufC

Heap descriptors provide a buffer of fixed length, allocated on the heap. They are useful for holding constant strings or data, when the length of the data may not be known until run time.

Some key points about heap descriptors:

  • For text data, it is usual to construct a HBufC type and allow the appropriate variant, either a HBufC8 or a HBufC16 to be selected at build time.

  • For binary data, an explicit HBufC8 is used.

  • It is rare to use an explicit HBufC16.

  • Data cannot be changed through this descriptor although it can be replaced using the assignment operators.

  • If you need to pass an HBufC to a function that takes a TDesC& parameter, simply dereference the HBufC pointer.

  • If you need to modify the HBufC's data, use its Des() function to construct a TPtr/TPtr8/TPtr16 modifiable pointer descriptor for the buffer's data.

  • The size of the heap descriptor buffer can be replaced by reallocating it with a new length.

Although the following notes refer to the build independent types, they are equally valid for the 8 bit and 16 bit types.

Constructing an HBufC

A heap descriptor can be constructed in one of two ways:

  • using the static member functions New(), NewL() or NewLC()

  • using the Alloc(), AllocL() or AllocLC() functions of an existing descriptor

The following code fragment constructs a heap descriptor which can hold up to 15 data items. The current length is zero.

HBufC* buf;
buf = HBufC::NewL(15);

The following code fragment constructs a heap descriptor from an existing descriptor. The new heap descriptor is initialised with the content of buf1, i.e. the string: "Hello World!"

The source descriptor is a literal which is converted to descriptor type.

_LIT(KText,"Hello World!");
TBufC<16> buf1(KText);
HBufC* hptr;
hptr = buf1.AllocL();

Replacing data and re-allocating

Although existing data within a heap descriptor cannot be modified, the assignment operator can be used to replace that data.

_LIT(KText,"Hello World!");
HBufC* buf;
buf = HBufC::NewL(15);
*buf = KText;

The source descriptor is a literal which is converted to descriptor type.

To allow more than 15 characters or data items to be assigned into the heap descriptor, it must be reallocated:

buf = buf->ReAllocL(20);

This permits the following assignment to be done without raising a panic:

_LIT(KNewText,"Hello World! Morning");
*buf = KNewText;

buf may or may not point to a different location in the heap after reallocation. The location of the reallocated descriptor depends on the heap fragmentation and the size of the new cell. It is always safer to assume that the location changes.

Changing data through a modifiable pointer descriptor.

The data contained by a heap descriptor can be changed by constructing a TPtr modifiable pointer descriptor using the Des() member function and then changing the data through that TPtr.

The maximum length of the TPtr is determined from the size of the cell allocated to the data area of the heap descriptor.

The following code fragment changes the data in the heap descriptor and the length of the heap descriptor.

TPtr ptr = buf->Des();
ptr.Append(_LIT(" & Hi"));

Take particular care if a the heap descriptor is re-allocated after the TPtr has been constructed. A TPtr created before re-allocating the heap descriptor is not guaranteed to have a valid pointer after re-allocation. Any attempt to modify data through the original TPtr after re-allocation may have undefined consequences.

Note that it is a common mistake to use Des() to create a TDesC& reference. While not incorrect, it is simpler and much more efficient to simply dereference the heap descriptor.



转自: 一、什么是CocoaPods CocoaPods是iOS项目的依赖管理工具,该项目源码在Githu...
  • gf771115
  • gf771115
  • 2016年01月19日 09:30
  • 15826


发件人 Apple 1.0 二进制文件被拒绝 2016年3月24日 2.16 Details Your app declares support for audio in the UIBackgro...
  • u012576807
  • u012576807
  • 2016年03月24日 12:45
  • 13913


Week 5 > Table startsWith endsWith Exercises > ProblemsQ1: Write code to print all the rows where th...
  • Herbe_chanceux
  • Herbe_chanceux
  • 2017年04月03日 16:17
  • 2085

Use MFC in a Static Library,This may be due to a corruption of the heap....

工程选择Use MFC in a Static Library 模式,编译出的程序较大,发布时可以不用带
  • gllg1314
  • gllg1314
  • 2014年09月30日 10:17
  • 745

fatal error C1076: compiler limit : internal heap limit reached; use /Zm to specify a higher limit

fatal error C1076: compiler limit : internal heap limit reached; use /Zm to specify a higher limit ...
  • Augusdi
  • Augusdi
  • 2011年09月16日 21:22
  • 3278

How Linkers Use Static Libraries to Resolve References

While static libraries are useful and essential tools, they are also a source of confusion to progra...
  • zhangyubingcatherine
  • zhangyubingcatherine
  • 2013年12月06日 14:15
  • 442

elasticsearch install and how to use javaapi

原文链接: Elasticsearch 基本安装 标签(空格分隔): Elasti...
  • luckgl
  • luckgl
  • 2016年11月18日 18:48
  • 411

How to use jquery template js

/*****by Jiangong SUN*****/ Jquery template can help generate code in the same format. It's lik...
  • garcon1986
  • garcon1986
  • 2012年04月23日 15:59
  • 3680

Java ThreadLocal - How to use and code sample

Source: Thread Loc...
  • j357777842
  • j357777842
  • 2011年04月01日 05:55
  • 584

How to Use HTML5 FUll Screen API(如何使用HTML5全屏接口)

此API可以使单个元素全屏显示。与按下F11键强制浏览器全屏不同,此API的目标是运行在一个容器中的图片,视频和游戏。当进入全屏模式时,将会出现一条信息通知用户可在任何时候按ESC键而返回页面。 ...
  • xiebaochun
  • xiebaochun
  • 2014年10月31日 11:39
  • 6461
您举报文章:How to use the heap descriptor — HBufC