-1

I'm confused as to what I'm doing wrong? While debugging, this shows 0xcdcdcdcd {theDouble=??? }so i know my variable isnt getting stored in my mutator. How would i go about fixing this issue? Am I supposed initialize this somewhere? I am using visual studio by the way. Thank you for the help.

int main()
{
    int i = 0;
    const int MAX = 4;
    CLASS **object = new CLASS*[MAX];
    string name = "todd";
    string aString = "words";
    double aDouble = 10.0;
    object[i]->setDouble(aDouble);
    return 0;
}

//.cpp
CLASS::Class()
{
    theDouble = new double;
    *theDouble = NULL;
}
CLASS::Class(double aDouble)
{
    *theDouble = aDouble;
}
void CLASS::setDouble(double p)
{
    *theDouble = p; 
double Class::getDouble()
{return (*theDouble);}

//.h
class CLASS
{
 protected:
    double *theDouble;
 public:
    Insurance();
    Insurance(double premium);
    //~Insurance();
    void setDouble(double p);
    double getDouble();
    string toString();
   };`
Iron Man
  • 7
  • 3
  • 2
    Why are you even using pointers in your program? There is no need for them whatsoever. – PaulMcKenzie Oct 04 '18 at 03:50
  • 3
    You've created an array of pointers, but they don't point anywhere because you never created any objects for the pointers to actually point to. Pointers don't automatically create an object and point to it when you declare them, you have to assign the address of an object yourself. – eesiraed Oct 04 '18 at 03:50
  • 2
    You also have other problems, such as storing the address of a local variable, assigning `NULL` to a double, memory leaks, and the completely unnecessary use of pointers in the first place. – eesiraed Oct 04 '18 at 03:52
  • As of right now the program is incomplete but the pointers will be used, I'm just trying to get it to store the information first. – Iron Man Oct 04 '18 at 04:07
  • Chances are the pointers are still completely unnecessary. It's rare that one would have to use pointers in good C++ (unless you're implementing some data structure like a BST), and using raw pointers is even more rare. – eesiraed Oct 04 '18 at 04:10
  • *As of right now the program is incomplete but the pointers will be used* -- That's not how using pointers works. Unless you write the pointer manipulation code to completion, then your program will not behave correctly. – PaulMcKenzie Oct 04 '18 at 04:16
  • *I'm confused as to what I'm doing wrong?* -- Well, you've basically messed up how to handle dynamically allocated memory properly, memory leaks, etc. That is done on multiple lines in the code you posted. – PaulMcKenzie Oct 04 '18 at 04:21
  • 3
    Sidenote: `0xCDCDCDCD` is [Microsoft-ese for uninitialized dynamic memory.](https://en.wikipedia.org/wiki/Magic_number_(programming)#Magic_numbers_in_other_uses_) – user4581301 Oct 04 '18 at 04:34
  • Your compiler turns UB into a diagnosable error message, it is a reliable indication of using an uninitialized pointer. object[i] = new CLASS; is missing. – Hans Passant Oct 04 '18 at 06:33
  • Why `double *theDouble`, then `new double...` and not `double theDouble`? Why you need to define address for single variable. – i486 Oct 04 '18 at 12:53

1 Answers1

1

You use CLASS, Class and Insurance in mix where you should use one name so your posted code can not compile. I replace those with IronMan.

What you see as 0xCDCDCDCD is not this pointer value but value of the pointer member theDouble. The issue comes from in constructor:

IronMan::IronMan(double aDouble)
{
    *theDouble = aDouble;
}

That constructor dereferences uninitialised member variable theDouble and since debuggers tend to fill uninitialized memory with some bytes like 0xCD you will have that as value of the pointer. Therefore you get a crash for accessing memory at such address.

One way out of it is to allocate memory for theDouble

IronMan::IronMan(double aDouble)
{
    theDouble = new double(aDouble);   
}

Better would be likely to avoid using pointers there at all but just have direct double data member:

protected:
    double theDouble;
Öö Tiib
  • 10,809
  • 25
  • 44