Saturday, May 28, 2005

23. Why is the _L literal descriptor macro deprecated?

Why is the original _L macro now deprecated in all but test code? These have the advantage of being used in place rather than declared separately, for example:

User::Panic(_L("TEST SERVER"), KErrNotOVerflow);

You will still see them used in test code since they saves typing and thinking up a name for each literal. It's OK to do so where memory use is not critical, but you should avoid using them in production code. Why?

Well, the text, for example "TEST SERVER" is built into ROM as a basic, NULL-terminated C style string, with no initial length member. Because the layout of the stored literal is not like that of a descriptor, it means that when each instance of the literal is used, a temporary TPtrC must be constructed around the data in ROM. The construction of the temporary, which requires 4 bytes of memory for the length/type data, and instructions to set the pointer, the length and the descriptor type, is an unnecessary overhead when compared to _LIT descriptors which can be used directly because they are stored in ROM to look like descriptors.

The generation of each temporary results in inline constructor code which may bloat binaries where many string literals are used. Furthermore, the use of a run-time temporary is safe as long as it is used only during the lifetime of the function in which it is created

So, in summary, you should use _LIT to _L for your literal descriptors, because _L has an overhead associated with constructing a run-time temporary TPtrC and _LIT does not.

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

WWW Descriptors FAQ