Does calling an empty class constructor invoke any memory?

Suppose I have a class like

class Empty{ Empty(int a){ cout << a; } } 

And then I call it with

 int main(){ Empty(2); return 0; } 

Will there be a reason for allocating any memory on the stack to create an "empty" object? Obviously, the arguments must be pushed onto the stack, but I don't want to take the extra overhead. I mainly use the constructor as a static member.

I want to do this because of the patterns. Actual code looks like

 template <int which> class FuncName{ template <class T> FuncName(const T &value){ if(which == 1){ // specific behavior }else if(which == 2){ // other specific behavior } } }; 

which allows me to write something like

 int main(){ int a = 1; FuncName<1>(a); } 

so that I can specialize in one parameter of the template without specifying type T In addition, I hope that the compiler will optimize other branches inside the constructor. If anyone knows if this is true or how to check, that would be greatly appreciated. I also suggested that throwing patterns into a situation does not change the "empty class" problem from above, is this true?

+9
c ++ micro-optimization templates metaprogramming
Jan 06
source share
3 answers

Quote from Stroustrup:

Why is the size of the empty class not equal to zero? Make sure the addresses of two different objects will be different. For the same reason, "new" always returns pointers to individual objects. Consider:

 class Empty { }; void f() { Empty a, b; if (&a == &b) cout << "impossible: report error to compiler supplier"; Empty* p1 = new Empty; Empty* p2 = new Empty; if (p1 == p2) cout << "impossible: report error to compiler supplier"; } 

There is an interesting rule that says that an empty base class should not be represented as a separate byte:

 struct X : Empty { int a; // ... }; void f(X* p) { void* p1 = p; void* p2 = &p->a; if (p1 == p2) cout << "nice: good optimizer"; } 

This optimization is safe and may be most useful. This allows the programmer to use empty classes to represent very simple concepts without overhead. Some modern compilers provide this "empty base class optimization".

+16
Jan 06 '10 at 21:45
source share

This may not be the case depending on the circumstances. If you say:

 Empty e; Empty * ep = & e; 

then obviously everything should be highlighted.

+2
Jan 6 '10 at 21:39
source share

Try and see. Many compilers will exclude such temporary objects when they are asked to optimize their output.

If the disassembler is too complicated, then create two functions with different numbers of such objects and see if there is a difference in the stack locations of the surrounding objects, for example:

 void empty1 ( int x ) { using namespace std; int a; Empty e1 ( x ); int b; cout << endl; cout << "empty1" << endl; cout << hex << int ( &x ) << " " << dec << ( &x - &a ) << endl; cout << hex << int ( &a ) << " " << dec << ( &a - &b ) << endl; } 

and then try to run this compared to the empty8 function with eight empty ones created. From g ++ to x86, if you take the address of any of the empty containers, you get the location between x and a on the stack, hence including x in the output. You cannot assume that the storage for the objects will be in the same order as in the source code.

+2
Jan 06 '10 at 21:44
source share



All Articles