Potential problem with C standard malloc'ing chars

前端 未结 3 1674
感情败类
感情败类 2020-12-31 20:49

When answering a comment to another answer of mine here, I found what I think may be a hole in the C standard (c1x, I haven\'t checked the earlier ones and yes, I k

3条回答
  •  感动是毒
    2020-12-31 21:04

    In a 16-bit char environment malloc(10 * sizeof(char)) will allocate 10 chars (10 bytes), because if char is 16 bits, then that architecture/implementation defines a byte as 16 bits. A char isn't an octet, it's a byte. On older computers this can be larger than the 8 bit de-facto standard we have today.

    The relevant section from the C standard follows:

    3.6 Terms, definitions and symbols

    byte - addressable unit of data storage large enough to hold any member of the basic character set of the execution environment...

    NOTE 2 - A byte is composed of a contiguous sequence of bits, the number of which is implementation-defined.

提交回复
热议问题