会`char`永远永远永远有8位? [英] Will a `char` always-always-always have 8 bits?

查看:106
本文介绍了会`char`永远永远永远有8位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直认为:


  1. 一个字符重新$ P $由一个字节psented,

  2. 一个字节总是在计数有8位,

  3. 的sizeof(char)的总是 1

  4. 和内存的最大理论量我可以分配(计入字符 s)是RAM(+交换空间)的字节数。

但现在,我读过关于我不那么肯定了该字节的维基百科条目。

我的哪个假设之一(s)是错的?哪一个(s)是危险的?


解决方案

  1. 字符字节是pretty大同小异。字节是内存的最小可寻址量,所以是一个字符在C 字符总是有大小1。

    从规范,部分的 3.6个字节


      

    字节


      
      

    数据存储足以容纳基本字符集执行环境

    中的任何成员的可寻址单元

    和部分的 3.7.1字符


      

    字符


      
      

    单字节字符结果
      &所述c取代;位重新presentation,适合在一个字节



  2. A 字符 CHAR_BIT 位。它可以是任何数字(当然,根据该规范8或更大),但绝对是最常见8.有与16位和32位实机字符类型,虽然。 CHAR_BIT limits.h中定义。

    从规范,部分整数类型5.2.4.2.1尺寸<&limits.h中GT;


      

    下面给出的值由适用于 #如果 preprocessing指令使用的常量前pressions所取代。此外,除了 CHAR_BIT MB_LEN_MAX ,以下应当由具有相同类型的前pressions更换将一个前pression是根据整数促销换算相应类型的对象。其实现定义值应在幅度(绝对值)等于或大于那些所示,用相同的符号


      
      

    - 为最小目标位的数字,它是不是一个位字段(字节)结果
          <$c$c>CHAR_BIT                               <$c$c>8



  3. 的sizeof(char)的== 1 。总是。

    从规范,部分的 6.5.3.4的的sizeof 运营商后,第3段:


      

    当应用到具有类型的操作数的 字符 unsigned char型 符号字符 (或其合格的版本)的结果为1。



  4. 您可以分配尽可能多的内存系统将让你分配 - 没有什么在定义多少,可能是标准。你能想象,例如,具有云存储支持的内存分配系统的计算机 - 你分配的存储器可能在实际上无限的。

    下面是完整的规范部分的 7.20.3.3的的malloc 函数


      

    简介


      
      

    1 的#include&LT;&stdlib.h中GT; 结果
      &NBSP;&NBSP;&NBSP; 的void * malloc的(为size_t大小);​​


      
      

    说明


      
      

    2 的malloc 函数的对象,其大小由 尺寸指定分配空间 ,其值是不确定的。


      
      

    返回


      
      

    3 的malloc 函数返回一个空指针或指向分配的空间。


    这是该规范的全部,所以真的有没有可以依靠的任何限制。


I've always assumed:

  1. that a char is represented by a byte,
  2. that a byte can always be counted upon to have 8 bits,
  3. that sizeof (char) is always 1,
  4. and that the maximum theoretical amount of memory I can allocate (counted in chars) is the number of bytes of RAM (+ swap space).

But now that I've read the Wikipedia entry on the byte I'm not so sure anymore.

Which one(s) of my assumptions is wrong? Which one(s) is dangerous?

解决方案

  1. Yes, char and byte are pretty much the same. A byte is the smallest addressable amount of memory, and so is a char in C. char always has size 1.

    From the spec, section 3.6 byte:

    byte

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

    And section 3.7.1 character:

    character

    single-byte character
    <C> bit representation that fits in a byte

  2. A char has CHAR_BIT bits. It could be any number (well, 8 or greater according to the spec), but is definitely most often 8. There are real machines with 16- and 32-bit char types, though. CHAR_BIT is defined in limits.h.

    From the spec, section 5.2.4.2.1 Sizes of integer types <limits.h>:

    The values given below shall be replaced by constant expressions suitable for use in #if preprocessing directives. Moreover, except for CHAR_BIT and MB_LEN_MAX, the following shall be replaced by expressions that have the same type as would an expression that is an object of the corresponding type converted according to the integer promotions. Their implementation-defined values shall be equal or greater in magnitude (absolute value) to those shown, with the same sign.

    — number of bits for smallest object that is not a bit-field (byte)
        CHAR_BIT                               8

  3. sizeof(char) == 1. Always.

    From the spec, section 6.5.3.4 The sizeof operator, paragraph 3:

    When applied to an operand that has type char, unsigned char, or signed char, (or a qualified version thereof) the result is 1.

  4. You can allocate as much memory as your system will let you allocate - there's nothing in the standard that defines how much that might be. You could imagine, for example, a computer with a cloud-storage backed memory allocation system - your allocatable memory might be practically infinite.

    Here's the complete spec section 7.20.3.3 The malloc function:

    Synopsis

    1 #include <stdlib.h>
       void *malloc(size_t size);

    Description

    2 The malloc function allocates space for an object whose size is specified by size and whose value is indeterminate.

    Returns

    3 The malloc function returns either a null pointer or a pointer to the allocated space.

    That's the entirety of the specification, so there's not really any limit you can rely on.

这篇关于会`char`永远永远永远有8位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆