在回答对我的另一种答案评论在这里,我找到了我想可能是在C标准(孔C1X,我没有检查的早期的,是的,我知道这是令人难以置信的可能性不大,我独自所有中行星的居民在标准中发现了一个错误).信息如下:
"The sizeof operator yields the size (in bytes) of its operand"."When applied to an operand that has type char, unsigned char, or signed char, (or a qualified version thereof) the result is 1".void *malloc(size_t sz)但所有内容都是"The malloc function allocates space for an object whose size is specified by size and whose value is indeterminate".它根本没有提到用于论证的单位.CHAR_BIT因此字符长度可以超过一个字节.我的问题很简单:
在char为16位宽的环境中,将malloc(10 * sizeof(char))分配10个字符(20个字节)或10个字节?上面的第1点似乎表示前者,第2点表示后者.
任何比我更有C-standard-fu的人都有这个答案?