I have a question. I am trying to sere into my brain how pointers work and just when I think I got it a new problem arises. So I think I understand pointers pretty good when working with primitives and such. Now I am trying to understand working with objects and pointers.
So my problem is that if I write this:
int main()
{
objNumber *num1obj;
num1obj->setNum1(2);
num1obj->print();
delete num1obj;
return 0;
}
It compiles just fine but when I execute it, it crashes. My understanding is that this example is working on the Stack.
Now if I do this code:
int main()
{
objNumber *num1obj = new objNumber;
num1obj->setNum1(2);
num1obj->print();
delete num1obj;
return 0;
}
It compiles just fine as before, but now it returns the value I set in the parameter of setnum1(); and everything is flowers and sunshine in the world. My understanding is that since I added "new" I reserved a new space in memory on the Heap. If what I said about the Stack and Heap are true, why does it work on the Heap and not on the Stack. Or am I misunderstanding something here.
Thanks,
Dani