Thursday, May 05, 2005

6. Large stack-based descriptors

The amount of stack space on Symbian OS is pretty limited (the default is 8 KB). So you should avoid creating large stack-based descriptors when it is unnecessary to do so. Symbian OS sometimes makes it easy to transgress this rule, by defining a number of classes and typedefs that can be used inefficiently on the stack. It pays to be aware of the these and only use them when you know the descriptor you are reading will fill the entire space allocated.

For example, TFileName is typedef-ed as follows:

typedef TBuf<KMaxFileName> TFileName;

where

const TInt KMaxFileName=0x100; // = 256 decimal


But, of course, each character in a descriptor is 2 bytes, since Symbian OS is a wide, UNICODE, build (as described in 1. The Basics). So each TFileName object created on the stack occupies 512 bytes (1/16th of the default stack size), regardless of whether the text occupying it is actually that long!

Sure, these objects can be very appealing, because they mean you don't have to worry about buffer overrun. But they come at a price.

For stack conservation, it's advisable to be aware of the amount of space the following objects consume:
  • TFileName 512 bytes
  • TEntry 544 bytes
  • TFullName 512 bytes
  • TName 256 bytes
If you do need to use these objects, it's best to use them on the heap. You can do this simply by making them a member of a heap-based object (ie a C class object) . They don't then need to be created on the heap themselves - just by being a member of the C class, they are automatically heap-based. Alternatively, you can alloc them on the heap using the new operator - but make sure they are leave-safe and destroyed when no longer needed.

Comments:
"For example, TFileName is typedef-ed in e32cmn.h as follows:

typedef TBuf TFileName;"

Jo,

There seems to be a problem with html tags (they're not allowed?): you may use < and > codes instead:

typedef TBuf <KMaxFileName> TFileName;

David.-
 
You're right, I went through and fixed a lot of these at the weekend, but missed this one - thanks for pointing it out. I've fixed it now.

I've also remove text about the files in which the typedef and KMaxFileName are defined, since this seems to differ according to the release in question.).
 
If you want to be exact TFileName is actually 520 bytes. 512 for the buffer and 4 bytes for the type and length plus 4 bytes for the max length.
 
Symbian third party development cost
 
Post a Comment

<< Home

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

Google
WWW Descriptors FAQ