3

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.

IndustProg
  • 627
  • 1
  • 13
  • 33
Zax
  • 2,870
  • 7
  • 52
  • 76
  • 1
    How did you initialize `ptr` ? – Nbr44 Aug 01 '13 at 12:07
  • It probably means you haven't made `ptr` point to anything. Or it could be something else. Hard to say, since you haven't posted much relevant code. – juanchopanza Aug 01 '13 at 12:07
  • 2
    The value `0xcd` is typically used by VC++ to mark uninitialized memory. – Some programmer dude Aug 01 '13 at 12:08
  • 2
    `0xCDCDCDCD` : Used by Microsoft's C++ debugging runtime library to mark uninitialised heap memory. See http://stackoverflow.com/questions/127386/in-visual-studio-c-what-are-the-memory-allocation-representations for full list of special memory pointer values. – Violet Giraffe Aug 01 '13 at 12:09
  • [When and why will an OS initialise memory to 0xCD, 0xDD, etc. on malloc/free/new/delete?](https://stackoverflow.com/q/370195/995714), [In Visual Studio C++, what are the memory allocation representations?](https://stackoverflow.com/q/127386/995714) – phuclv Sep 14 '19 at 08:59

5 Answers5

6

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();
}
John Dibling
  • 99,718
  • 31
  • 186
  • 324
1

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

ptr = new myClass();

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

Dino
  • 599
  • 1
  • 9
  • 20
1

0xcdcdcdcd means uninitialized data.

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

Yochai Timmer
  • 48,127
  • 24
  • 147
  • 185
1

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.

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.

ogni42
  • 1,232
  • 7
  • 11
0

I can see stars.
Does this work instead?

class sampleClass
{

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

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

doctorlove
  • 18,872
  • 2
  • 46
  • 62