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.
Related
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:
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.
This question already has answers here:
Why do objects of the same class have access to each other's private data?
(7 answers)
What is the difference between private and protected members of C++ classes?
(19 answers)
Closed 7 years ago.
I was surprised to discover that this compiles.
class A
{
int i;
public:
A() {}
A(A &a) {i = a.i;}
};
int main(void)
{
A a1;
A a2(a1);
}
That is, the object a2 has access to the private members of the object a1. Why is this the case i.e. why is this useful? How can this access be restricted, if needed?
This question already has an answer here:
How to initialize a shared_ptr that is a member of a class?
(1 answer)
Closed 8 years ago.
I have a class as:
class LargeObject
{
public:
LargeObject();
void DoSomething();
private:
std::unique_ptr<Thing> pThing;
};
Then when I want to create the pointer in the constructor
LargeObject()
{
pThing(new Thing()); //This does not work.
}
I want to use the member variable throughout the code. How to do that?
I think initialization should be in constructor's initialization list, that's the place where constructors should be invoked from another constructor:
LargeObject()
:pThing(new Thing){}
This question already has answers here:
Memory allocation for member functions in C++
(4 answers)
Closed 8 years ago.
I have a C++ class that has functions that return objects:
class MyClass
{
public:
int m_v1;
double m_v2;
ObjA m_obA;
ObjB m_obB;
std::string m_s1;
std::vector< float > foo();
public:
MyClass();
~MyClass();
int foo2();
double foo3();
ObjA foo4();
};
I know that reorganising the members in descending order of the type, the memory used will be smaller. If it is the same for the function members, how should I organize it the best way?
As #juanchopanza pointed out non-virtual member functions don't affect the size of the object.
In 'most all implementations of C++, the FIRST virtual method increases object size by sizeof(pointer). Additional methods (virtual or otherwise) have no impact on object size.