1. <tfoot id='tSiPO'></tfoot>
      2. <i id='tSiPO'><tr id='tSiPO'><dt id='tSiPO'><q id='tSiPO'><span id='tSiPO'><b id='tSiPO'><form id='tSiPO'><ins id='tSiPO'></ins><ul id='tSiPO'></ul><sub id='tSiPO'></sub></form><legend id='tSiPO'></legend><bdo id='tSiPO'><pre id='tSiPO'><center id='tSiPO'></center></pre></bdo></b><th id='tSiPO'></th></span></q></dt></tr></i><div id='tSiPO'><tfoot id='tSiPO'></tfoot><dl id='tSiPO'><fieldset id='tSiPO'></fieldset></dl></div>
      3. <legend id='tSiPO'><style id='tSiPO'><dir id='tSiPO'><q id='tSiPO'></q></dir></style></legend>
          <bdo id='tSiPO'></bdo><ul id='tSiPO'></ul>

        <small id='tSiPO'></small><noframes id='tSiPO'>

        是否可以在 C++ 中创建一种占用少于一个字节内存的类型?

        Is it possible to create a type in c++ that takes less than one byte of memory?(是否可以在 C++ 中创建一种占用少于一个字节内存的类型?)
          <tbody id='vyQDa'></tbody>
          <tfoot id='vyQDa'></tfoot>
            <i id='vyQDa'><tr id='vyQDa'><dt id='vyQDa'><q id='vyQDa'><span id='vyQDa'><b id='vyQDa'><form id='vyQDa'><ins id='vyQDa'></ins><ul id='vyQDa'></ul><sub id='vyQDa'></sub></form><legend id='vyQDa'></legend><bdo id='vyQDa'><pre id='vyQDa'><center id='vyQDa'></center></pre></bdo></b><th id='vyQDa'></th></span></q></dt></tr></i><div id='vyQDa'><tfoot id='vyQDa'></tfoot><dl id='vyQDa'><fieldset id='vyQDa'></fieldset></dl></div>
            • <bdo id='vyQDa'></bdo><ul id='vyQDa'></ul>
            • <legend id='vyQDa'><style id='vyQDa'><dir id='vyQDa'><q id='vyQDa'></q></dir></style></legend>

                • <small id='vyQDa'></small><noframes id='vyQDa'>

                • 本文介绍了是否可以在 C++ 中创建一种占用少于一个字节内存的类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  对于我的计算,我只需要使用 7 位空间,所以我使用的是 char 类型.但是我想知道是否可以声明我自己的使用少于一字节内存的类型?

                  For my computation I only need to use 7-bit space so I am using a char type. However I wonder if it is possible to declare my own type that uses less than one byte of memory?

                  推荐答案

                  并非如此.在结构体中,您可以使用位字段.因此,如果您知道您需要一定数量的固定条目,这将是一种节省一些位的方法(但请注意,该结构将始终至少填充到下一个完整的字节数).另请注意,由于普通"CPU 无法处理小于八位字节/字节的数量,因此对这些位字段值的访问可能会更慢,因为编译器必须生成额外的指令来获取/存储值在中间".所以为了节省几位,你必须花费一些CPU时间.

                  Not really. Inside a struct, you can make use of bit fields. So if you know you'll need a certain, fixed amount of entries, this would be a way to save a few bits (but note that the struct will always be padded to at least the next whole amount of bytes). Also note that due to the fact that "normal" CPUs can't address amounts smaller than an octet/byte, the access to these bit field values might be slower because of the extra instructions the compiler has to generate to get/store a value "in the middle". So in order to save a few bits, you have to spend some CPU time.

                  C++11 标准 在 1.7 The C++ memory model 一章中说(重点是我的):

                  The C++11 standard says in chapter 1.7 The C++ memory model (emphasis mine):

                  C++ 内存模型中的基本存储单元是字节.一个字节至少大到足以包含基本执行字符集 (2.3) 的任何成员和 Unicode UTF-8 编码形式的八位代码单元和由连续的位序列组成,其数量由实现定义.

                  The fundamental storage unit in the C++ memory model is the byte. A byte is at least large enough to contain any member of the basic execution character set (2.3) and the eight-bit code units of the Unicode UTF-8 encoding form and is composed of a contiguous sequence of bits, the number of which is implementation- defined.

                  换句话说:C++ 中最小的可寻址单元至少是 8 位宽.

                  In other words: the smallest addressable unit in C++ is at least 8 bits wide.

                  旁注:如果您想知道:有些机器,比如 DSP,一次只能寻址大于 8 位的单元;对于这样的机器,编译器可以将字节"定义为例如 16 位宽.

                  Side-note: In case you're wondering: there are machines like DSPs that can only address units larger than 8 bits at a time; for such a machine, the compiler may define "byte" to be, for example, 16 bits wide.

                  这篇关于是否可以在 C++ 中创建一种占用少于一个字节内存的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  What is inside .lib file of Static library, Statically linked dynamic library and dynamically linked dynamic library?(静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么?)
                  How do I load a C DLL from the SXS in Python?(如何从 Python 中的 SXS 加载 C DLL?)
                  Can Cython code be compiled to a dll so C++ application can call it?(Cython 代码可以编译成 dll 以便 C++ 应用程序可以调用它吗?)
                  Delay Loading DLLs(延迟加载 DLL)
                  Throwing C++ exceptions across DLL boundaries(跨 DLL 边界抛出 C++ 异常)
                  Loading a dll from a dll?(从 dll 加载 dll?)

                    • <tfoot id='ALmgh'></tfoot>

                    • <small id='ALmgh'></small><noframes id='ALmgh'>

                        <bdo id='ALmgh'></bdo><ul id='ALmgh'></ul>
                            <tbody id='ALmgh'></tbody>

                          • <legend id='ALmgh'><style id='ALmgh'><dir id='ALmgh'><q id='ALmgh'></q></dir></style></legend>
                            <i id='ALmgh'><tr id='ALmgh'><dt id='ALmgh'><q id='ALmgh'><span id='ALmgh'><b id='ALmgh'><form id='ALmgh'><ins id='ALmgh'></ins><ul id='ALmgh'></ul><sub id='ALmgh'></sub></form><legend id='ALmgh'></legend><bdo id='ALmgh'><pre id='ALmgh'><center id='ALmgh'></center></pre></bdo></b><th id='ALmgh'></th></span></q></dt></tr></i><div id='ALmgh'><tfoot id='ALmgh'></tfoot><dl id='ALmgh'><fieldset id='ALmgh'></fieldset></dl></div>