Bytewise reading of memory: “signed char *” vs “unsigned char *”

前端 未结 5 1465
一个人的身影
一个人的身影 2020-12-05 08:14

One often needs to read from memory one byte at a time, like in this naive memcpy() implementation:

void *memcpy(void *dest, const void *src, si         


        
5条回答
  •  猫巷女王i
    2020-12-05 08:48

    You should use unsigned char. The C99 standard says that unsigned char is the only type guaranteed to be dense (no padding bits), and also defines that you may copy any object (except bitfields) exactly by copying it into an unsigned char array, which is the object representation in bytes.

    The sensible interepretation of this is to me, that if you use a pointer to access an object as bytes, you should use unsigned char.

    Reference: http://blackshell.com/~msmud/cstd.html#6.2.6.1 (From C1x draft C99)

提交回复
热议问题