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

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

          <bdo id='BLzmI'></bdo><ul id='BLzmI'></ul>

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

        在内存方面,C++ 参考看起来如何?

        时间:2023-07-19

          • <bdo id='JWO5s'></bdo><ul id='JWO5s'></ul>
              <tbody id='JWO5s'></tbody>
            <i id='JWO5s'><tr id='JWO5s'><dt id='JWO5s'><q id='JWO5s'><span id='JWO5s'><b id='JWO5s'><form id='JWO5s'><ins id='JWO5s'></ins><ul id='JWO5s'></ul><sub id='JWO5s'></sub></form><legend id='JWO5s'></legend><bdo id='JWO5s'><pre id='JWO5s'><center id='JWO5s'></center></pre></bdo></b><th id='JWO5s'></th></span></q></dt></tr></i><div id='JWO5s'><tfoot id='JWO5s'></tfoot><dl id='JWO5s'><fieldset id='JWO5s'></fieldset></dl></div>
          • <small id='JWO5s'></small><noframes id='JWO5s'>

            <tfoot id='JWO5s'></tfoot>

            <legend id='JWO5s'><style id='JWO5s'><dir id='JWO5s'><q id='JWO5s'></q></dir></style></legend>

                  本文介绍了在内存方面,C++ 参考看起来如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  给定:

                  int i = 42;
                  int j = 43;
                  int k = 44;
                  

                  通过查看变量地址,我们知道每个地址占用 4 个字节(在大多数平台上).

                  By looking at the variables addresses we know that each one takes up 4 bytes (on most platforms).

                  但是,考虑到:

                  int i = 42;
                  int& j = i;
                  int k = 44;
                  

                  我们会看到变量 i 确实占用了 4 个字节,但是 j 占用了 nonek 又占用了堆栈上有 4 个字节.

                  We will see that variable i indeed takes 4 bytes, but j takes none and k takes again 4 bytes on the stack.

                  这里发生了什么?看起来 j 在运行时根本不存在.我作为函数参数收到的引用呢?那必须在堆栈上占用一些空间...

                  What is happening here? It looks like j is simply non-existent in runtime. And what about a reference I receive as a function argument? That must take some space on the stack...

                  当我们在做的时候 - 为什么我不能定义一个数组或引用?

                  And while we're at it - why can't I define an array or references?

                  int&[] arr = new int&[SIZE]; // compiler error! array of references is illegal
                  

                  推荐答案

                  凡是遇到引用 j 的地方,都会被替换为 i 的地址.所以基本上引用内容地址是在编译时解析的,不需要像运行时指针那样解引用.

                  everywhere the reference j is encountered, it is replaced with the address of i. So basically the reference content address is resolved at compile time, and there is not need to dereference it like a pointer at run time.

                  只是为了澄清我的地址是什么 :

                  void function(int& x)
                  {
                      x = 10;
                  }
                  
                  int main()
                  {
                      int i = 5;
                      int& j = i;
                  
                      function(j);
                  }
                  

                  在上面的代码中,j不应该占用主栈的空间,而是引用x函数 将在其堆栈中占据一席之地.这意味着当使用 j 作为参数调用 function 时,i 的地址 将被压入函数的堆栈强>.编译器可以也不应该在主堆栈上为j保留空间.

                  In the above code, j should not take space on the main stack, but the reference x of function will take a place on its stack. That means when calling function with j as an argument, the address of i that will be pushed on the stack of function. The compiler can and should not reserve space on the main stack for j.

                  对于数组部分,标准说 ::

                  For the array part the standards say ::

                  C++ 标准 8.3.2/4:

                  不得有对引用的引用,不得有引用数组,并且没有指向引用的指针.

                  There shall be no references to references, no arrays of references, and no pointers to references.

                  为什么引用数组是非法的?

                  这篇关于在内存方面,C++ 参考看起来如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:C++,自由存储 vs 堆 下一篇:如何在 C++ 中使用 new 运算符初始化内存?

                  相关文章

                    <bdo id='mRbDb'></bdo><ul id='mRbDb'></ul>
                  1. <small id='mRbDb'></small><noframes id='mRbDb'>

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