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

      <small id='4EPiy'></small><noframes id='4EPiy'>

          <bdo id='4EPiy'></bdo><ul id='4EPiy'></ul>
        <legend id='4EPiy'><style id='4EPiy'><dir id='4EPiy'><q id='4EPiy'></q></dir></style></legend>

        在 C 或 C++ 中返回结构是否安全?

        时间:2023-07-01
        <tfoot id='FX7jR'></tfoot>

        • <legend id='FX7jR'><style id='FX7jR'><dir id='FX7jR'><q id='FX7jR'></q></dir></style></legend>
            <tbody id='FX7jR'></tbody>

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

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

                • 本文介绍了在 C 或 C++ 中返回结构是否安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我的理解是不应该这样做,但我相信我已经看到过这样做的例子(注意代码不一定在语法上是正确的,但想法是存在的)

                  typedef struct{国际a,b;}我的结构;

                  然后是一个函数

                  mystruct func(int c, int d){mystruct retval;retval.a = c;retval.b = d;返回 retval;}

                  我知道如果我们想做这样的事情,我们应该总是返回一个指向 malloc 结构的指针,但我很肯定我已经看到了做这样的事情的例子.这个对吗?就我个人而言,我总是要么返回一个指向 malloc 结构的指针,要么只是通过对函数的引用进行传递并修改那里的值.(因为我的理解是,一旦函数的作用域结束,用于分配结构的任何堆栈都可以被覆盖).

                  让我们为问题添加第二部分:这是否因编译器而异?如果是,那么最新版本的桌面编译器的行为是什么:gcc、g++ 和 Visual Studio?

                  对此事的想法?

                  解决方案

                  这是完全安全的,而且这样做并没有错.另外:它不会因编译器而异.

                  通常,当(如您的示例)您的结构不是太大时,我认为这种方法甚至比返回 malloc 结构更好(malloc 是一项昂贵的操作).

                  What I understand is that this shouldn't be done, but I believe I've seen examples that do something like this (note code is not necessarily syntactically correct but the idea is there)

                  typedef struct{
                      int a,b;
                  }mystruct;
                  

                  And then here's a function

                  mystruct func(int c, int d){
                      mystruct retval;
                      retval.a = c;
                      retval.b = d;
                      return retval;
                  }
                  

                  I understood that we should always return a pointer to a malloc'ed struct if we want to do something like this, but I'm positive I've seen examples that do something like this. Is this correct? Personally I always either return a pointer to a malloc'ed struct or just do a pass by reference to the function and modify the values there. (Because my understanding is that once the scope of the function is over, whatever stack was used to allocate the structure can be overwritten).

                  Let's add a second part to the question: Does this vary by compiler? If it does, then what is the behavior for the latest versions of compilers for desktops: gcc, g++ and Visual Studio?

                  Thoughts on the matter?

                  解决方案

                  It's perfectly safe, and it's not wrong to do so. Also: it does not vary by compiler.

                  Usually, when (like your example) your struct is not too big I would argue that this approach is even better than returning a malloc'ed structure (malloc is an expensive operation).

                  这篇关于在 C 或 C++ 中返回结构是否安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:初始化 C++ 结构的正确方法 下一篇:此错误是什么意思:“错误:'type_name' 之前的预期说明符限定符列表"?

                  相关文章

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

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

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

                      <tfoot id='s7Vye'></tfoot>