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

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

        <legend id='dc5L2'><style id='dc5L2'><dir id='dc5L2'><q id='dc5L2'></q></dir></style></legend>
      1. <tfoot id='dc5L2'></tfoot>

        调用函数时 C++ 访问冲突读取位置 0xcdcdcdcd 错误

        C++ Access violation reading location 0xcdcdcdcd error on calling a function(调用函数时 C++ 访问冲突读取位置 0xcdcdcdcd 错误)
          <bdo id='4FBj0'></bdo><ul id='4FBj0'></ul>

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

                <tbody id='4FBj0'></tbody>

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

                <legend id='4FBj0'><style id='4FBj0'><dir id='4FBj0'><q id='4FBj0'></q></dir></style></legend>

                • 本文介绍了调用函数时 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 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  How to print vector#39;s data(如何打印矢量的数据)
                  Visual C++ appends 0xCC (int3) bytes at the end of functions(Visual C++ 在函数末尾附加 0xCC (int3) 字节)
                  How to use a variable inside a _T wrapper?(如何在 _T 包装器中使用变量?)
                  MSVC++ warning flags(MSVC++ 警告标志)
                  How to read file which contains uxxxx in vc++(如何在vc++中读取包含uxxxx的文件)
                  stack overflow error in C++ program(C++程序中的堆栈溢出错误)

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

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