This question already has answers here:
Do class/struct members always get created in memory in the order they were declared?
(6 answers)
Closed 6 years ago.
Is the order of members in a) in a struct b) in a bitfield guaruanteed?
In other words, given a certain member of a struct or a bitfield, am I guaranteed that its offset from the beginning of the struct/bitfield will be no less than the sum of the sizes of the members that preceded it?
To give an example:
struct S{
char a[N];
unsigned b : M;
char c : O;
};
Will the offset of c be at least sizeof(a)+sizeof(b)?
Yes.
C++ standard:
Nonstatic data members of a (non-union) class declared without an intervening access-specifier are allocated so that later members have higher addresses within a class object.
Related
This question already has answers here:
C/C++ Struct vs Class
(7 answers)
Closed last year.
Is memory allocation for class (when initializing an object) is the same as struct? I know that when we initialize struct,
struct example {
int a;
double b;
int c;
}
the memory allocated will be = (0-4) int, (8-16) double, (16-20) int. (If I am not mistaken)
What if we want to initialize
class Example {
private:
int a;
double b;
int c;
public:
Example();
}
Example object();
?
What's the memory allocation behavior when the object is initialized? Is it the same as struct?
Yes. Literally the only difference between a class and a struct is that in a struct, member-variables/methods listed in the first section of the declaration (above any public:/protected:/private: lines) are treated as public, whereas in a class they are treated as private.
Ask sizeof. It is always correct. In this simple case, the class is guaranteed to have no hidden members; in general that isn't true.
The rule isn't about class vs struct; the only difference is the default in struct is public members but class is private members. The rule is about when you use expensive things. When you add virtual in, the sizes of things start to change. When you add virtual inheritance, things get a lot bigger.
You can only use RTTI on a class that has a virtual member. See How to get the typeid of a void* pointer? for details.
You have asked a further question; whether you can just add up the sizes of the members. The answer is a definite "No". You can't do that. On my platform, that struct may well take up, 24 bytes due to alignment (the double wants to be 8 byte aligned).
This question already has answers here:
Where are member functions stored for an object?
(2 answers)
Closed 1 year ago.
Let's say I have a struct like this:
struct 64BitStruct
{
uint64_t value;
void SomeFunction(bool enable);
bool SomeOtherFunction();
};
sizeof(64BitStruct) returns 8 bytes, which is 64 bits. I assume those 64bits are the value variable in the struct, but then where are the functions stored?
Member functions are common functions of all objects of the structure type. So they are stored separately from objects. The size of the structure in your example is in fact the size of its data member. If a structure has virtual functions then it implicitly includes a pointer to the table of virtual function pointers as a data member for each object of the structure type.
This question already has answers here:
Can I use C++ class members initialized in the initializer list, later in the list?
(2 answers)
Can member variables be used to initialize other members in an initialization list?
(5 answers)
Closed 3 years ago.
Let's say I have this constructor :
MyClass::MyClass()
: m_foo(new Foo())
, m_bar(new Bar(m_foo))
{
}
Is it safe/legit to use a member like this to initialize other members ?
Thanks
It depends on the order in which m_foo and m_bar appear in the class definition. Since data members are initialized in order of definition, the code above is safe only if m_foo appears before m_bar.
Compilers usually warn about order mismatches between the constructor initialization list and class definition.
This question already has answers here:
Iterate through Struct and Class Members [duplicate]
(6 answers)
Closed 5 years ago.
I am currently trying to iterate through a struct var in C++. I've got a struct var but I need to access specific elements of the struct.
someStruct {
int a;
int b;
bool c;
...
};
&someStructVar+1 would increase the memory by the size of the struct. But I need to increase the memory address by one bit after another. Is this possible? Is there any other approach?
What you're asking for is reflection, which C does not support. You can't get a list of fields of a struct and iterate through them.
You'll need to explicitly call out each field by name.
You can't "iterate" members of struct through pointer arithemetics, this is not allowed in C++.
The only thing you can iterate with pointer arithemtics are C-style arrays.
This question already has answers here:
Why class size depend only on data members and not on member functions?
(7 answers)
Closed 7 years ago.
Below are two structures defined in C/C++:
struct a
{
static int i;
void fun() {int i;}
};
struct b
{
static int i;
};
a obj1;
b obj2;
why sizeof of both obj1 and obj2 are same?
Non-virtual member functions and static members don't affect the size of an object, as they aren't stored inside the object.
Adding one or more virtual member functions would increase the size by an implementation-specific amount, usually the size of a pointer.