Friday, May 06, 2005

8. So how do I create a heap based descriptor? And how do I use it when I've got it?

Create an HBufC (or an HBufC8 if you explicitly need 8-bit text).

HBufC* heapbasedFred=HBufC::NewL(4);

But how do I then write to a heap descriptor? HBufC is not derived from a modifiable descriptor class?

This is also easy. First you must call HBufC::Des() to get a TPtr, which is modifiable. You can then use that:

TPtr fredPtr(heapbasedFred->Des());
_LIT(KFred, “fred”);
fredPtr.Copy(KFred);

And how do I read from HBufC?
You can just dereference the HBufC pointer, like this:

TBuf<4> stackbasedFred(*heapBasedFred);

But I’ve seen code which calls Des() on an HBufC. Why’s that?

This is a common mistake, which fortunately does little harm except to overall efficiency.
Des() gives you a modifiable descriptor, TDes, which itself derives from TDesC, so you can use it to call any of the TDesC functions. But it’s unnecessary.

Here’s an example of this common mistake:

TBuf<4> stackbasedFred(heapBasedFred->Des()); // Unnecessary, just use
TBuf<4> stackBasedFred(*heapBasedFred); // More efficient


Comments:
moments ago i was trapped on same space ;) this blog is really good . Thumbs Up man .

http://dewaj.com
 
Hi,
Can you clarify me about basics of descriptors.
We have a piece of code like

TBuf<256> buf(_L("hello"));
TPtr ptr1(buf.des());
Where here ptr1 points to the contents of hello, which is valid.

The above code can i rewrite like
TBuf<256> buf(_L("hello"));
TPtr ptr1;
ptr1 = buf.des();

is the above piece of code correct?

If not can u explain the difference.
It seems to be a lack of basics about descriptors.:-(

--Thanks
Guru.
 
Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?

Google
WWW Descriptors FAQ