Qry strlen ?

R

raxitsheth2000

Hi,

we have strNcpy, strNcmp etc, (small n obviously), but why not in
strlen ?

the question is i am having some buffer

strlen(buff);

what happen if buff is not containing '\0', (i have try some prog. it
will crashing/printing junk chars, using valgrind its showing invalid
mem read etc..) so what i think is i made something simillar to above
N-versioned of strlen which will check for strlen of buff for some MAX
char, and return appropriate result.

I want to ask that any simillar implementation already available ?
C-Std says anything about this ?



--raxit sheth
 
R

Richard Tobin

we have strNcpy, strNcmp etc, (small n obviously), but why not in
strlen ?

Presumably because the case of having a buffer of known size,
containing a string, but not knowing whether it is null-terminated, is
rare.

In any case, you can use memchr() for this.

-- Richard
 
I

Igmar Palsenberg

Hi,

we have strNcpy, strNcmp etc, (small n obviously), but why not in
strlen ?

Most platforms do. Most BSD variants have a strnlen(), and so does this
Linux system.




Igmar
 
P

pete

Hi,

we have strNcpy, strNcmp etc, (small n obviously), but why not in
strlen ?

the question is i am having some buffer

strlen(buff);

what happen if buff is not containing '\0',

if (memchr(buff, '\0', buff_size) != NULL) {
length = strlen(buff);
}
 
R

raxitsheth2000

Thanks,

but when i do men strnlen it shows its gnu extn,... i am curious to
know any reason not included in std. (posix/bsd),

any way i found the solution how to handle this

--raxit
 
R

Richard Heathfield

pete said:
if (memchr(buff, '\0', buff_size) != NULL) {
length = strlen(buff);

Why count twice?

if((p = memchr(buff, '\0', buff_size)) != NULL) {
length = p - buff;
}
 
A

Ancient_Hacker

Hi,

we have strNcpy, strNcmp etc, (small n obviously), but why not in
strlen ?

the question is i am having some buffer

strlen(buff);

what happen if buff is not containing '\0',


One obvious answer is to not create bad strings to begin with!

But if you're handed an unknown string, something like strNlen *might*
be useful, *if* you have some idea what the legal upper bound for that
string might be.

But since C doesnt have any standard way to pass a string length along
with the string, you usually don't know what the upper bound might be,
which makes strnlen() much less useful.

Hint from Heloise: sprinkle your code with randomly named variables
like: char foo999 = '\0'; and periodically calloc() small blocks of
zeroes. :)













(i have try some prog. it
 
J

James Antill

if((p = memchr(buff, '\0', buff_size)) != NULL) {
length = p - buff;
}
else
length = buff_size;

....might as well have the entire strnlen function :).
 
J

Joe Wright

Hi,

we have strNcpy, strNcmp etc, (small n obviously), but why not in
strlen ?

the question is i am having some buffer

strlen(buff);

what happen if buff is not containing '\0', (i have try some prog. it
will crashing/printing junk chars, using valgrind its showing invalid
mem read etc..) so what i think is i made something simillar to above
N-versioned of strlen which will check for strlen of buff for some MAX
char, and return appropriate result.

I want to ask that any simillar implementation already available ?
C-Std says anything about this ?

You must know, somehow, that buff is a pointer to a string. If you don't
know that for a certainty, call strlen(buff);, and buff does not point
to a string, all bets are off. You have "undefined behavior".
 
P

Peter Nilsson

Undefined behaviour.

One obvious answer is to not create bad strings to begin with!

But if you're handed an unknown string, something like strNlen *might*
be useful, *if* you have some idea what the legal upper bound for that
string might be.

But since C doesnt have any standard way to pass a string length along
with the string, you usually don't know what the upper bound might be,
which makes strnlen() much less useful.

The strncpy and strncmp functions exists because in days of old,
fixed (known) width char fields where very common in database
designs. Rather than losing one character by always requiring a
null byte, the full field width is used when the field has the maximum
number of characters.

In most of those cases the field contents would be copied with
strncpy or anaylised with strncmp, so there probably wasn't
much actual need for strnlen, but it is a legitimate question
to wonder why strnlen wasn't included in the standard for
completeness.

Having said that, such a question is more for comp.std.c.
 

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,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top