zoukankan      html  css  js  c++  java
  • memcmp与strncmp函数【转】

    函数:int memcmp (const void *a1, const void *a2, size_t size) 
           函数memcmp用于比较字符串s1与s2的前size个字符。
          如果两上字符块相同,memcmp将返回0。

    函数:int strcmp (const char *s1, const char *s2) 
            这个函数用来比较s1和s2字符串,这个函数将返回一个值,它的符号与第一对不同的字符的比较结果相关。
           如果两个字符串相等的话,strcmp将返回0。
           如果s1是s2的一个子串的话,s1小于s2
    此外还有函数 
        int strncmp (const char *s1, const char *s2, size_t size) 
        此函数与strcmp极为类似。不同之处是,strncmp函数是指定比较size个字符。也就是说,如果字符串s1与s2的前size个字符相同,函数返回值为0。

    功能比较:

         二者都可以用于字符串的比较,但是二者是有比较大的差异的,因为strcmp是按照字节(byte-wise)比较的,并且比较的过程中会检查是否出现了""结束符,一旦任意一个字符串指针前进过程中遇到结束符,将终止比较。而memcmp函数是用于比较两个内存块的内容是否相等,在用于字符串比较时通常用于测试字符串是否相等,不常进行byte-wise的字符串比较。如果要比较的对象中包含一些由于边界对齐需求而填入结构对象中的空格、联合 (union)结束的额外空格、字符串所分配的空间未使用完的部分引起的“holes”的话,最好使用memcmp来完成。这些“holes”的内容是不确定的,在执行byte-wise比较时结果也是不明确的。

    效率差异:
         strcmp比较的字符串,而memcmp比较的是内存块,strcmp需要时刻检查是否遇到了字符串结束的 字符,而memcmp则完全不用担心这个问题,所以memcmp的效率要高于strcmp

    使用示例:

    给出一个如下的结构定义:
    struct foo
      {
        unsigned char tag;
        union
           {
             double f;
            long i;
              char *p;
           } value;
      };
          如果要比较两个struct foo对象的话,建议最好使用memcmp。
         在给出一个字符串比较的例子,判断字符串str中前四个中字符是否为 0x80100001,因为0x00对于字符串而言,这是个结束符,如果使用strncmp的话strncmp(str,"x80x10x00 x01",4)的话,实际效果是只判断了是否含有0x8010,也就是说一旦str中前两个字符为0x8010就返回0,表示相同了,显然这是不正确的!此时应该使用memcmp(str,"x80x10x00x01",4),这样一来就达到了目的

     附:strcmp,strncmp,memcmp的Linux的源代码

    /**
     * strcmp - Compare two strings
     * @cs: One string
     * @ct: Another string
     */
    int strcmp(const char *cs, const char *ct)
    {
            signed char __res;

            while (1) {
                    if ((__res = *cs - *ct++) != 0 || !*cs++)
                            break;
            }
            return __res;
    }

     /**
     * strncmp - Compare two length-limited strings
     * @cs: One string
     * @ct: Another string
     * @count: The maximum number of bytes to compare
     */
    int strncmp(const char *cs, const char *ct, size_t count)
    {
            signed char __res = 0;

            while (count) {
                    if ((__res = *cs - *ct++) != 0 || !*cs++)                  //比较到结束符,时,已经做了__res = *cs - *ct了,所以不等长度时,肯定返回不为0
                            break;
                    count--;
            }
            return __res;
    }
    /**
     * memcmp - Compare two areas of memory
     * @cs: One area of memory
     * @ct: Another area of memory
     * @count: The size of the area.
     */
    int memcmp(const void *cs, const void *ct, size_t count)
    {
            const unsigned char *su1, *su2;
            int res = 0;
            for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
                    if ((res = *su1 - *su2) != 0)
                            break;
            return res;
    }

    一、memcmp含义

    Compare characters in two buffers.

    int memcmp( const void* buf1, const void* buf2, size_t count );inline int wmemcmp ( const wchar_t* buf1, const wchar_t* buf2, size_tcount);

    Parameters

    buf1 : First buffer.
    buf2 : Second buffer.
    count : Number of characters.
    Return Values : The return value indicates the relationship between the buffers.
    Return ValueRelationship of First count Bytes of buf1 and buf2
    < 0 buf1 less than buf2
    0 buf1 identical to buf2

    > 0

    buf1 greater than buf2

    二、memcmp与strcmp的区别

    int memcmp(const void * cs,const void * ct,size_t count) 
    {

    const unsigned char *su1, *su2; 
    int res = 0; 

    for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--) 
    if ((res = *su1 - *su2) != 0) 
    break; 
    return res; 
     } 

    int strncmp(const char * cs,const char * ct,size_t count) 

    register signed char __res = 0; 
    while (count) { 
    if ((__res = *cs - *ct++) != 0 || !*cs++) 
    break; 
    count--; 

    return __res; 
    }

    1、这两个函数的差别其实还是挺大的,差别在这里: 
    对于memcmp(),如果两个字符串相同而且count大于字符串长度的话,memcmp不会在处停下来,会继续比较后面的内存单元,直到_res不为零或者达到count次数。 
    对于strncmp(),由于((__res = *cs - *ct++) != 0 || !*cs++)的存在,比较必定会在最短的字符串的末尾停下来,即使count还未为零。具体的例子: 
    char a1[]="ABCD"; 
    char a2[]="ABCD"; 
    对于memcmp(a1,a2,10),memcmp在两个字符串的之后继续比较 
    对于strncmp(a1,a2,10),strncmp在两个字符串的末尾停下,不再继续比较。 
    所以,如果想使用memcmp比较字符串,要保证count不能超过最短字符串的长度,否则结果有可能是错误的。

    2、strncmp("abcd", "abcdef", 6) = 0。比较次数是一样的: 
    memcmp:在比较到第5个字符也就是'',*su1 - *su2的结果显然不等于0,所以满足条件跳出循环,不会再进行后面的比较。我想在其他情况下也一样。
    strncmp:同样的道理再比较到第5个字符时结束循环,其实strncmp中“!*cs++”完全等同于“!*ct++”,其作用仅在于当两个字符串相同的情形下,防止多余的比较次数。

  • 相关阅读:
    虚拟机镜像压缩(qcow2,raw)
    CentOS7--删除virbr0
    django简单实例(1)
    python3.7操作mysql数据库
    KVM虚拟化安装部署
    zabbix监控温度及风扇
    excel以某列为基础进行行排序
    kibana添加首页登陆认证
    openstack常见问题解决方法
    linux的7种运行模式
  • 原文地址:https://www.cnblogs.com/sky-heaven/p/4791808.html
Copyright © 2011-2022 走看看