News for hackers. Stuff that matters.
Re:Comparing C and C++ usage and performance with a real wor... by khitchdee on Sunday September 03, 2017 @08:25PM (#15164776)
OK. Wouldn't that indirection add an extra instruction, each time you called a function?
Re:Comparing C and C++ usage and performance with a real wor... by KayEss on Monday September 04, 2017 @03:51AM (#15166477)
Yes, 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.
Re:Comparing C and C++ usage and performance with a real wor... by khitchdee on Monday September 04, 2017 @04:35AM (#15166659)
If 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
Re:Comparing C and C++ usage and performance with a real wor... by bstamour on Monday September 04, 2017 @11:30AM (#15168883)
Unless you explicitly type `virtual`, your C++ classes will have the exact same overhead as C structs. Even with inheritance. The memory layout of

    struct A { int x; };
    struct B : A { int y; };
is the same as if you had written

    struct B { int x; int y; };
Public/private/protected inheritance and access control do not add overhead. It's literally only if you opt in by typing `virtual` do you get class hierarchy overhead.
Re:Comparing C and C++ usage and performance with a real wor... by khitchdee on Monday September 04, 2017 @12:21PM (#15169186)
Public/private/protected inheritance and access control do not add overhead

That's interesting. So does the compiler just put the functions in different parts of the vtable to remember the access control rules. There's no such thing as a free lunch and you're adding information here -- has to be stored somewhere.

Re:Comparing C and C++ usage and performance with a real wor... by bstamour on Monday September 04, 2017 @04:43PM (#15170745)
Access control rules are all checked at compile time. There's literally nothing to store. If you want proof, check the output of your compiler. The only thing a non-virtual struct/class might do is reorder member variables if they're of different access controls, but if you're just using a C-style struct but with private member variables and public non-virtual member functions, it has literally the same memory layout as it would in C.
Re:Comparing C and C++ usage and performance with a real wor... by khitchdee on Monday September 04, 2017 @05:25PM (#15170947)
OK. That makes sense. So it's only a compiler overhead
Re:Comparing C and C++ usage and performance with a real wor... by psyc on Monday September 04, 2017 @10:58AM (#15168643)
You only pay for the indirection for virtual functions. You don't pay it simply for choosing C++. There is no "much larger memory structure" either. The vtable is per class. The per object cost is one pointer. C++ compilers are pretty smart.