9

The question is NOT about the Linux kernel. It is NOT a C vs. C++ debate either.

I did a research and it seems to me that C++ lacks tool support when it comes to exception handling and memory allocation for embedded systems:

Why is the linux kernel not implemented in C++? Beside the accepted answer see also Ben Collins' answer.

Linus Torvalds on C++:

"[...] anybody who designs his kernel modules for C++ is [...]
(b) a C++ bigot that can't see what he is writing is really just C anyway"

" - the whole C++ exception handling thing is fundamentally broken. It's especially broken for kernels.
- any compiler or language that likes to hide things like memory allocations behind your back just isn't a good choice for a kernel."

JOINT STRIKE FIGHTER AIR VEHICLE C++ CODING STANDARDS:

"AV Rule 208 C++ exceptions shall not be used"


  1. Are the exception handling and the memory allocation the only points where C++ apparently lacks tool support (in this context)?

  2. To fix the exception handling issue, one has to provide bound on the time till the exception is caught after it is thrown?

  3. Could you please explain me why the memory allocation is an issue? How can one overcome this issue, what has to be done?

As I see this, in both cases one has to provide an upper bound at compile time on something nontrivial that happens and depends on things at run-time.


Answer:

  1. No, dynamic casts were also an issue but it has been solved.

  2. Basically yes. The time needed to handle exceptions has to be bounded by analyzing all the throw paths.

  3. See solution on slides "How to live without new" in Embedded systems programming. In short: pre-allocate (global objects, stacks, pools).

Community
  • 1
  • 1
Ali
  • 56,466
  • 29
  • 168
  • 265
  • 13
    Ultimately, Linus doesn't like C++, so it isn't going in the kernel. – Vaughn Cato Aug 18 '12 at 16:51
  • Yes, I know but the question is not about the Linux kernel in particular. Also, I did not add the linux kernel tag. – Ali Aug 18 '12 at 16:54
  • 4
    This question is not appropriate here; it has no right answer, it solicits discussion, and this does not meet the criteria of the FAQ. Aside from which, there are plenty of 'kernels' written in C++, so its premise is counterfactual. – bmargulies Aug 18 '12 at 16:57
  • @bmargulies Which SE site should I use then? From the FAQ: "if your question generally covers … software tools commonly used by programmers". The question is about lack of software tools. – Ali Aug 18 '12 at 16:59
  • @VaughnCato Not widespread use / C is preferred, that what I mean by prohibited. Feel free to edit the title and propose any better word. – Ali Aug 18 '12 at 17:00
  • 1
    Although the question has been closed, I really recommend reading [this](http://msdn.microsoft.com/en-us/library/windows/hardware/gg487420.aspx) article from Microsoft about using C++ for drivers; although it's Windows-centric, many problems are common to any modern OS kernel. – Matteo Italia Aug 18 '12 at 17:27
  • *"Could you please explain me why the memory allocation is an issue?"* Ahhh...I'd suggest that you sit and think hard for a while until this is obvious (and it sounds like you *should* be able to see this), because without that insight you're not really ready to think about coding in a kernel context. – dmckee --- ex-moderator kitten Aug 18 '12 at 19:58
  • 2
    @VaughnCato: I think he doesn't like C++ programmers more than C++ itself; quoting [Linus Torvalds on C++](http://harmful.cat-v.org/software/c++/linus): *Quite frankly, even if the choice of C were to do **nothing** but keep the C++ programmers out, that in itself would be a huge reason to use C.* – Michał Górny Aug 18 '12 at 20:05
  • @dmckee To quote myself: "I have only done Desktop application programming in my life, no system programming, and as for my education, I am a chemical engineer." Still, I have the right to live, learn, and while learning, ask questions. My questions may seem silly to you... I accept that. – Ali Aug 18 '12 at 20:07
  • 2
    There's a **very** simple answer, which I think is the reason, but I just wish I could post it, since the question's closed... just read MSDN, it plainly tells you why C++ can't be used: *"**Resources, particularly the stack, are severely constrained**. Resources that are “inexpensive” in user space may be expensive or require different methods to obtain in kernel mode. Specifically, the size of the kernel stack is 3 pages."* C++ **loves** allocating on the stack, so obviously that's a problem... also see the part about inlining (inline methods in C++ very very common). – user541686 Aug 18 '12 at 20:34
  • 1
    Yes, of course you have a right to learn, but you would be better served by discovering this yourself than by being told: it is a very small matter but of great importance and you have the tools to figure it out. As for the question it is a good question for itself, but is more about programmers than about programming (because people *do* use c++ for writing kernels, but some continue to prefer plain c). You might poke about on Programmer.SE a little to see if this has been addressed there. – dmckee --- ex-moderator kitten Aug 18 '12 at 20:37
  • @dmckee Thanks. Yes, I should do some kernel programming, you are right. Actually, I might post this question to comp.lang.c++.moderated but improve it first, based on the answers I've got here. – Ali Aug 18 '12 at 21:10

3 Answers3

14

Well, there are a couple of things. First, you have to remember that the STL is completely built on OS routines, the C standard library, and dynamic allocation. When your writing a kernel, there is no dynamic memory allocation for you (you're providing it) there is no C standard library (you have to provide one built on top of your kernel), and you are providing system calls. Then there is the fact that C interops very well and easily with assembly, whereas C++ is very difficult to interface with assembly because the ABI isn't necessarily constant, nor are names. Because of name mangling, you get a whole new level of complication.

Then, you have to remember that when you are building an OS, you need to know and control every aspect of the memory used by the kernel. In C++, there are quite a few hidden structures that you have no control over (vtables, RTTI, exceptions) that would severely interfere with your work.

In other words, what Linus is saying is that with C, you can easily understand the assembly being generated and it is simple enough to run directly on the machine. Although C++ can, you will always have to set up quite a bit of context and still do some C to interface the assembly and C. Another reason is that in systems programing, you need to know exactly how methods are being called. C has the very well documented C calling conventions, but in C++ you have this to deal with, name mangling, etc.

In short, it's because C++ does things without you asking.

Per @Josh's comment bellow, another thing C++ does behind your back is constructors and destructors. They add overhead to enter and exiting stack frames, and most of all, make assembly interop even harder, as when you destroy a C++ stack frame, you have to call the destructor of every object in it. This gets ugly quickly.

Linuxios
  • 34,849
  • 13
  • 91
  • 116
  • If you were writing the Kernel, could you not then specify the implementation defined stuff to be a certain way on that platform (name mangling, calling conventions, exceptions, etc, etc)? vtables and this pointer for example MS seems to have basically said on Windows shall be a certain way for COM. – Fire Lancer Aug 18 '12 at 17:10
  • @FireLancer: The compiler is the one responsible for name mangling, not the OS/kernel. – Cornstalks Aug 18 '12 at 17:11
  • Yes but if say Windows had said name mangling should be done like so, would not all Windows C++ compilers have done it that way? – Fire Lancer Aug 18 '12 at 17:13
  • @FireLancer: Name mangling is a compiler thing that isen't very easy to deal with and makes your code *very* cryptic (who wants to remember `_Z4skasdf3_print` as the name of your print function)? As for calling conventions, that's generated by the compiler, not the OS. And Windows is the worst example here, as COM is an unholy nightmare. And we're much lower level then COM. COM is built on layers and layers of assembly and C in the NT kernel. – Linuxios Aug 18 '12 at 17:13
  • 1
    In addition, I think the constructor/destructor and virtual functions are what Linus is referring to about doing things behind your back. These are all fundamental to C++ and it wouldn't really have the "++" without it. Most programmers writing plain old userland code want these things (because they are awesome and make your life easier. RAII can be used in so many different ways to make your life safer). However, programmers in the kernel want to know what is going on (very specifically). – Josh Petitt Aug 18 '12 at 17:13
  • @FireLancer: That doesn't make name mangling any easier, even if they all did. And there's nothing to force them to do it the same way. – Linuxios Aug 18 '12 at 17:14
  • @JoshPetitt: Exactly! I'll add that. – Linuxios Aug 18 '12 at 17:14
  • @Linuxios: I believe that if you were doing something *public* in C++ for the kernel, you'd actually use `extern "C"` to disable the name mangling. – Michał Górny Aug 18 '12 at 17:18
  • @MichałGórny: But then you'd be using C... – Cornstalks Aug 18 '12 at 17:20
  • @MichałGórny: Yes, but as Linus said, than it's C. – Linuxios Aug 18 '12 at 17:20
  • @MichałGórny You bring up a good point. Namespaces are another area where C++ is awesome. I type all day, but I don't want to do more than required. When writing in C (which I do for a living), if you do it well, you end up with really, really long function names cause you have to mangle it yourself. However, with C, the decision of "how shalt I mangle?" is made by the author. With C++ the decision is made by another tool, so it can't be guaranteed and specific. (AFAIK, mangling convention is left to the compiler vendor and there is no standard.) For the kernel guys, obvious is better. – Josh Petitt Aug 18 '12 at 17:25
  • @JoshPetitt: Exactly. If I'm asked to write a GUI app in C or C++, I'll choose C++, but if I'm given that chose for a driver or kernel, C all the way. – Linuxios Aug 18 '12 at 17:27
  • 6
    No, Linus is just stupid. :-) By claiming that C++ programmers are inferior, he doesn't get any help from those of us who could write good C++ code. It's a catch-22 situation. Kernels are written in C because they have a C API, which is just because they *have* a C API. – Bo Persson Aug 18 '12 at 18:21
  • @BoPersson: I hope you ment the first part sarcastically. I agree that I by no means think that C++ programers are by definition inferior and think that that wording is a little strong... But I do agree that the kernel should stay C. Help from C++ programers writing C is good, but there will be no C++ in Linux -- save the Linux C++ for Qt. – Linuxios Aug 18 '12 at 18:25
  • 5
    It is just *slightly* sarcastic. Linux will not get any C++ code because Linus doesn't want that, not because C++ isn't suitable. Sure you cannot use `std::vector` when implementing memory management in the kernel, but you can't use `malloc` either. Putting code inside a class doesn't make the code size explode, but *does* make it more local and easier to maintain. Linus is (knowingly) putting off a lot of good developers (like me :-) by saying that C is better. I think he is wrong, and doing the community a disservice. – Bo Persson Aug 18 '12 at 18:31
  • 4
    @Linuxios - Beause Linus is wrong, and C++ is no mystery to those who are familiar with it. Constructors and destructors are no surprises to those who designed the classes. And in C, not even Linus can tell that `f(x)` will call other functions 20 levels deep. There *is* no difference. – Bo Persson Aug 18 '12 at 18:37
  • 1
    @BoPersson: And how is that a valid reason to downvote? – Linuxios Aug 18 '12 at 18:38
  • @BoPersson: Take up the constructor thing with Josh above. – Linuxios Aug 18 '12 at 18:40
  • 5
    When the discussion is about C++ code bloat, I often end up linking to [my answer here](http://stackoverflow.com/a/11639305/597607), where 10+ lines of templated C++ code is inlined by the compiler and ends up in 5 lines of assembly. Why is everyone afraid of such code? Any equivalent C code would do much worse... – Bo Persson Aug 18 '12 at 18:51
  • @BoPersson: I'm not trying to argue with you. I just want an invalid downvote removed. I agree with you on many of your points. – Linuxios Aug 18 '12 at 18:53
  • Ok, so I disagree with *"In C++, there are quite a few hidden structures that you have no control over (vtables, RTTI, exceptions) that would severely interfere with your work."*. If I use these features, they are in no way hidden to me. – Bo Persson Aug 18 '12 at 18:56
  • @BoPersson: Sorry if that didn't make sense -- I was talking about memory locations. I have no way of controlling where my vtable lives, which is an important thing to be able to do in systems programming -- control the locations of everything. – Linuxios Aug 18 '12 at 19:02
  • @BoPersson: Also, you can question Linus' judgment all you want, but you can't call the creator of one of the worlds most popular kernels and version control systems stupid. – Linuxios Aug 18 '12 at 19:06
  • @Linuxios +1 and thanks for your efforts to answer my question. Could you please expand on "In C++, there are quite a few hidden structures that you have no control over (vtables, RTTI, exceptions) that would severely interfere with your work." I have only done Desktop application programming in my life, no system programming, and as for my education, I am a chemical engineer. – Ali Aug 18 '12 at 19:08
  • @Ali: Sure. What I mean is that with C++ polymorphism you have a few structures in memory that although they are not hidden from you, it is difficult to get the address they live at, let alone change it. Having control over the locations of all memory used by your kernel is very important, which is why this could be an impediment. To change the location of the vtable, you'd probably have to edit the compiler generated assembly. – Linuxios Aug 18 '12 at 19:13
  • @Linuxios OK, I believe now I understand the second paragraph of your answer. – Ali Aug 18 '12 at 19:15
  • @Linuxios Sorry, but no, its not. No developer ever would need to pin certain objects into memory address in a modern kernel. And even then, there is nothing that prevents you from doing the same things in C++. The whole discussion boils down to 2 points, politics, and certain C++ features must be used with more care (or cannot be used at all) – Christopher Aug 18 '12 at 19:23
  • 1
    @Linuxios I have no difficulty accepting that C is better suited for *Linux* kernel development than C++. I am still not convinced that for smaller systems (tiny microcontrollers for doing simple things) C++ is not a better choice. "In short, it's because C++ does things without you asking." Yes, I agree, currently this is a problem. But that's what I call lack of tool support. These things are done by tools and the tools should present these things to us in a proper and easy-to-digest way. – Ali Aug 18 '12 at 19:26
  • @Christopher Could you please write an answer and expand on "certain C++ features must be used with more care (or cannot be used at all)"? – Ali Aug 18 '12 at 19:27
  • 3
    @Ali - C++ doesn't do anything without you asking. If you write a constructor and destructor, you *are* asking for it. And you would have to write the same code in C anyway (just called `init(&x)` and `destroy(&x)`). – Bo Persson Aug 18 '12 at 19:32
  • @Linuxios So, the lack of tool support. Everyone is bitching about the error messages you get with templates. Long story short, it again boils down to [lack of tool support](http://stackoverflow.com/q/8779521/341970). AFAIK, in clang you get good quality error messages, so it was really just the lack of tool support. What I am trying to figure out here: Is it just the lack of tool support that makes C preferred over C++ or is there something else? – Ali Aug 18 '12 at 19:40
  • @BoPersson Yes, you don't have to convince me! :) I am trying to understand why people prefer C to C++. I guess it is just the lack of tool support why people prefer C. Please write up all your comments in an answer! – Ali Aug 18 '12 at 19:43
  • @Ali and Bo, it is possible to write "kernel" code in C++. However, if given a language you should be able to use all of it. C is a simple language. C++ is a complex language. At the end of the day it is probably easier to say, "Use C" rather than continually saying, "You can use C++, but don't do that." It's a slippery slope. You've probably got most of the most common reasons why here. If you want to use C++, fine nobody will convince you otherwise. But you have an answer to your question. – Josh Petitt Aug 18 '12 at 19:45
  • @JoshPetitt I would like to know the "but don't do that" part you write. I would like to understand what I should not do, why, and is it just a lack of tool support or is it a C++ flaw. My question is about C++'s lack of tool support and how to fix it. **I totally except that people use C for Linux kernel development and at the moment it is a better choice than C++. Just my question is not about the Linux kernel or a C vs. C++ debate.** – Ali Aug 18 '12 at 19:51
  • 1
    Why is everyone talking about *making* a kernel? Kernel-mode programming isn't just about *making* a kernel, it's about writing drivers, etc. too... and they have just as good of a memory-allocation system available to them as in user mode. Obviously, the lack of memory allocation isn't the issue unless you're implementing the memory manager. – user541686 Aug 18 '12 at 20:32
  • OK people, this has gotten out of hand. If you wish to have a protracted argument on the subject, take it to chat. Comments aren't really the place for this. – Brad Larson Aug 18 '12 at 21:34
  • @BradLarson Could you please lock the question? It really has gotten out of hand, although it wasn't my intention, I am very sorry... – Ali Aug 18 '12 at 21:37
  • @Ali - No need to lock it (which we only use in rare cases) if everyone just calms down and takes this language war elsewhere. If people keep arguing, though, I'll clean up what I can here and lock it. – Brad Larson Aug 18 '12 at 21:39
  • @BradLarson I am so sorry. Getting this war and fighting was unexpected. In my opinion, this question is not about C vs. C++, apparently people interpret it that way... :( – Ali Aug 18 '12 at 21:42
  • @BradLarson: Thank you SOOO much. I'm sick of this argument. I was wondering when a diamond mod would get involved. I half wish you could disable comments on this question... – Linuxios Aug 18 '12 at 21:53
  • @BradLarson: By the way, you were elected this year, right? Congrats! – Linuxios Aug 18 '12 at 21:53
  • @Linuxios OK, turns out I asked my question in a wrong way, see my answer at the end of the post. Anyway, I accepted your answer. And I hope we can forget this question ever happened, I am tired... :( – Ali Aug 18 '12 at 23:44
  • @Ali: Heartily agreed! Lets hope next time we interact, it will be in a better context. – Linuxios Aug 19 '12 at 00:45
9

Why certain kernels refuse C++ code in their code base? Politics and preference, but I digress.

Some parts of modern OS kernels are written in certain subsets of C++. In these subsets mainly exceptions and RTTI are disabled (sometimes multiple inheritance and templates are disallowed, too).

This is the case too in C. Certain features should not be used in a kernel environment (e.g. VLAs).

Outside of exceptions and RTTI, certain features in C++ are heavily critiqued, when we are talking about kernel code (or embedded code). These are vtables and constructors/destructors. They bring a bit of code under the hood, and that seems to be deemed 'bad'. If you don't want a constructor, then don't implement one. If you worry about using a class with a constructor, then worry too about a function you have to use to initialize a struct. The upside in C++ is, you cannot really forget using a dtor outside of forgetting to deallocate the memory.

But what about vtables?

When you implement a object which contains extension points (e.g. a linux filesystem driver), you implement something like a class with virtual methods. So why is it sooo bad, to have a vtable? You have to control the placement of this vtable when you have certain requirements in which pages the vtable resides. As far as I recall, this is irrelevant for linux, but under windows, code pages can be paged out, and when you call a paged out function from a too high irql, you crash. But you really have to watch out what functions you call, when you are on a high irql, whatever function it is. And you don't need to worry, if you don't use a virtual call in this context. In embedded software this could be worse, because (very seldomly) you need to directly control in which code page your code goes, but even there you can influence what your linker does.

So why are so many people so adamant of 'use C in kernel'?

Because they either got burned by a toolchain problem, or got burned by overenthusiastic developers using the latest stuff in kernel mode.

Maybe the kernel-mode developers are rather conservative, and C++ is a too newfangled thing ...


Why are exceptions not used in kernel-mode code?

Because they need to generate some code per function, introduce complexity in a code path and not handling an exception is bad for a kernel mode component, because it kills the system.

In C++, when an exception is thrown, the stack must be unwound and the according destructors must be called. This involves at least a bit of overhead. This is mostly negligible, but it does incur a cost, which may not be something you want. (Note I do not know how much a table base unwind does actually cost, I think I read that there is no cost when no exception is running, but ... I guess I have to look it up).

A code path, which cannot throw exceptions can be much easier reasoned about, then one which may. So :

int f( int a )
{
   if( a == 0 )
      return -1;

   if( g() < 0 )
      return -2;
   f3(); 

   return h();
}

We can reason about every exit path, in this function, because we can easily see all returns, but when exceptions are enabled, the functions may throw and we cannot guarantee what the actual path is, that the function takes. This is the exact point of the code may do something we cannot see at once. (This is bad C++ code, when exceptions are enabled).

The third point is, you want user mode applications to crash, when something unexpected occurs (E.g. when memory runs out), a user mode application should crash (after freeing resources) to allow the developer to debug the problem or at least get a good error message. You should not have a uncaught exception in a kernel mode module, ever.

Note that all this can be overcome, there are SEH exceptions in the windows kernel, so point 2+3 are not really good points in the NT kernel.


There are no memory management problems with C++ in the kernel. E.g. the NT kernel headers provide overloads for new and delete, which let you specify the pool type of your allocation, but are otherwise exactly the same as the new and delete in a user mode application.

Christopher
  • 8,912
  • 3
  • 33
  • 38
  • +1 Thanks for your efforts to answer my question. How could you post an answer after the question had been closed??? – Ali Aug 18 '12 at 20:22
3

I don't really like language wars, and have voted to close this again. But anyway...

Well, there are a couple of things. First, you have to remember that the STL is completely built on OS routines, the C standard library, and dynamic allocation. When your writing a kernel, there is no dynamic memory allocation for you (you're providing it) there is no C standard library (you have to provide one built on top of your kernel), and you are providing system calls. Then there is the fact that C interops very well and easily with assembly, whereas C++ is very difficult to interface with assembly because the ABI isn't necessarily constant, nor are names. Because of name mangling, you get a whole new level of complication.

No, with C++ you can declare functions having an extern "C" (or optionally extern "assembly") calling convention. That makes the names compatible with everything else on the same platform.

Then, you have to remember that when you are building an OS, you need to know and control every aspect of the memory used by the kernel. In C++, there are quite a few hidden structures that you have no control over (vtables, RTTI, exceptions) that would severely interfere with your work.

You have to be careful when coding kernel features, but that is not limited to C++. Sure, you cannot use std::vector<byte> as the base for you memory allocation, but neither can you use malloc for that. You don't have to use virtual functions, multiple inheritance and dynamic allocations for all C++ classes, do you?

In other words, what Linus is saying is that with C, you can easily understand the assembly being generated and it is simple enough to run directly on the machine. Although C++ can, you will always have to set up quite a bit of context and still do some C to interface the assembly and C. Another reason is that in systems programing, you need to know exactly how methods are being called. C has the very well documented C calling conventions, but in C++ you have this to deal with, name mangling, etc.

Linus is possibly claiming that he can spot every call to f(x) and immediately see that it is calling g(x), h(x), and q(x) 20 levels deep. Still MyClass M(x); is a great mystery, as it might be calling some unknown code behind his back. Lost me there.

In short, it's because C++ does things without you asking.

How? If I write a constructor and a destructor for a class, it is because I am asking for the code to be executed. Don't tell me that C can magically copy an object without executing some code!

Per @Josh's comment bellow, another thing C++ does behind your back is constructors and destructors. They add overhead to enter and exiting stack frames, and most of all, make assembly interop even harder, as when you destroy a C++ stack frame, you have to call the destructor of every object in it. This gets ugly quickly.

Constuctors and destructors do not add code behind your back, they are only there if needed. Destructors are called only when it is required, like when dynamic memory needs to be deallocated. Don't tell me that C code work without this.


One reason for the lack of C++ support in both Linux and Windows is that a lot of the guys working on the kernels have been doing this since long before C++ was available. I have seen posts from the Windows kernel developers arguing that C++ support isn't really needed, as there are very few device drivers written in C++. Catch-22!


Are the exception handling and the memory allocation the only points where C++ apparently lacks tool support (in this context)?

In places where this is not properly handled, just don't use it. You don't have to use multiple inheritance, dynamic allocation, and throwing exceptions everywhere. If returning an error code works, fine. Do that!

To fix the exception handling issue, one has to provide bound on the time till the exception is caught after it is thrown?

No, but you just cannot use application levels features in the kernel. Implementing dynamic memory using a std::vector<byte> isn't a good idea, but who would really try that?

Could you please explain me why the memory allocation is an issue? How can one overcome this issue, what has to be done?

Using standard library features depending on memory allocation on a layer below the functions implementing the memory management would be a problem. Implementing malloc using calls to malloc would be just as silly. But who would try that?

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
  • +1 and thanks! Yes, it seems to turn into a language war... Perhaps I should change the title and edit the question to make it clear that I am not interested in a C vs. C++ debate. Could you also answer the 3 questions that I ask at the end of the post? – Ali Aug 18 '12 at 20:03
  • In your 3d quote from my answer, you didn't even hear a thing I said. I never said *anything* about call stacks or methods calling methods. I was referring to the fact that C code looks a lot like the corresponding assembly, while C++ looks nothing like it. – Linuxios Aug 18 '12 at 20:14
  • Also, we're not arguing standard libraries here. I have no doubt that the C++ lib is much more powerful than the C one, on the other hand, neither's libs can be used when programing a kernel. – Linuxios Aug 18 '12 at 20:16
  • *To me* C++ code looks a lot like the underlying machine code. I do some chess programming where performance is everything, and C++ works a lot better than C. Some people just don't believe that, like Linus, but I just can't help that. – Bo Persson Aug 18 '12 at 20:17
  • @BoPersson: There are also plenty of kernels written in C++, I was just answering the question. – Linuxios Aug 18 '12 at 20:20
  • @Linuxios and Bo It is turning into a C vs. C++ language debate. :( Quite frankly, I am little happy that the question has been closed because people are fighting and not paying attention to the 3 questions at the end of the post... :( – Ali Aug 18 '12 at 20:27
  • @ali: I'm trying to answer your question. Take a look at the thread after my answer and you'll see. I'm a happy user of both C and C++ and only trying to answer the question. – Linuxios Aug 18 '12 at 20:28
  • @ali: the reason is that the three at the end aren't clear. If you could make them a little clearer, I'd be happy to try. – Linuxios Aug 18 '12 at 20:29
  • @Linuxios Please give me time to read through all the answers and comments and digest a bit. This question generated an unexpected and HUGE amount of feedback. Thank you for your efforts, and I will get back to you on this subject. – Ali Aug 18 '12 at 21:16
  • 1
    Bo, for the constructors and destructors, its not so much about adding code, its that you can't stop them. AFAIK, if you declare a struct instance on the stack, then you call its default constructor (at minimum) and default destructor (at minimum). Obviously you are talented and could do the low-level stuff in C++, but for "mere mortal" programmers, there is too much that is not obvious, and its hard for every programmer on a team to know what everything does. Even the operator= can be overloaded, so something that looks simple, could actually be complex. Your info is good though. – Josh Petitt Aug 18 '12 at 22:39
  • @Josh - Obviously, I fail to see the problem. :-) If you have code in a constructor or destructor, it is because that code *has* to execute, so in C you will have to call it anyway, like `init(&x)` or `free(x.ptr)`. Here C++ even saves you from the bug of forgetting the call, it's automatic. And in C, when you call `fopen("some_file")` you have no idea how much code is executed, but that doesn't seem to be a problem? A bit odd, I think. – Bo Persson Aug 19 '12 at 07:09