Thread: C++ for a low-level language

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    266

    C++ for a low-level language

    I know most people call C a low-level language and that made me wonder. Is there any low-level action that C can do that C++ can't? Also, can C++ use the asm("") feature?

  2. #2
    Registered User Kernel Sanders's Avatar
    Join Date
    Aug 2008
    Posts
    61
    With few exceptions all valid C code is valid C++ code, so no, there is nothing that C can do that C++ can't. When people call C low level it is in comparison to languages like perl or java or haskell more so than in comparison to C++

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Right, what he said. C++ is a subset of C. It strives to be more type restricted. Though since even C style casts are allowed in C++, one can even dupe the compiler into not even doing that task.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    > C++ is a subset of C.

    Other way. I made the same mistake on these boards too. heh.

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    If I recall correctly, many years ago there was a thread about exactly what you can do in C that you can't do in C++. Maybe I'm crossing my mind and it was actually a thread about what you can do in ASM that you can't do in C, but let's just assume I'm right, okay?

    I think they came up with a list of 5 things, and they were all REALLY low-level things that you'd probably only do when writing an OS - stuff like that. But for most intents and purposes, C++ really is a superset of C.

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Righto, I meant superset, I swear :P

    There are plenty of things one can do in assembler that cannot be done in C without the usage of inline assembler. There are a few differences, actually. But they are more or less issues of syntax than anything. i.e.

    Example:
    Code:
    /* C style prototype */
    void main();
    // C++ style prototype
    void main();
    Mean two different things. In C, that functionally means void main(...) whereas in C++ it means void main(void).

    Those types of things hardly qualify as what you are talking about, however.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by master5001 View Post
    In C, that functionally means void main(...)
    No, that means variable arguments. main() means unspecified arguments.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    master5001, you choose a very unfortunate example: main() returns an int and should not be prototyped.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Why not?

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Why not?
    hmm... looks like I was wrong about the no prototyping thing: C99 states that the implementation shall not declare a prototype for main(), not that main() itself may not be prototyped, and C++03 states that the implementation shall not predefine the main() function. On the other hand, the very fact that main() may not be used within one's program means that there is no reason to declare a prototype for main().
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Jul 2003
    Posts
    110
    Speaking of main()...

    I believe you can safely return from main() in C to the startup code without worrying about destructors being called for objects with internal or external linkage.

    I only bring this up because it is done in tightly constrained embedded programs to save a stack frame. main() contains the startup code and system initialization, but it returns to the startup code to sleep until an interrupt occurs. I don't believe you can safely do that in C++.

    I could be wrong though, because I think we step out of the "hosted environment" scope there. Perhaps C++ doesn't technically need to suffer that restriction. I'd be interested to hear if someone has looked at that before.

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by whoie View Post
    I believe you can safely return from main() in C to the startup code without worrying about destructors being called for objects with internal or external linkage

    I only bring this up because it is done in tightly constrained embedded programs to save a stack frame. main() contains the startup code and system initialization, but it returns to the startup code to sleep until an interrupt occurs. I don't believe you can safely do that in C++.

    I could be wrong though, because I think we step out of the "hosted environment" scope there. Perhaps C++ doesn't technically need to suffer that restriction. I'd be interested to hear if someone has looked at that before.
    You can terminate your program abruptly by other means in C++. It's called 'exit' iirc. You don't have to let your program end gracefully.
    Or you could avoid globals or statics with destructors, and place the entire body of main in an extra scope guard.

    The lack of destructors in C is a burden though, not an advantage.
    Last edited by iMalc; 08-28-2008 at 12:59 AM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  13. #13
    Registered User
    Join Date
    Jul 2003
    Posts
    110
    Quote Originally Posted by iMalc View Post
    You can terminate your program abruptly by other means in C++. It's called 'exit' iirc. You don't have to let your program end gracefully.
    Or you could avoid globals or statics with destructors, and place the entire body of main in an extra scope guard.
    Perhaps I failed to make myself understandable. The point is that you want to return from main, or 'exit', without really ending the program. It's just a trick to save a few bytes of memory. I'm just pointing out one of C++'s hidden mechanisms that can throw up obstacles when you want to do some "low-level" stuff. I'm not even sure I'm right in a non-hosted environment. I'm just bringing something up to talk about .

    Also, I didn't mean to suggest that destructors were bad, just that they are one issue that you don't have to work around in C. I think they are great, until you don't want them.

  14. #14
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    C/C++ are actualyl considered mid level languages by most old timers. A low level language is more like assembly, i.e. just above machine code. I woudl say that C++ is slightly higher than C, but its not really a high level language either. IMO it really depends on teh seperation from teh hardware. C/C++ are both very close to the hardware, although not as close as assembly. Perl etc are very abstracted langauges and are considered high level.

    But truthfully, these definitions are arbitrary and meaningless. whether you define C/C++ as a low level mid level high level or grapefruit level language has no effect on what it can and cant do.
    Last edited by abachler; 08-28-2008 at 11:45 AM.

  15. #15
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by whoie View Post
    Perhaps I failed to make myself understandable. The point is that you want to return from main, or 'exit', without really ending the program. It's just a trick to save a few bytes of memory. I'm just pointing out one of C++'s hidden mechanisms that can throw up obstacles when you want to do some "low-level" stuff. I'm not even sure I'm right in a non-hosted environment. I'm just bringing something up to talk about .

    Also, I didn't mean to suggest that destructors were bad, just that they are one issue that you don't have to work around in C. I think they are great, until you don't want them.
    Returning from main also exits the program so there is no difference.

    The obstacle is the fact that in C you have to clean up manually, and not forget to do so. It is better to have to say "No I don't want to clean up this one time", than it is to have to say "And please clean this thing up for me" zillions of times (and try not to forget coding them all).
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

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