the minimum size when using strncpy(...)

S

Simon

Hi,

I am a bit confused with zero based items and strncpy(...)

assuming I have

char str1[] = "Hello world";
char str2[3];

I now have str2 that should look something like

str[0] == '?';
str[1] == '?';
str[2] == '?';

where '?' is any character that was there b4

and str2 does not have str[3] == '?';

am I right so far?
now can I also say that if I did

int nSize = strlen(str2);
I could get any value really 'cause there is no null char

now if I wanted to create a function to get 2 characters from a string I
could do something like..
the reason why I want a function is because the size(s) might change at a
later stage so I'd rather change one function and all the strings that call
it.
I also want to make sure that the data has a terminating null char and that
the rest of the data is set to null as well.

int MyCopy( char szDest[3], const char*szSource )
{
memset( szDest, 0, 3 );
strcpy( szDest, szSource, 2 );
szDest[2] = '\0';

strlen(szDest );
}

is my logic above correct? or would it be better to define my function as
(but I would I ensure that the size is correct?

int MyCopy( char *szDest, const char*szSource )
{
...
}

many thanks in advance.

regards.
 
I

Ivan Vecerina

Simon said:
int MyCopy( char szDest[3], const char*szSource )
{
memset( szDest, 0, 3 );
strcpy( szDest, szSource, 2 ); ~~~~~~ strncpy
szDest[2] = '\0';

strlen(szDest );
}

is my logic above correct? or would it be better to define my function as
(but I would I ensure that the size is correct?

The function could be simplified to:
int MyCopy( char szDest[3], const char*szSource )
{
strncpy( szDest, szSource, 2 );
szDest[2] = '\0';

return strlen(szDest );
}

And if your library provides as an extension the safer strlcpy
function that was introducted by the OpenBSD team (see
http://www.gsp.com/cgi-bin/man.cgi?section=3&topic=strlcpy ),
you can even write:
int MyCopy( char szDest[3], const char*szSource )
{
strlcpy( szDest, szSource, 3 );
return strlen(szDest);
}
.... and actually get rid of the function altogether.


This said, people in this NG prefer to use std::string ;)

Cheers,
Ivan
 
G

Guest

Simon said:
Hi,

I am a bit confused with zero based items and strncpy(...)

assuming I have

char str1[] = "Hello world";
char str2[3];

I now have str2 that should look something like

str[0] == '?';
str[1] == '?';
str[2] == '?';

where '?' is any character that was there b4

and str2 does not have str[3] == '?';

am I right so far?
now can I also say that if I did

int nSize = strlen(str2);
I could get any value really 'cause there is no null char

I think the situation is 'undefined'.
now if I wanted to create a function to get 2 characters from a string I
could do something like..
the reason why I want a function is because the size(s) might change at a
later stage so I'd rather change one function and all the strings that
call
it.
I also want to make sure that the data has a terminating null char and
that
the rest of the data is set to null as well.

int MyCopy( char szDest[3], const char*szSource )
{
memset( szDest, 0, 3 );
strcpy( szDest, szSource, 2 );
szDest[2] = '\0';

strlen(szDest );
}

My problem with using a function like that are the design ramifications.
Hardcoding those kind of values is silly at best. :p
is my logic above correct? or would it be better to define my function as
(but I would I ensure that the size is correct?

int MyCopy( char *szDest, const char*szSource )
{
...
}

This would be fine assuming you zero terminated each passed string. Or why
not implement a 'limit' copy. Pass a number indicated the size. Or better
yet, use a supplied system function (most of the string and printf functions
have buffer 'safe' counterparts) for the desired results.
 
S

Simon

is my logic above correct? or would it be better to define my function as
(but I would I ensure that the size is correct?

The function could be simplified to:
int MyCopy( char szDest[3], const char*szSource )
{
strncpy( szDest, szSource, 2 );
szDest[2] = '\0';

return strlen(szDest );
}

I understand, but am I right in assuming there is nothing wrong with adding
memset( szDest, 0, 3 ); even if it is a waste of space.
And if your library provides as an extension the safer strlcpy
function that was introducted by the OpenBSD team (see
http://www.gsp.com/cgi-bin/man.cgi?section=3&topic=strlcpy ),
you can even write:

no i do not seem to have it.
This said, people in this NG prefer to use std::string ;)

of course, but I am playing with chars for now :)
Cheers,
Ivan

Simon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top