深入理解计算机系统P36 练习题2.15 只使用位级和逻辑运算,编写一个C表达式,他等价于x==y。换句话说,当x和y相等时他将返回1,否则就返回0. 题目考察的是位级运算符和逻辑运算符的用法,以下是两种实现方式,供参考: #include void main(){ int x = 0x1101, y = 0x1100; printf(“%dn”, 0x01 ^ x ^ y || 0); printf(“%dn”, !(x ^ y)); x = 0x1101; y = 0x1101; printf(“%dn”, 0x01 ^ x ^ y || 0); printf(“%dn”, !(x ^ y)); }
阅读全文

undefined symbol: itoa

itoa不是c的一个标准输入输出,所以可能不在stdlib.h里。 解决办法很简单,不就是要把整型转为字符串么。sprintf 格式化输出一下就好了。 举个例子: int val = 177; char *val_s ; spprintf(&val_s, 0, “%d”, val);
阅读全文

尝试用libmemcached的类库链接memcache的时候,报错:sasl/sasl.h: No such file or directory。

经查发现是没有安装sasl的开发库,所以需要安装下,stackoverflow上说安装一个libsasl2-dev,用yum没找到,然后使用sasl和dev关键字筛选了下,得到了一个libgsasl-devel,然后就装上试了试,就ok了。

sasl/sasl.h: No such file or directory

sasl/sasl.h: No such file or directory

install libgsasl-devel

install libgsasl-devel


阅读全文

自己在尝试写一个简单的hashTable的时候,报错: hashTable.h: In function ‘hash_resize’: hashTable.h:40: 警告:隐式声明与内建函数 ‘calloc’ 不兼容 hashTable.h: In function ‘hash_init’: hashTable.h:65: 警告:隐式声明与内建函数 ‘calloc’ 不兼容 解决办法就是需要包含一个头文件: #include <stdlib.h>
阅读全文

结构体-struct是把若干个数据类型组合到一起,相当于形成一个结构类型,这样的结构体变量就拥有结构体所包含的所有的数据类型。

结构体所占用的内存单元是它里面包含数据类型所占内存单元的总和。

联合体-union是为了节省内存而设置的,若干个不同数据类型共用一组内存单元。

联合体所占的内存单元是它所包含的数据类型中占用内存单元最大的那个数据类型所占用的内存单元的值。

  union   test1
  {   
          long   a;   
          char   b;   
  }   
  sizeof(test1)==sizeof(long)   
    
  struct  test2
  {   
          long   a;   
          char   b;   
  }   
  sizeof(test2)==sizeof(long)+sizeof(char)

struct和union都是由多个不同的数据类型成员组成, 但在任何同一时刻, union中只存放了一个被选中的成员, 而struct的所有成员都存在。

对于union的不同成员赋值, 将会对其它成员重写, 原来成员的值就不存在了, 而对于struct的不同成员赋值是互不影响的。


阅读全文

溢出 C语言

在C 语言中, 若初值上溢, 则编译通不过; 若运行动态上溢, 则运行出错; 若下溢, 则值按0 处理。对于无符号整数, 其值若超过最大无符号数, 则取其最大无符号数加1 的模; 对于有符号整数, 最大正整数的值加1 后变成最大的负整数值, 最大的负整数值减1 后变成最大的正整数值。
阅读全文

APR_DECLARE_NONSTD(unsigned int) apr_hashfunc_default(const char *char_key,
                                                      apr_ssize_t *klen)
{
    unsigned int hash = 0;
    const unsigned char *key = (const unsigned char *)char_key;
    const unsigned char *p;
    apr_ssize_t i;

    /*
     * This is the popular `times 33' hash algorithm which is used by
     * perl and also appears in Berkeley DB. This is one of the best
     * known hash functions for strings because it is both computed
     * very fast and distributes very well.
     *
     * The originator may be Dan Bernstein but the code in Berkeley DB
     * cites Chris Torek as the source. The best citation I have found
     * is "Chris Torek, Hash function for text in C, Usenet message
     * <27038@mimsy.umd.edu> in comp.lang.c , October, 1990." in Rich
     * Salz's USENIX 1992 paper about INN which can be found at
     * .
     *
     * The magic of number 33, i.e. why it works better than many other
     * constants, prime or not, has never been adequately explained by
     * anyone. So I try an explanation: if one experimentally tests all
     * multipliers between 1 and 256 (as I did while writing a low-level
     * data structure library some time ago) one detects that even
     * numbers are not useable at all. The remaining 128 odd numbers
     * (except for the number 1) work more or less all equally well.
     * They all distribute in an acceptable way and this way fill a hash
     * table with an average percent of approx. 86%.
     *
     * If one compares the chi^2 values of the variants (see
     * Bob Jenkins ``Hashing Frequently Asked Questions'' at
     * http://burtleburtle.net/bob/hash/hashfaq.html for a description
     * of chi^2), the number 33 not even has the best value. But the
     * number 33 and a few other equally good numbers like 17, 31, 63,
     * 127 and 129 have nevertheless a great advantage to the remaining
     * numbers in the large set of possible multipliers: their multiply
     * operation can be replaced by a faster operation based on just one
     * shift plus either a single addition or subtraction operation. And
     * because a hash function has to both distribute good _and_ has to
     * be very fast to compute, those few numbers should be preferred.
     *
     *                  -- Ralf S. Engelschall 
     */

    if (*klen == APR_HASH_KEY_STRING) {
        for (p = key; *p; p++) {
            hash = hash * 33 + *p;
        }
        *klen = p - key;
    }
    else {
        for (p = key, i = *klen; i; i--, p++) {
            hash = hash * 33 + *p;
        }
    }
    return hash;
}

阅读全文

作者的图片

DigDeeply

Technology Stack: PHP/Openresty/GoLang, and so on…

Web Develop Eneigneer

Beijing China