3 ms·
Comparing C plus Python to C++ doesn't make any sense. The comparison would be C plus Python vs C++ plus Python. The C++ solution would most likely be shorter
by kingkongrevenge 18y ago
Comparing C plus Python to C++ doesn't make any sense. The comparison would be C plus Python vs C++ plus Python.
The C++ solution would most likely be shorter, more flexible, and more secure. Personally, I fail to understand why anyone would choose to write new C over C++.
- KirinDave 18y agoIs this really true? I've never really felt that C++ was buying me a lot of brevity with its features. It was more a subject of manageability.
- kingkongrevenge 18y agoModern C++ almost eliminates manual memory management. STL along with tr1::shared_ptr makes memory management almost as easy as with scripting languages. This alone trounces C.
- qwph 18y agoThere's no law that states that you have to litter your code with calls to malloc()/free() when writing C. If reference counting won't do, there's more than one garbage collector freely available, and they're not all that hard to implement from first principles if the mood takes you.
- timr 18y agoOkay, so now you're writing your own memory management. How much work are you willing to do to avoid a language which gives you the feature for free?
- qwph 18y agoIt was an interesting exercise. (It gratified my intellectual curiosity, you might say...) How do you suppose memory management works in high-level languages? Magic?
- timr 18y agoNo. Everyone knows that there are little memory gnomes that run around your system, sweeping up bits.
- tptacek 18y agoThis is exactly what you really do get in garbage-collected programs.
- timr 18y agoDude...seriously: there are garbage collectors for C++. They're not exclusive to C! Moreover, the C++ garbage collectors are pretty darned easy to use, given that the language builds in support for custom allocators, overriding new/delete, etc.
- tptacek 18y agoThe fact that it is trivially simple to garbage collect both C and C++ code neutralizes the "automatic memory management" advantage, dubious as it was, from C++.
- timr 18y agoNot really. One reason that you infrequently see C++ programs using garbage collection, is because the language includes some nice tools that mitigate many of the pain points that drive people to use GC in the first place. If you're using C++ as its own language, you get many of the nice things that come with memory management in dynamic languages, but you don't have to pay for it at runtime (the inclusion of shared_ptr in C++0x is a great example of this, actually). If you merely use C++ as "C with classes" (I'm not saying that you do this...just generally), then you don't see this benefit of the language.
- tptacek 18y agoIt's weird that you think you don't pay for the C++ Standard Library's implementation of reference counting at runtime.
- tptacek 18y agoI don't know, but when I want nice memory management in C code (and usually, if I'm writing something in C, it's at least partly because I care a lot about how memory is managed), I just use Boehm GC.
- timr 18y agoOkay, so you've introduce a third-party dependency. Again, how much work are you willing to do to duplicate a language feature that C++ gives you for free?
- tptacek 18y agoOh give me a break. Until the new standard hit, practically everything that made C++ bearable was a third-party dep on Boost. And the difference between Boost and Boehm GC is, I don't have to read documentation or even change my code to use Boehm. "Avoiding third party deps". The dev team that embraced that standard sure sounds like a blast to work on. The wheel I can't wait to reinvent? Zlib.
- kingkongrevenge 18y agoComparing a third party GC library to TR1 stuff supported by many compiler vendors and vetted by the committee is a bit of a stretch.
- tptacek 18y agoComparing the most famous garbage collection library of all time to the output of a standards body is not a stretch, and now we're into "some third party deps are good --- the ones that support my argument --- and some are bad --- the ones that support yours". I'm happy to consider us stalemated and move on.
- kingkongrevenge 18y ago> I'm happy to consider us stalemated Somehow I doubt it.
- DarkShikari 18y agoIf you're peppering your code with malloc/free, it has nothing to do with not "writing your own memory management"--your code is just badly designed to begin with.
- KirinDave 18y agoOne word: "Greenspun"
- tptacek 18y agoPass a shared pointer to a library that doesn't know how to handle it (read: almost any third-party library). Now you've got two pointers to the same block of memory under two different memory management regimes. What could possibly go wrong?
- kingkongrevenge 18y agoYou can't accidentally pass a shared pointer to a library function in C++. You'd get a compiler error. Then you'd pass the pointer within the smart pointer and make sure the case was handled appropriately. It's not hard and it's definitely still worth it to liberally use shared_ptr.
- tptacek 18y agoYou can't "accidentally" do it; it's just that the only way to actually do it in practice is fundamentally unsafe. "It's not hard"? You know what else isn't hard? Making sure you don't treat an integer as signed in one place and unsigned in another. There's $1.5Bn USD of "not hard", right there.
- anewaccountname 18y ago>You can't accidentally pass a shared pointer to a library function in C++. Spoken like someone who has never used a template function.
- qwph 18y ago(Speaking as one of those people who did choose to write C over C++...) People choose C over C++ because C is a simpler language than C++, something which this new standard does nothing to address.
- kingkongrevenge 18y agoI have difficulty handling the complexity of typical C code due to the lack of abstraction. C always winds up with huge functions and lots of hard to grasp state up in your face. C++ gives you way more opportunities to hide away complexity.
- qwph 18y agoC always winds up with huge functions and lots of hard to grasp state up in your face. That's something I've never found to be the case. Most of my C functions are 5-15 lines long. Anything more than 20 lines long is a candidate for refactoring - the unit tests will tell you if you've broken anything.
- tptacek 18y agoThe solution to complexity is to eliminate it, not to hide it. You've nailed the big problem with C++ right there.
- kingkongrevenge 18y agoI really don't know what that means...
- earthboundkid 18y agoThink of it like this: Would you rather have an operating system that has a bunch of little ads in the corner by the clock that are periodically "hidden" under a chevron, or would you rather that the crap wasn't there in the first place?
- 18y ago
- tptacek 18y agoThe C++ version will absolutely not be more secure; we find just as many vulnerabilities in "modern" template-y C++ code as we do in straight up C. We know how to secure C code, even if we are bad at it. Nobody knows anything about C++ security. http://www.matasano.com/log/914/c-a-cautionary-tale-or-1-hour-of-your-black-hat-trip-is-spoken-for/ http://www.matasano.com/log/914/c-a-cautionary-tale-or-1-hou...