I recently tried debugging a small program by printing the values of several pointers to the console. The first was the memory address of a struct, and the others were the memory addresses of its fields. A stripped-down version of the code is as follows:
#include <iostream>
struct testingPointers
{
int i;
float f;
double d;
} test;
int main()
{
std::cout << &test << '\n' << &(test.i) << '\n' <<
&(test.f) << '\n' << &(test.d);
}
And the output is:
0x681110
0x681110
0x681114
0x681118
(obviously the exact values are different for different runs but they always have the same positions relative to each other).
I am confused because the value of first pointer–the memory location of test–is the same as that of the second one (the first field of test). Does this mean that objects have no real unique memory address, and that a pointer to a struct or class simply points to its first field? If so, how do statements like
a.b
a->b
a.b()
make sense if a is actually just its first field, and therefore does not have any fields or methods?
A class or struct just describes a collection of fields that should be kept together in memory, and have some semantic relationship between them and some operations that operate over them. There is, in the simple case, nothing much more to the content of a class type object in memory than the members it is made up of (and some padding). When you have a
testingPointersobject in memory, it is really just anint, afloatand adouble. The concept of the class was only used to generate the correct executable code – it does not exist at run time (at least not for this purpose).The important part from the standard regarding whether objects can share memory addresses is §1.8/6:
We can infer from this that because member
test.iis a subobject oftest, they may well have the same address.Once you look inside all of the objects of your program as deep as you can go, what you really have is a big collection of scalar values and adjacent bit-fields. These are known as memory locations in the standard. These are the things that really take up space. The rest of your objects are all in some way composed of these.