On Mon, 5 May 2008 18:04:15 -0300
"César Leonardo Blum Silveira" <[EMAIL PROTECTED]> wrote:

> Hello,
> 
> What is the correct way to fill the dsize field in apr_datum_t? I am
> not sure whether I should do it like this
> 
> apr_datum_t d;
> 
> d.dptr = "hello, world";
> d.dsize = strlen(d.dptr);
> 
> or like this
> 
> apr_datum_t d;
> 
> d.dptr = "hello, world";
> d.dsize = strlen(d.dptr) + 1;
> 
> Could anyone tell me whether the +1 in the second case is really
> necessary?
> 
> Thanks,

"hello" =

 +---+---+---+---+---+---+
 | h | e | l | l | o | 0 |
 +---+---+---+---+---+---+

That 0 at the end is the null terminator. It is automatically included
in the string literal "hello, world". Although you did not explicitly
put it there it will still be required during string operations
otherwise you will enter overflow.

If on the other hand you're just using it as a raw memory (for image
data etc) then it's not required, but you're unlikely to use strncpy on
this sort of data.

strlen will return 5 when you give it the string "hello", but if you
want to put "hello" into some memory then you better have 6 bytes of
memory ready.


-- 
The DS3 to the Xbox is toast because of All your base are belong to us.
Earthlink is being bought by AOL.
:: http://www.s5h.net/ :: http://www.s5h.net/gpg.html

Attachment: signature.asc
Description: PGP signature

Reply via email to