Slashdot Mirror


Someone on Medium Just Said C++ Was Better Than C (medium.com)

Developer David Timothy Strauss is publishing a call to code "straightforward, easy-to-reason-about approaches" -- in an essay titled "Choosing 'Some C++' Over C". (Alternate title: "C++ for Lovers of C." The problem with just picking C++ is that most criticism of it is legitimate. Whether it's the '90s-era obsession with object orientation and exceptions or the template errors that take up an entire terminal window, there have been -- and remain -- rough edges to C++. But, these rough edges are avoidable, unlike the problems in C that get worse with modern event and library programming. The opinionated essay calls for "adopting a subset of C++ to smooth out C's rough edges," arguing that C++ offer a better, type-safe approach for event-driven design (as well as destructors to avoid memory allocation leaks). Are there any readers who'd like to weigh in on the advantages of C versus C++?

10 of 315 comments (clear)

  1. Indeed by hcs_$reboot · · Score: 5, Funny

    "C++" > "C" (as long as > has not been overloaded..)

    --
    Slashdot, fix the reply notifications... You won't get away with it...
    1. Re:Indeed by allo · · Score: 4, Funny

      In fact, it is not. Try it yourself:

      #include <stdio.h>
      int main(int argc, char **argv) {
          int C=0x11;
          if(C++ > C) {
              printf("C++ is greater than C\n");
          } else {
              printf("C++ is not greater than C\n");
          }
      }

      $ ./a.out
      C++ is not greater than C

    2. Re: Indeed by Anonymous Coward · · Score: 5, Funny

      Trick question. There is only one ada programmer.

    3. Re:Indeed by TsuruchiBrian · · Score: 4, Informative

      It also has good features, many of which provide you with an alternative to the bad features of C.

      Constructors/destructors (RAII) > manual initialization/deinitialization.
      Smart Pointers (made possible by RAII) > raw pointers
      Polymorphism > function pointers
      Templates > macros

      If you are subscribing to a streaming movie service and you have the choice between netflix and a site that only allows you to watch "Armageddon (1998)", does it make sense to choose the latter because netflix has more bad movies? No of course not, because you don't have to watch the bad movies on netflix, and you can even choose only to watch movies better than Armageddon.

      C++ is netflix. Nobody watches all the movies. Everyone watches the movies that are good from their point of view (even though many of those people are just wrong).

  2. I use awk by jlowery · · Score: 5, Funny

    enough sed. don't bash me.

    --
    If you post it, they will read.
  3. Re:They both suck! by phantomfive · · Score: 4, Interesting

    What particularly don't you like about C syntax? I've always thought that the main security problem with C are caused by the lack of a decent buffer processing library, and a lousy string processing library. Fix those two things (and any person can do it in their own code!) and you've fixed the vast majority of C security bugs.

    --
    "First they came for the slanderers and i said nothing."
  4. Undefined behavior by Immerman · · Score: 5, Informative

    Not quite, it is in fact undefined.

    C++ is the post-increment operator, it increments the variable, but then returns the original value. Therefore, since C started out as 0x11, C++ will evaluate to 0x11 while modifying C to be 0x12 as a aside effect.

    Therefore, if you were > optimistic you could try to claim that "C++ < C", expecting the operations to be evaluated left-to-right and thus be evaluated as "0x11 < 0x12". However, C++ doesn't specify the evaluation order of operators, which means that "C" might end up being evaluated before "C++", in which case the comparison would be evaluated as "0x11 < 0x11" instead. The only thing you can be sure of is that C++ will NOT be greater than C.

    Basically, as a rule of thumb you should never modify a variable within a line of code if the value of that variable will matter anywhere else within that same line. http://en.cppreference.com/w/c... - discusses undefined behavior halfway down the page.

    --
    --- Most topics have many sides worth arguing, allow me to take one opposite you.
  5. Re:Actually what the guy wrote was by Dutch+Gun · · Score: 5, Insightful

    People who know and like C will continue to use it. Giant legacy projects written in C (like the Linux kernel) will continue to use C, of course. C++ 11, IMO, is not going to convince C programmers to switch. Rather, it's a love letter to existing C++ programmers due to the radical way it transformed the language.

    It's still ugly as hell, and has sharp corners that will slice your fingers and toes off if you're not careful (we ARE talking C++ here). Even so, for the first time, using just the core language and libraries (Boost doesn't count), I can completely avoid manual memory management in 99% of the cases, almost like using a garbage collector, but with the advantages of destructors for non-memory resources. Raw pointers are almost a thing of the past, except for some very rare exceptions, and move semantics means your APIs can look like you always wanted them to while still remaining efficient. It's hard to describe how different the language feels pre and post 11.

    In my opinion, C++ 11 simply makes C++ far better at creating large, high-performance libraries and applications (in my case, videogames) because of the confidence that robust ref-counted resource management gives you.

    --
    Irony: Agile development has too much intertia to be abandoned now.
  6. I worked on a C++ device driver by AaronW · · Score: 5, Interesting

    In the 1990s I worked on a complex device driver for OS/2 for ATM networking (asynchronous transfer mode). The driver was around 100K lines of C++ code however only a subset of C++ was used. Surprisingly the use of C++ worked out quite well. We had an equivalent driver for Windows NT that was written in C that was over 300K lines of code. The C++ codebase was a lot more reliable and easier to work on, despite all the work that was done to make C++ work in kernel mode under OS/2. The C++ driver actually had more functionality than the C driver and it was faster as well with a smaller binary. Also, as I said, it was a subset of C++, especially in the performance critical code. The driver in question included the entire ATM signalling stack and implemented full LAN emulation support with both Ethernet and Tokenring plus it could emulate multiple networks (ELANs, equivalent to VLANs) simultaneously. When I implemented multiple ELAN support I was afraid it was going to be a nightmare, but due to the way it was architected in C++ I ended up only having to change a few lines of code, changing a pointer to the LANE class to an array of pointers.

    For the signalling stack and ILMI C++ worked out especially well due to the event and message based nature of it and the various state machines. For LAN emulation it made it easy to support both Ethernet and Tokenring by having a few virtual functions in the main LANE class.

    There was no exception handling support and none of the more complex C++ features were not used. It used templates but that was also somewhat limited.

    Having destructors was also quite nice, making it easy to clean up resources.

    Despite being C++, debugging wasn't too bad though at the time the OS/2 kernel debugger basically ran at the assembly level.

    If the infrastructure is in place I can certainly see using C++ in the kernel and device drivers.

    --
    This post is encrypted twice with ROT-13. Documenting or attempting to crack this encryption is illegal.
  7. Re:Actually what the guy wrote was by swillden · · Score: 4, Informative

    In exchange for manual memory allocation, C++ gives you automatic memory allocation: lots of it.

    Nonsense. You don't get memory allocation unless you ask for it.

    When resources are scarce (eg. IOT devices) this overhead can be a show stopper.

    You're misinformed. With C++11 move semantics, you can have both safe, automatic ownership management, and no unnecessary dynamic allocation. Most of my work is done in a very constrained environment, where I have only a handful of pages of heap... or in some cases none at all. C++ is awesome for such environments.

    Something that C++ advocates seem to ignore there is no free lunch.

    No one is claiming that there is. What there is, is the opportunity to delegate tedious and error-prone due diligence that C programmers have to do themselves to the compiler. For example, you know all those functions that have comments describing whether the returned data structure's contents are owned by the caller or the library? In C++ you can write the function so that it's impossible for the caller to avoid taking ownership when that's what you want, or so that it's impossible for the caller to believe it has ownership when the library is retaining it. If the caller gets it wrong, the compiler will flag the error. That's one example, there are many, many more. C++ enables you to have buffers and strings that do automatic bounds checking... or even to write code such that potential bounds violations are flagged by the compiler, making run-times bounds checks provably unnecessary.

    There's no magic here, just language constructs that allow you to accurately specify the semantics you want, which the compiler can enforce.

    --
    Note to ACs: I usually delete AC replies without reading them. If you want to talk to me, log in.