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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17993591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:38:59  来源:igfitidea点击:

C++ Access violation reading location 0xcdcdcdcd error on calling a function

c++visual-c++exception

提问by Zax

Please consider the below scenario:

请考虑以下场景:

I have a header file and its corresponding source file:

我有一个头文件及其相应的源文件:

exmp.h (Header file)

exmp.h(头文件)

exmp.cpp (Source file)

exmp.cpp(源文件)

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

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

exmp.cpp

示例文件

myClass::bubSort(...)
{

....
....

}

Where, myClass->is a class defined in exmp.h

其中,myClass->exmp.h 中定义的类

Now in order to use the function bubSort(...)in another file Sample.cpp, I have declared myClassinside Sample.has shown below:

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

/*Sample.h*/
class myClass;

class sampleClass
{

  .....
  .....
  myClass *ptr;
};

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

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

//Sample.cpp
#include "exmp.h"
sampleClass::func(...)
{
     ....
     ....
     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:

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

Access violation reading location 0xcdcdcdcd

访问冲突读取位置 0xcdcdcdcd

Would anyone tell how I can avoid this?

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

Thanks in advance.

提前致谢。

回答by John Dibling

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

ptr是指向 a 的指针myClass,但您似乎从未初始化它。换句话说,ptr不指向任何东西。它是未初始化的——指向超空间。

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.

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

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

要直接解决此问题,您需要初始化ptr. 单程:

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

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

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

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 deleteanything you new, or you will leak memory:

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

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();
}

回答by ogni42

in order to solve it (one possible way), you must make 'ptr' actually to point to a adress where a myClass objects exists. In order to exist, you have to create it, e.g.

为了解决它(一种可能的方法),您必须使 'ptr' 实际上指向 myClass 对象所在的地址。为了存在,你必须创造它,例如

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

you might still consider using C++11's unique_ptr or shared_ptr instead of a raw pointer, though.

不过,您可能仍会考虑使用 C++11 的 unique_ptr 或 shared_ptr 而不是原始指针。

回答by Dino

You didn't initialize pointer, so you're calling empty memory. It is 0xcdcdcdcd and not some random stuff because of visual debugger.

您没有初始化指针,因此您正在调用空内存。由于可视化调试器,它是 0xcdcdcdcd 而不是一些随机的东西。

ptr = new myClass();

ptr = new myClass();

This will do. But you don't have to use pointer in this case at all.

这会做。但是在这种情况下您根本不必使用指针。

回答by Yochai Timmer

0xcdcdcdcdmeans uninitialized data.

0xcdcdcdcd表示未初始化的数据。

This means you're trying to access an uninitialized pointer somewhere.

这意味着您正试图在某处访问未初始化的指针。

Read more about it here:

在此处阅读更多相关信息:

Troubleshooting Common Problems with Applications: Debugging in the Real World

应用程序常见问题的故障排除:在现实世界中调试

Win32 Debug CRT Heap Internals

Win32 调试 CRT 堆内部结构

回答by doctorlove

I can see stars.
Does this work instead?

我能看到星星。
这有用吗?

class sampleClass
{

  .....
  .....
  myClass ptr;
};

Do not use pointers unless you know what you are doing.

除非您知道自己在做什么,否则不要使用指针。