Data members layout in memory

Suppose i have a struct with data members of same type:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
struct foo
{
    int a;
    int b;
    int c;
    
    // is this safe?
    operator int* ()
    {
        return &a;
    }
};
...
foo f;

int* p = f;
p[0] = 5;  // sets a
p[1] = 10; // sets b
p[2] = 15; // sets c 


Is it guaranteed that a, b & c are stored sequentially in a memory?
No, with data members of same type?
If i understood that wiki article, it won't add padding in this case?
Last edited on
It is guaranteed that they are stored sequentially, but not that they are stored continuously. The address of f and the address of f.a are the same, and b follows a, but the implementation is free to inset arbitrary padding between a and b.
Not to mention that p is not a pointer to an element of an array so you can't dereference p[1] anyway.
Ok. Thanks.
Topic archived. No new replies allowed.