Re: strncmp issue



Hi, Channa--

On Apr 28, 2009, at 2:52 AM, Channa wrote:
I am using the freebsd implementation of strncmp for ARM which is an
assembly implementation.
I have a small doubt, when i tested the strncmp by passing the third argument:
'n' as -1 the return values is '0' instead it should '-1'.
When the third argument to strncmp is as below:

ret = strncmp("a","b",-1)

Thanks for the thought, but strncmp() is defined to take a size_t as the third argument, which is unsigned (ie, uint32_t or uint64_t).

Presumably when you tell it to compare with length of -1, that will be converted to UINT_MAX or ULONG_MAX, and strncmp() will run until it finds a null somewhere in one the strings and then return the comparison result. (Or get a segfault, perhaps, if you run off the end into unallocated address space.)

Regards,
--
-Chuck

_______________________________________________
freebsd-current@xxxxxxxxxxx mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-current
To unsubscribe, send any mail to "freebsd-current-unsubscribe@xxxxxxxxxxx"



Relevant Pages

  • Re: strncmp performance
    ... > I have an application where I use the strncmp function extensively to ... > compare 2 string to see if they are the same or not. ... String comparison can be inherently slow, ... application and ask for advice on algorithms. ...
    (comp.lang.c)
  • strncmp issue
    ... I am using the freebsd implementation of strncmp for ARM which is an ... assembly implementation. ... when i tested the strncmp by passing the third argument: ...
    (freebsd-current)
  • Re: Is strncmp allowed to read past first difference?
    ... that to compare a value in X and a value in Y we must ... And if some words are in non-readable memory, ... (characters that follow a null character are not compared) ... strncmp permission to read as many as 42 bytes from each array. ...
    (comp.std.c)
  • Re: strncmp performance
    ... > I have an application where I use the strncmp function extensively to ... > compare 2 string to see if they are the same or not. ... Also it appears that you are implementing strcmp() ... use of memcmpif you know that the strings are the same length. ...
    (comp.lang.c)
  • Re: strncmp performance
    ... > I have an application where I use the strncmp function extensively to ... > compare 2 string to see if they are the same or not. ... word-comparisons instead of char comparisons can easily give you upto ...
    (comp.lang.c)