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

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

          <bdo id='IYAyL'></bdo><ul id='IYAyL'></ul>
      1. <i id='IYAyL'><tr id='IYAyL'><dt id='IYAyL'><q id='IYAyL'><span id='IYAyL'><b id='IYAyL'><form id='IYAyL'><ins id='IYAyL'></ins><ul id='IYAyL'></ul><sub id='IYAyL'></sub></form><legend id='IYAyL'></legend><bdo id='IYAyL'><pre id='IYAyL'><center id='IYAyL'></center></pre></bdo></b><th id='IYAyL'></th></span></q></dt></tr></i><div id='IYAyL'><tfoot id='IYAyL'></tfoot><dl id='IYAyL'><fieldset id='IYAyL'></fieldset></dl></div>
        <tfoot id='IYAyL'></tfoot>
      2. 调用函数时 C++ 访问冲突读取位置 0xcdcdcdcd 错误

        时间:2023-10-05
        • <tfoot id='nZRU1'></tfoot>

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

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

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

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

                  <tbody id='nZRU1'></tbody>
                • 本文介绍了调用函数时 C++ 访问冲突读取位置 0xcdcdcdcd 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  请考虑以下场景:

                  我有一个头文件和它对应的源文件:

                  I have a header file and its corresponding source file:

                  exmp.h(头文件)

                  exmp.cpp(源文件)

                  在头文件中我有一个函数声明 bubSort(...) 其定义存在于

                  In the header file I have a function declaration bubSort(...) whose definition is present in

                  exmp.cpp

                  myClass::bubSort(...)
                  {
                  
                  ....
                  ....
                  
                  }
                  

                  其中,myClass->exmp.h

                  现在为了在另一个文件Sample.cpp中使用bubSort(...)函数,我在myClass中声明了myClassstrong>Sample.h 如下图:

                  Now in order to use the function bubSort(...) in another file Sample.cpp, I have declared myClass inside Sample.h as shown below:

                  /*Sample.h*/
                  class myClass;
                  
                  class sampleClass
                  {
                  
                    .....
                    .....
                    myClass *ptr;
                  };
                  

                  现在使用上面的ptr,我试图访问Sample.cpp中的bubSort(...),如下所示:

                  Now using the above ptr, I'm trying to access bubSort(...) in Sample.cpp as shown below:

                  //Sample.cpp
                  #include "exmp.h"
                  sampleClass::func(...)
                  {
                       ....
                       ....
                       ptr->bubSort(...);
                  }
                  

                  上述情况在编译过程中没有给出任何错误,但是在执行时,当控件到达ptr->bubSort(...);时,我得到一个异常:

                  The above scenario doesn't give any error during compilation, However while execution, when the control reaches ptr->bubSort(...);, I get an exception:

                  访问违规读取位置0xcdcdcdcd

                  Access violation reading location 0xcdcdcdcd

                  谁能告诉我如何避免这种情况?

                  Would anyone tell how I can avoid this?

                  提前致谢.

                  推荐答案

                  ptr 是一个指向 myClass 的指针,但您似乎从未初始化它.换句话说, ptr 没有指向任何东西.它未初始化——指向超空间.

                  ptr is a pointer to a myClass, but you don't seem to ever initialize it. In other words, ptr isn't pointing to anything. It's uninitialized -- pointing in to hyperspace.

                  当你尝试使用这个未初始化的指针时,

                  When you try to use this uninitialized pointer,

                  ptr->bubSort(...);
                  

                  您得到未定义的行为.您实际上很幸运,应用程序崩溃了,因为其他任何事情都可能发生.它似乎可以工作.

                  You get Undefined Behavior. You're actually lucky that the application crashed, because anything else could have happened. It could have appeared to work.

                  要直接解决这个问题,需要初始化ptr.一种方式:

                  To fix this problem directly, you need to initialize ptr. One way:

                  class sampleClass
                  {
                  public:
                    sampleClass()
                    :
                      ptr (new myClass)
                    {
                    }
                  };
                  

                  (有关时髦的 : 语法的解释,请查找初始化列表")

                  (For an explanation about the funky : syntax, look up "initialization list")

                  但这使用动态分配,最好避免.最好避免动态分配的主要原因之一是你必须记住delete任何你new的东西,否则你会泄漏内存:

                  But this uses dynamic allocation, which is best avoided. One of the main reasons why dynamic allocation is best avoided is because you have to remember to delete anything you new, or you will leak memory:

                  class sampleClass
                  {
                  public:
                    ~sampleClass()
                    {
                      delete ptr;
                    }
                  };
                  

                  问问自己这里是否真的需要一个指针,或者没有就可以吗?

                  Ask yourself if you really need a pointer here, or would doing without be ok?

                  class sampleClass
                  {
                  public:
                    myClass mMyClass;
                  };
                  
                  sampleClass::func(...)
                  {
                    mMyClass.func();
                  }
                  

                  这篇关于调用函数时 C++ 访问冲突读取位置 0xcdcdcdcd 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:SFML 在多线程中失败 下一篇:将 Unicode 转换为多字节

                  相关文章

                  最新文章

                  <small id='9JYrp'></small><noframes id='9JYrp'>

                      • <bdo id='9JYrp'></bdo><ul id='9JYrp'></ul>
                      <legend id='9JYrp'><style id='9JYrp'><dir id='9JYrp'><q id='9JYrp'></q></dir></style></legend>

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

                    2. <tfoot id='9JYrp'></tfoot>