Thread: C++ for a low-level language

  1. #16
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    AFAIK you cant use system() calls in C++, not that its all that important.

  2. #17
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by mike_g View Post
    AFAIK you cant use system() calls in C++, not that its all that important.
    Did I miss something? I don't see how this has anything to do with this thread?
    And yes, you can call any C function in C++.

  3. #18
    Registered User
    Join Date
    Jul 2003
    Posts
    110
    Quote Originally Posted by iMalc View Post
    Returning from main also exits the program so there is no difference.
    I completely agree. Conceptually, main gets called by startup code like this:

    Code:
    exit(main(argc, argv));
    I just can't figure out where I have said anything different. I'm not even sure we are discussing the same thing anymore.

  4. #19
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'll throw my 2p in the collection:

    There is nothing you can do in C, that can't be done in C++, that is useful in an operating system (or any other low-level code). The company I work for has the entire OS written in C++ (and yes, it's C++, not just C compiled with a C++ compiler).

    The OS I work on is a mid-to-high-level Embedded OS, so it has low-level funcitonality in the OS for sure.

    There are things in C++ that can't be done in C that may be useful (such as using destroctors as unlock for locking primitives - so you don't have to remember to end your function with "unlock(lock)" at all exit points).

    The main reason that OS's traditionally hasn't been written in C++ is the lack of support for C++ compilers ten-fifteen years ago, and lack of skilled C++ programmers that are also skilled OS developers.

    You can not write a complete OS without some assembler, however. There are a few things you can't do in (standard-ish) C or C++:
    - Context switch (save registers, switch stack, restore registers)
    - Interrupt setup (usually, you have to write to some special register(s))
    - MMU setup (again, special processor registers need to be accessed)
    - Lock primitives may need special assembler instructions.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #20
    Registered User
    Join Date
    Jul 2003
    Posts
    110
    Quote Originally Posted by matsp View Post
    The OS I work on is a mid-to-high-level Embedded OS, so it has low-level funcitonality in the OS for sure.
    Are exceptions enabled? Perhaps that isn't an issue in a mid-to-high-level OS, I'm simply curious.

  6. #21
    Registered User
    Join Date
    May 2006
    Location
    Berkshire, UK
    Posts
    29
    I would have thought that exceptions were one of the main features that would make C++ such a great language for writing an OS in - much easier to follow.
    Michael S-R

    Fedora F7 x86_64

    www.steinbeck-reeves.co.uk

  7. #22
    Registered User
    Join Date
    Jul 2003
    Posts
    110
    Quote Originally Posted by michaels-r View Post
    I would have thought that exceptions were one of the main features that would make C++ such a great language for writing an OS in - much easier to follow.
    A great feature indeed. I'm serious. Sometimes it's not worth the expense though. I've seen a single throw/catch add over 2.5K to the code size on a PowerPC. I believe it was an up-front price that you could leverage as you added more (similar to a full-featured printf cost), but it really depends on how much code space you can afford to spend for that. It's another hidden mechanism that I don't have to account for when I write a program in C.

    Additionally, I can't throw from system space to user space if my OS supports address spaces, or from interrupt to mainline code, so I end up having to return an error code anyway. Of course the answer is that C++ supports all of that stuff too, but exceptions are always on in standard-ish C++. If I can't afford the exception mechanism, and I can't use implementation extensions to disable them, then I'm forced back to C.
    Last edited by whoie; 08-30-2008 at 12:30 AM.

  8. #23
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I say bah to the whole standard-ish of C++ using exceptions, because quite frankly, they can be quite expensive, so it's a trade-off. This means using returns doesn't necessarily make it C.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #24
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    I agree, Elysia: it's a trade-off. A fair few compilers allow exception handling support to be turned off though, and some recent ones only give significant runtime overhead if an exception is actually thrown.

    I seem to recall seeing a mention of one compiler that would turn off exception support for functions declared with extern "C" too, but never confirmed that was true.

  10. #25
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by grumpy View Post
    I agree, Elysia: it's a trade-off. A fair few compilers allow exception handling support to be turned off though, and some recent ones only give significant runtime overhead if an exception is actually thrown.
    So true. This fits the bill of VC exactly. Exception overhead is huge, but only if thrown.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #26
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    This fits the bill of VC exactly. Exception overhead is huge, but only if thrown.
    No, Win32's MS ABI is actually the only (I think) remaining fairly modern system that does have overhead when no exception is thrown, since it bases the exception off SEH.

    I think this changed in Win64.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #27
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by whoie View Post
    Are exceptions enabled? Perhaps that isn't an issue in a mid-to-high-level OS, I'm simply curious.
    It does, where it makes sense to do so. Many functions return an error code to indicate failure, instead of throwing.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #28
    Registered User
    Join Date
    Jul 2003
    Posts
    110
    Quote Originally Posted by matsp View Post
    It does, where it makes sense to do so. Many functions return an error code to indicate failure, instead of throwing.
    Do you have to use nothrow specifications in order to prohibit a well-intentioned maintenance programmer from introducing them into parts that don't make "exception sense"?

  14. #29
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by whoie View Post
    Do you have to use nothrow specifications in order to prohibit a well-intentioned maintenance programmer from introducing them into parts that don't make "exception sense"?
    Actually, there is a naming convention that indicates whether a function is throwing or not - and a tool to verify that a "no throw" function never calls a "throw" function.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange loop
    By D@rk_force in forum C++ Programming
    Replies: 22
    Last Post: 12-18-2004, 02:40 PM
  2. "lower level language"...??
    By matheo917 in forum C++ Programming
    Replies: 3
    Last Post: 04-12-2002, 03:52 PM
  3. Low level debuggers
    By Korn1699 in forum C++ Programming
    Replies: 8
    Last Post: 03-27-2002, 01:39 PM
  4. Looking low level tcp/ip tracker program
    By Hoxu in forum Windows Programming
    Replies: 3
    Last Post: 01-07-2002, 11:46 PM
  5. Visual J#
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 10-08-2001, 02:41 PM