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.
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:
C++: Difference Between Non-Member Function and Static Member Function?
(3 answers)
Closed 1 year ago.
So using static keyword below we can call MyMethod without creating an object of the class MyClass, my question is how is it even possible as a class is not allocated memory when the code is compiled?
static int MyMethod( int * a, int * b );
int one = 1;
int two = 2;
MyClass::MyMethod( &two, &one );
It seems you have a bit of a misunderstanding. There is no memory allocated for a class's methods every time it is made. Class methods are generally separate from their classes. I guess you could call the "Memory allocated to the member functions" the space that the opcodes take up. But yes, this is separate from the objects. It is just a language requirement that a member function that isn't static must be called by an instance of the class. Static functions intentionally don't care about this, and can be called even when it is not called by an object.
This question already has answers here:
Object layout in case of virtual functions and multiple inheritance
(4 answers)
Closed 4 years ago.
I understand that for single inheritance a pointer to a virtual function table is added to determine what parent class functions to call at runtime.
class Genius {
int IQ;
public:
virtual void brag();
};
class Me : public Genius {
int age;
};
When instantiated, the memory layout of Me should look something like
pointer to Genius vtable
int iq
int age
But what happens in the case of multiple inheritance?
// Assume CoolDude has virtual functions as well
class Me : public Genius, public CoolDude {
int age;
};
What does the memory layout of the Me class look like now? How is multiple inheritance handled?
The class will have 2 pointers to vtables, one to its implementation of Genius and one to its implementation of CoolDude. When casting to a base class, the the returned pointer will differ from the original by the offset of the vtable(and other members) or the base class.
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.
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){}