03.01.2013 Views

Chapter 1

Chapter 1

Chapter 1

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Chapter</strong> 5: Strings and Descriptors<br />

Overview<br />

I once looked up the word 'computer' in a big dictionary in my high school library. It said, 'one<br />

who computes.' This view is somewhat old- fashioned: the truth is that most programmers<br />

expend far more effort on processing strings than they do computing with numbers, so it's a<br />

good idea to start getting to know Symbian OS APIs by looking at its string handling<br />

facilities.<br />

In C, string processing is inconvenient. You have an awkward choice of char*, char[],<br />

and malloc() with which to contend, just to allocate your strings. You get some help from<br />

such functions as strlen(), strcpy(), and strcat(), but little else. You have to pass<br />

around awkward maximum-length parameters to functions such as strncpy()and<br />

strncat() that modify strings with an explicit length limit. You have to add one and<br />

subtract one for the trailing NULL at the end of every string. If you get your arithmetic slightly<br />

wrong, you overwrite memory and produce bugs that are hard to track down. It's not much<br />

fun.<br />

In Java, life is much easier. There is a String class with nice syntax such as a + operator for<br />

concatenating strings. Memory for strings looks after itself: new memory is allocated for new<br />

strings and memory for old string values or intermediate results is garbage collected when<br />

no longer needed.<br />

In standard C++, a similarly useful string class is also available, though it came along quite a<br />

while after the C++ language itself.<br />

In Symbian OS, strings are implemented by descriptors. Descriptors provide a safe and<br />

consistent mechanism for dealing with both strings and general binary data regardless of the<br />

type of memory in which they reside.<br />

Like the string classes in Java and standard C++, they're much more comfortable to work<br />

with than C strings. However, Symbian OS doesn't take the same approach as either Java<br />

or standard C++, because memory management is so important in Symbian OS. You have<br />

to be fully aware of the memory management issues when you're using descriptors.<br />

I'll start this chapter off with a discussion of descriptors and memory management. Because<br />

C string handling gives you control of memory management, I'll compare descriptors and<br />

their memory management with C strings and their memories.<br />

Then I'll move on to what you can do with descriptors. You need to know about both the<br />

concrete implementation classes and the two key abstract base classes, TDesC and TDes,<br />

which include a large number of convenience functions. TDesC is a one-word class (just the<br />

length), and its convenience functions are all const – that's what the C in TDesCstands for.<br />

TDes derives from TDesC, and adds an extra word (maximum length), and nonconst<br />

convenience functions.<br />

Symbian OS is built to use 'wide' characters – 16 bits, using Unicode. I'll explain some of the<br />

implications of this.<br />

Finally, I'll look at descriptors' role in describing data – which is where they got their name.<br />

Descriptors are fundamental to many data- related Application Programming Interfaces<br />

(APIs), such as the interthread reading and writing used by the client-server framework and<br />

reading and writing data to files.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!