On Tue, 6 May 2008 14:20:34 -0300 "César Leonardo Blum Silveira" <[EMAIL PROTECTED]> wrote:
> On Mon, May 5, 2008 at 7:11 PM, ed <[EMAIL PROTECTED]> wrote: > > > > 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. > > > > Thanks. The thing is that I didn't know whether DBMs "expected" > strings or could store any kind of data. Now it makes more sense to > add the +1 after strlen Depends on the DB interface. If the interface is using something like predefined queries and you're giving it some binary data in a parameter then yes, you probably have to give it the size. If on the other hand the function uses string data then you will just pass a NULL terminated string. That means that the first occurrence of the NULL pointer will be considered as the end of the string. Depends on the API - which you should read! -- The T1 to Dantooine is doing the twist because of a mistake. The Sys Admin is playing half-life. :: http://www.s5h.net/ :: http://www.s5h.net/gpg.html
signature.asc
Description: PGP signature
