C ++访问冲突读取位置调用函数时出现0xcdcdcdcd错误 [英] C++ Access violation reading location 0xcdcdcdcd error on calling a function

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

问题描述

请考虑以下情况:



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



exmp.h(标题文件)



exmp.cpp(源文件) b

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



exmp.cpp

  myClass :: bubSort b $ b {

....
....

}

其中 myClass-> 是在 exmp.h



现在为了在另一个文件 Sample.cpp 中使用函数bubSort(...),我已经在 Sample.h ,如下所示:

  / * Sample.h * / 
class myClass;

class sampleClass
{

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

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

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

上述情况在编译期间不会产生任何错误,当控制到达 ptr-> bubSort(...); 时,会出现异常:



违反读取位置0xcdcdcdcd



有人会告诉我如何避免这种情况。



提前感谢。

解决方案

ptr 是一个指向 myClass 的指针,但是你似乎没有初始化它。换句话说, ptr 没有指向任何东西。



当您尝试使用此未初始化的指针时,

  ptr-> bubSort(...); 

您将获得未定义的行为。你真的很幸运,应用程序崩溃,因为任何其他可能发生。它可能似乎工作。



要直接解决这个问题,您需要初始化 ptr 。一种方法:

  class sampleClass 
{
public:
sampleClass()

ptr(new myClass)
{
}
};

(关于时髦的语法,查找初始化列表)



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

  class sampleClass 
{
public:
〜sampleClass
{
delete ptr;
}
};问自己,如果你真的需要一个指针在这里,或者会做没有确定吗?

  class sampleClass 
{
public:
myClass mMyClass;
};

sampleClass :: func(...)
{
mMyClass.func();
}


Please consider the below scenario:

I have a header file and its corresponding source file:

exmp.h (Header file)

exmp.cpp (Source file)

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

exmp.cpp

myClass::bubSort(...)
{

....
....

}

Where, myClass-> is a class defined in exmp.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;
};

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

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

Access violation reading location 0xcdcdcdcd

Would anyone tell how I can avoid this.

Thanks in advance.

解决方案

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.

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")

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错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆