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.

strlen() doesn't include the trailing NUL. In memory, the result of these operations looks<br />

like Figure 5.3:<br />

Figure 5.3<br />

The issue here is that had I got my array size wrong (for helloworld-stack) or my heap<br />

cell size wrong (for helloworldheap), I might have overwritten the end of the string. It's<br />

not simply the irritation of having to add or subtract 1 for the trailing NUL – the real problem<br />

is that the string data might not fit into the memory allocated for it.<br />

5.2.2 Modifying Symbian OS Strings<br />

The strings example shows how to modify strings using descriptors. You can get a buffer<br />

suitable for appending one string to another by placing a TBuf on the stack:<br />

_LIT(KWorldRom, "world!");<br />

TBuf helloWorldStack(KHelloRom);<br />

helloWorldStack.Append(KWorldRom);<br />

TBuf is a modifiable buffer with a maximum length of 12 characters. After the<br />

constructor has completed, the data is initialized to 'hello' and the current length is set to 5.<br />

The code is somewhat unsatisfactory in that I have used a magic number, 12, for the size of<br />

the buffer rather than calculating it. This is because you can't take the size of a _LIT<br />

constant. I could have avoided magic numbers, but it didn't seem worth it for this example.<br />

Append() starts by checking the maximum length to ensure that there will be enough room<br />

for the final string. Then, assuming there is room, it appends the 'world!' string, and adjusts<br />

the current length.<br />

You can see how it looks in memory in Figure 5.4.

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

Saved successfully!

Ooh no, something went wrong!