Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pointer to struct or class versus pointer to first field

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?

like image 714
ApproachingDarknessFish Avatar asked Nov 20 '12 23:11

ApproachingDarknessFish


People also ask

What is the difference between a structure pointer and a pointer?

Pointer is a variable which holds the address of another variable of its type and points to the variable and can be used to read and write to that variable by dereferencing. Structs are data structures. Pointer to structure is a pointer which holds the address of a structure.

What is struct pointer in C++?

Structure Pointer: It is defined as the pointer which points to the address of the memory block that stores a structure is known as the structure pointer. Below is an example of the same: In the above code s is an instance of struct point and ptr is the struct pointer because it is storing the address of struct point.

How do you create a pointer variable in a struct?

Structures can be created and accessed using pointers. A pointer variable of a structure can be created as below: struct name { member1; member2; . . }; int main() { struct name *ptr; }. Here, the pointer variable of type struct name is created.

How to access members of a structure using pointers in C?

To access members of a structure using pointers, we use the -> operator. In this example, the address of person1 is stored in the personPtr pointer using personPtr = &person1;. Now, you can access the members of person1 using the personPtr pointer. Dynamic memory allocation of structs.


1 Answers

The address of an object shall always be the address of the first non-static member within that object. Quoting from the standard (C++11-9.2-20):

A pointer to a standard-layout struct object, suitably converted using a reinterpret_cast, points to its initial member (or if that member is a bit-field, then to the unit in which it resides) and vice versa. [ Note: There might therefore be unnamed padding within a standard-layout struct object, but not at its beginning, as necessary to achieve appropriate alignment.

The requirements for standard-layout are mentioned here: StandardLayoutType.

This can certainly be applied via nesting. The standard makes no exceptions for the type of the first member except for bit-fields. I.e.:

class X
{
public:
    int x;
};

class Y
{
public:
    X x;
    int y;
};

Y yobj;

By the standard, &yobj == &yobj.x == &yobj.x.x.

like image 141
WhozCraig Avatar answered Nov 15 '22 07:11

WhozCraig