Originally posted by: ObscureCaucasian
Originally posted by: engineereeyore
what you're doing is allocating a memory area large enough to store a VCore structure/class and then setting the next variable (memory location 0xabcd) equal to the address that was allocated for the structure/class. If the memory allocated is located at 0xabababab, then the value at 0xabcd would be 0xabababab. So next is simply used to reference that location in memory, which is where the structure is actually located.
Just to clarify, the value where 0xabcd used to be stored will be 0xabababab, but 0xabababab won't be stored in the address 0xabcd correct?
The value at 0xabcd would always represent the value of next. I'm just picking some arbitrary memory location as the location of the variable next. So from that point on, any time you reference the variable next, it's going to look at memory address 0xabcd for the value of next.
After you perform "next = new VCore", the CPU is going to allocate a block of memory to store an instance of the VCore class, in this example at memory location 0xabababab. So the value at 0xabcd will be 0xabababab.
Anytime you reference some element of the class, for instance next->variable1, what the computer is going to do is say, "Ok, the next variable is at address 0xabcd. Since next is a pointer, what is contained at 0xabcd isn't the instance of the class, but the memory location where the class was allocated. So I'm going to look at the address stored at 0xabcd, which is 0xabababab, for the variable I'm looking for."
If variable1 is the first element of the class, then the computer will look at address 0xabababab + 0 (the offset of variable1 in the class structure) for the value of variable1.
If this still doesn't make sense, or even if it does, here's a good explanation to look at as well:
Pointer explanation.
Here's what I interpret what you are trying to get across:
VCore *next;
// This means next = 0xa
// This means &next = 0xb
Now call:
next = new VCore;
// Now next = 0xc <==== the address where the new VCore was created
// Now &next = 0xb <==== the address where the next pointer is stored
Yep, that looks correct. Just remember that originally, where you have "next = 0xa", next could actually be any arbitrary value. But you are correct, the address where next is store should always be 0xb, but the value of next will change when you assign it to the result of "new VCore".
Looks like you got it though! Pointers, and how they work, are one of the biggest stumbling blocks for almost all C and C++ programmers, so don't feel bad.