相关疑难解决方法(0)

C标准malloc'ing字符的潜在问题

在回答对我的另一种答案评论在这里,我找到了我想可能是在C标准(孔C1X,我没有检查的早期的,是的,我知道这是令人难以置信的可能性不大,我独自所有中行星的居民在标准中发现了一个错误).信息如下:

  1. 第6.5.3.4节("运营商规模")第2段规定"The sizeof operator yields the size (in bytes) of its operand".
  2. 该部分第3段指出:"When applied to an operand that has type char, unsigned char, or signed char, (or a qualified version thereof) the result is 1".
  3. 第7.20.3.3节描述void *malloc(size_t sz)但所有内容都是"The malloc function allocates space for an object whose size is specified by size and whose value is indeterminate".它根本没有提到用于论证的单位.
  4. 附件E开始时8是最小值,CHAR_BIT因此字符长度可以超过一个字节.

我的问题很简单:

在char为16位宽的环境中,将malloc(10 * sizeof(char))分配10个字符(20个字节)或10个字节?上面的第1点似乎表示前者,第2点表示后者.

任何比我更有C-standard-fu的人都有这个答案?

c malloc standards sizeof c11

7
推荐指数
1
解决办法
806
查看次数

标签 统计

c ×1

c11 ×1

malloc ×1

sizeof ×1

standards ×1