I have the following data structures:
struct Inner
{
int myValue;
};
struct Outer
{
Inner* inner;
};
Option 1
If I do the following:
Outer outer;
outer.inner = NULL;
outer.inner = new inner;
inner* pInner = outer.inner;
and then add a watch for the following 2 values:
- outer.inner
- pInner
then they are both non-NULL, and equal in value.
Option 2
If I do the following:
Outer outer;
outer.inner = NULL;
inner* pInner = outer.inner;
if (! pInner)
{
pInner = new inner;
}
then pInner points to valid heap memory, and outer.inner is still NULL.
Questions
Why are the options different - shouldn't they both be pointing at the same memory in option 2 as well?
Assuming that I went with option 1, and I was then reading values out of inner. I could then use pInner and outer.inner interchangeably couldn't I? Why is this different to allocating the memory?