21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

safestr_release( ) or safestr_free( ) is called to decrement the string’s reference<br />

count.<br />

People are sometimes confused about when actually to use SAFESTR_TEMP( ), as well<br />

as how to use it properly. Use SAFESTR_TEMP( ) when you need to pass a constant<br />

string as an argument to a function that is expecting a safestr_t. A perfect example<br />

of such a case would be safestr_sprintf( ), which has the following signature:<br />

int safestr_sprintf(safestr_t *output, safestr_t *fmt, ...);<br />

The string that specifies the format must be a safe string, but because you should<br />

always use constant strings for the format specification (see Recipe 3.2), you should<br />

use SAFESTR_TEMP( ). The alternative is to use SAFESTR_CREATE( ) to create the string<br />

before calling safestr_sprintf( ), and free it immediately afterward with safestr_<br />

free( ).<br />

int i = 42;<br />

safestr_t fmt, output;<br />

output = SAFESTR_ALLOC(1);<br />

/* Instead of doing this: */<br />

fmt = SAFESTR_CREATE("The value of i is %d.\n");<br />

safestr_sprintf(&output, fmt, i);<br />

safestr_free(fmt);<br />

/* You can do this: */<br />

safestr_sprintf(&output, SAFESTR_TEMP("The value of i is %d.\n"), i);<br />

When using temporary strings, remember that the temporary string will be destroyed<br />

automatically after a call to any SafeStr API function except safestr_reference( ),<br />

which will increment the string’s reference count. If a temporary string’s reference<br />

count is incremented, the string will then survive any number of API calls until its<br />

reference count is decremented to the extent that it will be destroyed. The API functions<br />

safestr_release( ) and safestr_free( ) may be used interchangeably to decrement<br />

a string’s reference count.<br />

For example, if you are writing a function that accepts a safestr_t as an argument<br />

(which may or may not be passed as a temporary string) and you will be performing<br />

multiple operations on the string, you should increment the string’s reference count<br />

before operating on it, and decrement it again when you are finished. This will<br />

ensure that the string is not prematurely destroyed if a temporary string is passed in<br />

to the function.<br />

void some_function(safestr_t *base, safestr_t extra) {<br />

safestr_reference(extra);<br />

if (safestr_length(*base) + safestr_length(extra) < 17)<br />

safestr_append(base, extra);<br />

safestr_release(extra);<br />

}<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.<br />

Using the SafeStr Library | 87

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

Saved successfully!

Ooh no, something went wrong!