Symbian Programming Summary-Basic article-Dynamic buffer (1)-Review HBUFC

Source: Internet
Author: User

Dynamic buffers are useful for saving binary data when the data size is not fixed at compile time and may be extended to a large size at run time. We can save binary data using C + + arrays, and then invoke functions like memcpy to dynamically change the size of the space occupied by the array; we can also use the HBUFC descriptor to get its modifiable descriptors to write to it, and then invoke the ReAlloc method to extend the array. The above two points method is feasible, but not good, because we have to manage the allocation of memory ourselves. Symbian C + + takes this into account and introduces the concept of dynamic buffers.

The heap-based buffer descriptor HBUFC prefix h is clearly not compliant with Symbian C + + naming conventions (see Symbian Programming Summary-Basic article-class type). Here, "H" only indicates that the data is stored on the heap (Heap). Although the HBUFC class is "C" suffix, meaning is not modifiable, but we can HBUFC::D es () method to obtain its modifiable tptr pointer, the content of the HBUFC to modify.

Construction of a heap descriptor

Build from stack content of Tdesc class

The Tdesc class has several methods that allow the contents of the stack to be copied into the heap and return a HBUFC pointer with the following function prototypes:

IMPORT_C HBufC16 *Alloc() const;
IMPORT_C HBufC16 *AllocL() const;
IMPORT_C HBufC16 *AllocLC() const;

In particular, if the creation is unsuccessful, ALLOC returns NULL,ALLOCL throws an exception.

The following code illustrates the use of Tdesc::alloc:

1 LOCAL_C void HBufCFromDesLC(HBufC*& aBuf)
2   {
3   _LIT(KText, "Hello World!");
4   aBuf = KText().AllocLC();
5   }
6
7 LOCAL_C void MainL()
8   {
9   HBufC* buf;
10   HBufCFromDesLC(buf);
11   console->Write(buf->Des());
12
13   CleanupStack::PopAndDestroy(buf);
14   }

Build Using the Hbufc::new (L,LC) method

When created by the Hbufc::new (L,LC) method, you need to specify in the parameters the maximum length of the heap space that is created, and throw an exception if the next fill of data is longer than the defined length:

IMPORT_C static HBufC16 *New(TInt aMaxLength);
IMPORT_C static HBufC16 *NewL(TInt aMaxLength);
IMPORT_C static HBufC16 *NewLC(TInt aMaxLength);

Sample code:

1 LOCAL_C void HBufCFromNewLC(HBufC*& aBuf)
2   {
3   aBuf = HBufC::NewLC(19);
4   _LIT(KText, "Hello World My Girl!");
5
6   TPtr ptr = aBuf->Des();
7   ptr.Append(KText);
8   }
9
10 LOCAL_C void MainL()
11   {
12   HBufC* buf;
13   HBufCFromNewLC(buf);
14   console->Write(buf->Des());
15
16   CleanupStack::PopAndDestroy(buf);
17   }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.