ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Welcome to LinuxQuestions.org, a friendly and active Linux Community.

You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!

Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.

If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.

Having a problem logging in? Please visit this page to clear all LQ-related cookies.

Introduction to Linux - A Hands on Guide

This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.

Matir I think your program proves a big point. While the program may be small take to let say commercial size. I wonder what the results would be. You and your small app has helped me decide that I will study C instead of c++. Thank You.

No problem. I personally prefer C, but tried to make my pseudo-example as unbiased as possible. I would love for someone to provide a counter-example, even if it is NOT on runtime. Perhaps a counter-example of code readability, or stability. I don't think they'll find one, but I'm all for seeing one if there is one. We can all learn things.

In answer to your second (very different, completely independent!) question:

Q: "Can a C++ compiled binary be as efficient as it's C equivalent?"

A: "Quite possibly. But that's completely missing the point."

The question of "C" vs. "C++" (or, for that matter, C vs Perl or Visual Basic or anything else imaginable) is really "what's the best tool for the project"???

There is seldom any "right" answer, and how gosh darn small or fast the resulting binary compiles to is usually the *least* of your concerns. How closely the language maps to the problem domain is usually much more important. How robust and maintainable you can design the implementation is usually *vastly* more important. And, frankly, that's an area where C++ shines.

So no, C++ is not an appropriate choice for a kernel driver.

And yes, C++ might well be an appropriate choice for any library (e.g. programmer's API) or application (e.g. configuration program) that makes *use* of your kernel driver.

Kernel drivers are a "system thing", applications and application libraries are a "user thing", and C++ is more appropriate for the latter than the former.

Originally posted by schneidz through my previous analysis i conclude through single-statment 'hello-world' programs, 'c' would be about 5 times more efficient than 'c++' in the amount of machine code generated.

That's like looking at an apple and concluding that all apples are red (but in fact they aren't), or using Windows for 30 minutes without crashing it and concluding that it's stable and secure (but it isn't).
Hello World is barely representativ.

i think it can give a metric on what to expect on more robust testing (matir's testing).

what hello-world did for me was allow me to speculate on the probability of which is more efficient. much like not all apples are red but most are (probably about 90%). and windoze isn't always stable and secure but it mostly is (insert subjective opinion ----> here).

as a scientist we all test out speculations and then generate theories to be proved or disproved in postulations.

now that we have a basis for a baseline speculation, we should continue the thread by referencing in/formal reports that may bring us closer to a principle.

addy's post is correct because probably not all c++ code will be larger than comparable c code. (although there is more overhead with a fully object/class designed infrastructure).

Originally posted by schneidz
can someone provide examples where c++ will be faster than c?

Yeah, I was the guy with the original study, so my opinion may seem somewhat tainted, but here's my input as a Computer Science major:
C++ was never designed to be faster than C. It was designed to support classes, function and operator overloading, better type promotions, and other features.

b) If you're interested in analyzing system performance, this is a great book:

- The Practical Performance Analyst, Neil J. Gunther
(I understand he has a new version that uses Perl tools)

c) And these books have some very good discussions about the
relative UNIMPORTANCE of "optimizing code" (to the exclusion of
other concerns):

- The Practice of Programming, Brian Kernighan

- Code Complete, Steve Connell

I appreciate your interest in the questions "Can C++ be made to run faster than C?" and "How valid are 'Hello world' samples?" Matir did a great job, and I encourage you to look at the benchmarks I mentioned above.

But can we all agree that C++ is *not* a good tool for writing kernel drivers, and move on to another topic????

Originally posted by Matir Yeah, I was the guy with the original study, so my opinion may seem somewhat tainted, but here's my input as a Computer Science major:
C++ was never designed to be faster than C. It was designed to support classes, function and operator overloading, better type promotions, and other features.

I agree, but I think its definately possible to write C++ code that is just as fast as C code. It all depends on the programmer and their knowledge of the two languages.

I don't think this first C++ program would be any slower or eat any more memory than this second C program, although I don't have time to test it at the moment (I'm in Windows, I'll try to benchmark later). Actually looking at the code I'm not too sure, the C one could be slightly faster. Again I'll try to benchmark later using the same tests used earlier in this thread. I should be home later tonite, in about 12 hours or so.

edit: This is not a good example of C++ code that is "just as fast" as C code. If I wanted to do that I would write C code and compile with a C++ compiler. Technically almost all C code is also C++ code. I wanted to use a simple example of C++ that used classes and a simple example of C code that used a function.

The click router project at MIT was done in C++ for Linux. I have used the netlab.ru.is patches. Sweet. Works like a charm. My module is a stateful firewall and IDS/IPS written in C++. It has been used in VxWorks, pSOS, various flavors of Win, Linux, and under a real-time executive.

It is rather strange that embedded OS provide goods support for C++ but the Linux kernel does not.

I think the real problems with C++ kernel code are compiler-determined dependencies. Even without using a single #include, you can still incur dependencies when using C++. C++ generally requires some sort of entry and exit code, which g++ automatically links into executables and forces into init and fini in shared libraries. You'd be wasting your time to use static non-POD objects because it requires both initialization and destruction routines.

Most non-trivial kernel code requires dynamic allocation, or the kernel will provide the required memory in certain cases (such as ciphers,) which would require the use of in-place new with C++, which incurs a run-time dependency. As far as I know, the global new is also a run-time routine, though possibly-statically linked in by g++. In both cases you'd have to manually construct your structures, which is unreliable at best even without anything associated with the word "virtual."
ta0kira