Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a significant inherent cost of object instantiation in C++?

Tags:

c++

I was recently told in a code review (by an older and wiser C++ developer) to rewrite a class I'd written turning it into a set of static methods instead. He justified this by saying that although my object did contain a very small amount of internal state, it could be derived at runtime anyway and if I changed to static methods I'd avoid the cost of insantiating objects all over the place.

I have now made this change but it got me to thinking, what is the cost of instantiation in C++? I'm aware that in managed languages, there's all the cost of garbage collecting the object which would be significant. However, my C++ object was simply on the stack, it didn't contain any virtual methods so there would be no runtime function lookup cost. I'd used the new C++11 delete mechanism to delete the default copy/assignment operators so there was no copying involved. It was just a simple object with a constructor that did a small amount of work (required anyway with static methods) and a destructor which did nothing. Can anyway tell me what these instation consts would be? (The reviewer is a bit intimidating and I don't want to look stupid by asking him!) ;-)

like image 402
Benj Avatar asked Feb 28 '12 21:02

Benj


People also ask

What is instantiation of objects in C++?

In C++, the creation of a new instance of the class is called instantiation. Memory is allocated for that object and the class constructor runs. Programmers can instantiate objects on the heap with a new keyword or on the stack as a variable declaration.

How do you instantiate an object in C#?

When you create a new object in C# for a class using the new keyword, then it is called instantiation. Use the new operator to instantiate a class in C#.

How do you instantiate a class?

Instantiating a ClassThe new operator requires a single, postfix argument: a call to a constructor. The name of the constructor provides the name of the class to instantiate. The new operator returns a reference to the object it created.

Which constructor declaration is not valid in C++?

1 Answer. A constructor cannot specify any return type, not even void. A constructor cannot be final, static or abstract.


2 Answers

Short answer - inherently object allocation is cheap but can get expensive in certain cases.

Long Answer

In C++ the cost of instantiating an object is the same as instantiating a struct in C. All an object is, is a block of memory big enough to store the v-table (if it has one) and all the data attributes. Methods consume no further memory after the v-table has been instantiated.

A non-virtual method is a simple function with an implicit this as its first parameter. Calling a virtual function is a bit more complicated since it must to a v-table lookup in order to know which function of which class to call.

This means that instantiating a object on the stack involves a simple decrement of the stack pointer (for a full decending stack).

When an object is instantiated on the heap, the cost can go up substantially. But this is something inherent with any heap related allocation. When allocating memory on the heap, the heap needs to find a free block big enough to hold your object. Finding such a block is a non-constant time operation and can be expensive.

C++ has constructors that may allocated more memory for certain pointer data attributes. These are normally heap allocated. This is further compounded if said data members perform heap allocations themselves. This can lead to something involving a substantial number of instructions.

So bottom line is that it depends on how and what the object is that you are instatiating.

like image 149
doron Avatar answered Sep 21 '22 07:09

doron


If your object-type must invoke a non-trivial constructor and destructor during it's life-time, then the cost is the going to be the minimum cost of creating any C++ object that has a non-trivial constructor and destructor. Making the rest of your methods static will not reduce that cost. The "price" of space will be at least 1 byte since your class is not a base-class of a derived class, and the only cost-savings in the static class method calls will be the omission of the implicit this pointer passed as the hidden first argument of the call, something that would be required for non-static class methods.

If the methods your reviewer is asking you to re-designate as static never touch the non-static data-members of your class-type, then the passing of the implicit this pointer is a wasted resource, and the reviewer has a good point. Otherwise, you would have to add an argument to the static methods that would take the class-type as either a reference or pointer, nullifying the gained performance from the omission of the implicit this pointer.

like image 21
Jason Avatar answered Sep 23 '22 07:09

Jason