OSDev.org

The Place to Start for Operating System Developers
It is currently Mon May 20, 2024 5:21 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 51 posts ]  Go to page Previous  1, 2, 3, 4
Author Message
 Post subject: Re: The C versus C++ debate
PostPosted: Fri Aug 20, 2010 5:16 am 
Offline
User avatar

Joined: Tue Jun 26, 2007 12:36 pm
Posts: 20
Kevin wrote:
Not sure if this is the best example for the usefulness of inheritance. You have one derived class per architecture in the source code, so what ends up in the binary (which is architecture specific) is exactly one implementation. You can certainly handle this without inheritance or any C++ - just link the the right putchar.o for the architecture. And you even save the overhead of virtual functions if you do without inheritance.


That's true, but it does enable me to have a LfbOut and a SerialOut and switch between them at runtime.

_________________
This is not a productive area of discussion


Top
 Profile  
 
 Post subject: Re: The C versus C++ debate
PostPosted: Fri Aug 20, 2010 5:42 am 
Offline
Member
Member
User avatar

Joined: Thu Nov 16, 2006 12:01 pm
Posts: 7615
Location: Germany
rJah wrote:
Solar wrote:
Overload operators only where it would feel natural and unambiguous to do so, and nowhere else,


Which is why I don't overload << in my output stream, I always found the "shift stream left" kind of strange.


Well, but that's the way how "it's done" in C++. It is natural and unambiguous to use operator<<() for C++ output. Overloading operator<<() allows the implementor of a class to enable the system to print that class, instead of you having to extend printf() (in non-standard ways, too). There are more reasons (e.g. type safety), but in the end it boils down to the old saying:

Real programmers can write FORTRAN in any language.

_________________
Every good solution is obvious once you've found it.


Top
 Profile  
 
 Post subject: Re: The C versus C++ debate
PostPosted: Fri Aug 20, 2010 6:03 am 
Offline
Member
Member

Joined: Sun Feb 01, 2009 6:11 am
Posts: 1070
Location: Germany
rJah wrote:
Kevin wrote:
Not sure if this is the best example for the usefulness of inheritance. You have one derived class per architecture in the source code, so what ends up in the binary (which is architecture specific) is exactly one implementation. You can certainly handle this without inheritance or any C++ - just link the the right putchar.o for the architecture. And you even save the overhead of virtual functions if you do without inheritance.


That's true, but it does enable me to have a LfbOut and a SerialOut and switch between them at runtime.

Right, this is a much better example.

_________________
Developer of tyndur - community OS of Lowlevel (German)


Top
 Profile  
 
 Post subject: Re: The C versus C++ debate
PostPosted: Fri Aug 27, 2010 3:47 am 
Offline
Member
Member

Joined: Sat Nov 18, 2006 9:11 am
Posts: 571
That was a perfect example of what not to do in the kernel... virtual functions. Granted, some people really don't care about performance, if that's the case, go for whatever is easier then, just don't expect miracles when you start benchmarking your kernel against others :). You do realize you could just as easily store a pointer to your current putChar right? Or even something like setting a default output device. I currently have all my serial devices in my VFS, and I can easily toggle my output between the screen and serial port without virtual functions, while still being perfectly readable. I am more from the performance side of the house... but everything in OS dev is a tradeoff. How much performance vs. readability/safety are you willing to sacrafice. There is no correct answer. I could write my OS in asm (and have written an os in asm)... development was slow, code wasn't as easily followed, not very likely that I could port it to another system, etc. But, it sure was fast, small and efficient :).


Top
 Profile  
 
 Post subject: Re: The C versus C++ debate
PostPosted: Fri Aug 27, 2010 3:53 am 
Offline
Member
Member
User avatar

Joined: Tue Oct 17, 2006 11:33 pm
Posts: 3882
Location: Eindhoven
Ready4Dis wrote:
That was a perfect example of what not to do in the kernel... virtual functions. Granted, some people really don't care about performance, if that's the case, go for whatever is easier then, just don't expect miracles when you start benchmarking your kernel against others :). You do realize you could just as easily store a pointer to your current putChar right?


*that's exactly what virtual functions ARE*.

Quote:
I am more from the performance side of the house...

Try using a profiler next time. Or code analysis.


Top
 Profile  
 
 Post subject: Re: The C versus C++ debate
PostPosted: Tue Aug 31, 2010 3:18 am 
Offline
Member
Member

Joined: Sat Nov 18, 2006 9:11 am
Posts: 571
Candy wrote:
Ready4Dis wrote:
That was a perfect example of what not to do in the kernel... virtual functions. Granted, some people really don't care about performance, if that's the case, go for whatever is easier then, just don't expect miracles when you start benchmarking your kernel against others :). You do realize you could just as easily store a pointer to your current putChar right?


*that's exactly what virtual functions ARE*.

Quote:
I am more from the performance side of the house...

Try using a profiler next time. Or code analysis.


I've done both, and I can say for 100% certainty (after a good amount of benchmarking thank you) that having a class full of virtual functions vs. a single device pointer is much slower. Yes, virtual functions ARE pointers, congratulations, you made it past C++ 101... Anyways, I'm not going to argue, just re-iterate what I've said. If you don't care about performance, and just want code readability (which he clearly stated he preferred), then go for it. The overhead of virtual functions, especially in higher level functionality is minimal, it's when people start abusing it in inner loops that require speed that the difference really shines. If the person knows how to use it, and when to use it, you can minimize the performance hits, which I and others have already stated. If you bothered to read his code, instead of just try to jump down my throat about how a virtual function is a pointer, you'd realize how inefficient his IOOutputStream class is. Firstly, in order to support multiple devices, he'd still need an array, or list of output streams... there's MY device pointer reference. Now, EACH of those streams has a set of virtual functions, which has an extra memory lookup to perform. If he calls printf, then calls putChar... that's another extra lookup. Yes, for an iostream, who cares, I'm sure it'll print to the screen plenty fast enough, but in general (unless you are VERY careful) virtual functions end up adding extra code, and slowing things down (how badly, and how much the effect depends on what ti's being used for).


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 51 posts ]  Go to page Previous  1, 2, 3, 4

All times are UTC - 6 hours


Who is online

Users browsing this forum: Majestic-12 [Bot] and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group