4 ms·
If you ignore the protection mechanisms and the class heirarchy built into C++, then a C++ class is like a C struct that can contain function pointers. For many
by khitchdee 9y ago
If you ignore the protection mechanisms and the class heirarchy built into C++, then a C++ class is like a C struct that can contain function pointers. For many programs this is all that's needed, and the amount of overhead involved in using such an approach to creating objects is obviously lower. So there's no question C will always be faster. It's only when you need protection and class heirarchies that C++ benefits you. That benefit is mainly one of better code organisation.
- humanrebar 9y ago> It's only when you need protection and class heirarchies that C++ benefits you. Well, need is a strong word. You might not need better correctness while still coming out way ahead by using C++.
- khitchdee 9y agoProtection implies a much more complex structure to represent an object and class heirarchies and inheritance imply the need for a runtime. Both these overheads come at a cost. It's the nature of the program you are wriitng that determines whether you will come out ahead. If you were writing a codec, say, you would not use C++.
- humanrebar 9y agoNot necessarily. A trivial scope guard initialized with a lambda has basically no overhead versus the equivalent be-really-careful approach in C.
- khitchdee 9y agoThat would be cruelty to the cat dancing on the hot tin roof.
- Joky 9y agoCan you clarify what runtime needs would a class hierarchy have in C++ that a correctly structured C program wouldn't have?
- khitchdee 9y agoWell, if you override the implementation of a function in a subclass, the runtime has to determine that and load it in at runtime, when you instantiate an object of the subclass. In C there is no runtime.
- psyc 9y agoThis is done at compile time. The call is indirect, which only means the call destination is decoupled from the generated calling code. This does not entail the runtime loading anything. http://www.geeksforgeeks.org/virtual-functions-and-runtime-polymorphism-in-c-set-1-introduction/ http://www.geeksforgeeks.org/virtual-functions-and-runtime-p...
- khitchdee 9y agoOK. Wouldn't that indirection add an extra instruction, each time you called a function?
- KayEss 9y agoYes, but if your design requires the virtual function then you'll be using a function pointer in the C implementation as well, which has the same indirection.
- khitchdee 9y agoIf your design requires inheritance or virtual functions, C++ is the right choice. But in many cases, when it doesn't, if you still use C++, you'll pay the price of an extra indirection and a much larger memory structure to hold your objects. If your function were processing the inner loop of a video codec, that would unneccesarily slow you down
- nly 9y ago> a C++ class is like a C struct that can contain function pointers. No, it's not. Calling an ordinary class member function in C++ has exactly the same overhead as calling a function in C. Even virtual functions in C++ are not the same as putting function pointers in a C struct (they live in a separate data structure called the vtable). > the protection mechanisms and the class heirarchy All C++ protection mechanisms occur at compile time and have no runtime overhead. Non-virtual inheritance hierarchies have the same overhead as C struct composition (because under the covers the memory layout is the same).
- cperciva 9y agoCalling an ordinary class member function in C++ has exactly the same overhead as calling a function in C. Modern compilers make the same optimization for function pointers which are only ever set to one value, too.
- nly 9y agoHi Colin. This isn't an optimisation though, it's a guarantee. Member function calls are resolved statically at compile-time. Replacing indirect calls off a function pointer with direct calls (devirtualization) is an optimisation that applies to both languages equally and requires whole-program / link-time optimization.
- khitchdee 9y agoDoesn't a vtable imply an extra level of indirection? You have to find where the vtable is in the object, then the function within the vtable, right? Is that not slower?
- jcelerier 9y ago> So there's no question C will always be faster Except that the poster above you shown that the opposite was true. The exact same program was written multiple times with C structs and with C++ classes and, "Except for the non-inline unbundled monostate in C++, every non-inline C++ implementation outperformed every non-inline C implementation."
- andrepd 9y agoThat is absolutely false. C++ member functions are zero-cost abstractions, i.e. they have the same cost as any other function call. Member functions are _not_ function pointers that reside inside the struct. They don't take up space, they don't need dereferencing to call. They are just "syntatic sugar" to group functions more logically.
- inetknght 9y ago> Member functions are _not_ function pointers that reside inside the struct. ...as long as they're not virtual functions, this is correct. Add a virtual table and this is less correct (but the optimizer may still make it correct if it can prove the types match).
- andrepd 9y agoYes, that is correct, but then again virtual functions give you new functionality, namely dynamic dispatching. Stick with static inheritance and you won't have this overhead.
- khitchdee 9y agoPlease check your facts. C++ member functions are not as simple as function pointers inside structs.
- ric129 9y agoOnly virtual functions behave as you described, and even them don't exactly cause a function pointer to be stored in an object.