Allocating Strings in libadr
adr_data_t *adr_data_new_string(const char *s, lifetype_t lifetime);adr_data_new_string allocates a new string adr_data_t, initializing it to the NULL-terminated string pointed to by s. If s is NULL, adr_data_new_string will fail.
The value of the lifetime determines how the string s is to be used:
-
LT_COPY -
adr_data_new_string must allocate and make a copy of the string pointed to by
s. This copy will be freed when theadr_data_tis freed. -
LT_CONST -
The string pointed to by
sis a constant that will never be changed or deallocated. Therefore, adr_data_new_string need not copy the string; it can instead refer directly to s indefinitely. This is the recommended lifetime value when passing a string literal to adr_data_new_string. -
LT_FREE -
The string pointed to by
swas dynamically allocated using malloc and is no longer needed by the caller. adr_data_new_string will ensure that the string is eventually freed. It may choose to use the string directly instead of making a copy of it. Obviously, this lifetime value should never be used with string literals.If lifetime is
LT_FREEand adr_data_new_string fails for any reason,swill automatically be freed.
adr_data_t*adr_data_new_fstring (const char*format, ...);
adr_data_new_fstring allocates a new string adr_data_t, initializing it to the string generated by calling sprintf on format and any additional arguments provided.
adr_data_t*adr_data_new_nstring (const char*s, size_t count);
adr_data_new_nstring allocates a new string adr_data_t, initializing it to the first count bytes of s.