473,386 Members | 1,602 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Qry strlen ?

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

Oct 9 '06 #1
9 1934
In article <11*********************@m7g2000cwm.googlegroups.c om>,
<ra************@yahoo.co.inwrote:
>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
Oct 9 '06 #2
ra************@yahoo.co.in wrote:
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
Oct 9 '06 #3
ra************@yahoo.co.in wrote:
>
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);
}

--
pete
Oct 9 '06 #4
pete said:
ra************@yahoo.co.in wrote:
>>
strlen(buff);

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

if (memchr(buff, '\0', buff_size) != NULL) {
length = strlen(buff);
Why count twice?

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

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Oct 9 '06 #5
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

Igmar Palsenberg wrote:
ra************@yahoo.co.in wrote:
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
Oct 9 '06 #6

ra************@yahoo.co.in wrote:
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
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
Oct 9 '06 #7
On Mon, 09 Oct 2006 11:00:12 +0000, Richard Heathfield wrote:
if((p = memchr(buff, '\0', buff_size)) != NULL) {
length = p - buff;
}
else
length = buff_size;

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

--
James Antill -- ja***@and.org
http://www.and.org/and-httpd

Oct 9 '06 #8
ra************@yahoo.co.in wrote:
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".

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Oct 9 '06 #9
raxitsheth2...@yahoo.co.in wrote:
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',
Undefined behaviour.

On Oct 9, 10:05 pm, "Ancient_Hacker" <g...@comcast.netwrote:
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.

--
Peter

Oct 10 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: sugaray | last post by:
hi, it just came up my mind that since we can get the length of any given string literal S with 'sizeof S-1', so, what's the merit of library function strlen()'s existence ? thanx in advance for...
81
by: Matt | last post by:
I have 2 questions: 1. strlen returns an unsigned (size_t) quantity. Why is an unsigned value more approprate than a signed value? Why is unsighned value less appropriate? 2. Would there...
33
by: apropo | last post by:
what is wrong with this code? someone told me there is a BAD practice with that strlen in the for loop, but i don't get it exactly. Could anyone explain me in plain english,please? char...
66
by: roy | last post by:
Hi, I was wondering how strlen is implemented. What if the input string doesn't have a null terminator, namely the '\0'? Thanks a lot Roy
83
by: rahul8143 | last post by:
hello, what is difference between sizeof("abcd") and strlen("abcd")? why both functions gives different output when applied to same string "abcd". I tried following example for that. #include...
10
by: danielesalatti | last post by:
Hello!! I'm studying c++ and I'm trying to get a little piece of code working, but I'm getting a segfault with strlen here: void tabhash::set (url *U) { uint hash = U->hashCode(); char* url =...
2
by: Chris | last post by:
Hi there, I have been reading in this group for a while really enjoy this pool of infinite c wisdom. :) Anyway, I have question now. I have this little piece of code that is giving me a...
53
by: ¬a\\/b | last post by:
strlen is wrong because can not report if there is some error e.g. char *a; and "a" point to an array of size=size_t max that has no 0 in it
11
by: Bill Cunningham | last post by:
Strncat is supposed to be better than strcat for some reason I've read. Is this because of a potential buffer overflow? I have compiled properly and used strlen too and I just wonder what is the...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.