<tfoot id='XawK7'></tfoot>

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

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

      未初始化的变量会发生什么?C++

      时间:2023-05-24
      <i id='OcA9u'><tr id='OcA9u'><dt id='OcA9u'><q id='OcA9u'><span id='OcA9u'><b id='OcA9u'><form id='OcA9u'><ins id='OcA9u'></ins><ul id='OcA9u'></ul><sub id='OcA9u'></sub></form><legend id='OcA9u'></legend><bdo id='OcA9u'><pre id='OcA9u'><center id='OcA9u'></center></pre></bdo></b><th id='OcA9u'></th></span></q></dt></tr></i><div id='OcA9u'><tfoot id='OcA9u'></tfoot><dl id='OcA9u'><fieldset id='OcA9u'></fieldset></dl></div>

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

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

          <tfoot id='OcA9u'></tfoot>

                <tbody id='OcA9u'></tbody>
              • <bdo id='OcA9u'></bdo><ul id='OcA9u'></ul>
                本文介绍了未初始化的变量会发生什么?C++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                限时送ChatGPT账号..
                int main()
                {    
                    int a;
                    cout << a;
                    return 0;
                }
                

                我想知道为什么输出值 0.我想如果一个变量未初始化,它会输出一个垃圾值.

                I am wondering why the value 0 is being output. I thought if a variable is uninitialized, it would output a garbage value.

                不过,我也记得听说一个整数的默认值是0,所以我有点困惑.

                However, I also remember hearing that the default value of an integer is 0 so I am a bit confused.

                谢谢

                推荐答案

                C++ 中未初始化的函数作用域(即本地)整数的默认行为是 不确定,这很好;然而如果在定义之前使用该值会引入未定义的行为,并且任何事情都可能发生 - 恶魔可能会从你的鼻子里飞出来.

                The default behavior of an uninitialized function scope (i.e., local) integer in C++ is for it to be indeterminate, which is fine; however if that value is used before it is defined it introduces undefined behavior, and anything could happen - demons could fly out of your nose.

                cppreference 上的此页面提供了默认整数行为的示例.

                This page on cppreference provides examples of default integer behavior.

                另一方面,所有非局部、线程局部变量,而不仅仅是整数,都是 零初始化.但是这个案例没有包含在你原来的例子中.

                On the other hand, all non-local, thread-local variables, not just integers, are zero initialized. But this case wasn't included in your original example.

                (旁注:通常认为简单地初始化变量并完全避免潜在危险是一种很好的做法......特别是在 全局变量.)

                (Side note: It is generally considered good practice to simply initialize variables anyway and avoid potential hazards altogether... Especially in the form of global variables. )

                在极少数特殊情况下(例如某些嵌入式系统)使用全局变量的最佳实践有例外;根据启动时或初始循环迭代期间的传感器读数初始化值......并且需要在循环范围结束后保留​​一个值.

                There are exceptions to best practice using global variables in rare special cases, such as some embedded systems; which initialize values based off of sensor readings on startup, or during their initial loop iteration... And need to retain a value after the scope of their loop ends.

                这篇关于未初始化的变量会发生什么?C++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:在 C++ 中使用表示变量名称的字符串访问变量值 下一篇:是什么让静态变量只初始化一次?

                相关文章

                最新文章

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

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

                  <bdo id='Hs3Ts'></bdo><ul id='Hs3Ts'></ul>
                <tfoot id='Hs3Ts'></tfoot>
                1. <legend id='Hs3Ts'><style id='Hs3Ts'><dir id='Hs3Ts'><q id='Hs3Ts'></q></dir></style></legend>