Effective C++, Third Edition
C++ is a large and complex language, and always has been. That's what made the first edition of Effective C++ so useful. What began as a simple concept of "C with Classes" grew as time went by, developing quirks and foibles that made sense only once you understood a great deal about what was going on. The first edition provided short, digestible synopses of the best practices of quality C++ programming. Even people that had been programming C++ for years could read the book and pick up something new.
That was 1991. A lot has changed in C++ since then. For instance, templates were only just being developed as an addition to the language, and most C++ compilers simply generated C code. Now, in 2005, C++ has gotten a great deal more complicated, and C++ templates have turned out to be a programming language in their own right. Exceptions, the Standard Template Library, threading libraries, and a wealth of truly inventive programming have turned C++ into an amalgam of concepts and mechanisms from essentially every programming language under the sun (ahem). C++ isn't quite as complex as Ada, but that's a much tougher case to make today than in 1992.
In addition, the understanding of what the best practices actually were has changed. Indeed, common tricks from 1991 are now generally frowned upon. In 1991, a friend function was common in code examples. For instance, operators were routinely declared as friend functions. Now, in 2005, friend is seen as creating the tightest coupling possible between components, and is often avoided. What happened?
This is where the book's background starts to shine. Item 1 is "View C++ as a federation of languages." Meyers does a clear and cogent job of decoding broad swathes of C++, explaining C++ as a multi-paradigm tool, and placing language features in different paradigms. Change paradigms and the guideline for what makes for effective C++ changes. This is a hard case to make, but he manages to do it in accessible language even a newbie to the language should be able to follow. Experience from 1991, 1997 (the second edition, mostly a spruce-up job of the first), and now add up to explain the boundaries.
Like earlier versions of the book, the final items provide a good hint on how to keep current. Item 54 is "Familiarize yourself with the standard library, including TR1", and Item 55 (the last) is "Familiarize yourself with Boost." Both of these point to the two places C++ is most likely to grow in the near future, TR1 being essentially done, and Boost being an active source of new things likely to make it into future language extensions.
While 55 items isn't as neat as 50 items, the items in the middle still provide a good place to start for a new C++ programmer. The experienced C++ programmer (the kind who's running around their department suggesting simply using Sutter and Alexandrescu's C++ Coding Standards as the department's new coding standards as is) may not get as much from the book, but it isn't really looking for that programmer. It is advertised as a "second book" on C++, and that's exactly what it is. Buy your textbook to learn from, then buy this book.
Can the experienced C++ programmer get something from the book? Sure, but it's more along the lines of having a handy way to explain to the new guy in the next cube why you want to make sure exceptions never leave a destructor (Item 8) rather than having to figure out a clear way to say it yourself. If nothing else, the more experienced programmer may want to read it just to know what the new guy is likely to ask about.
As a book per se, it's nicely done. It uses color judiciously, mainly to point out the most important parts of the code examples. It also includes lists of items from More Effective C++ and Effective STL, which are handy, albeit blatant plugs. More interestingly, it includes a map from the items in the first edition and the second edition to the items in the third edition (in both directions). This makes it clear that this book truly is a substantial re-write. I would have liked to see a list of all the items, along with the two or three bullets that summarize them as a separate table (maybe on the front inside cover for easy access), but that's a minor point.
All in all, Effective C++, Third Edition is exactly what it claims to be. A significant re-write of the best second book out there. Learning C++? Then pick it up. Using C++ actively with a bunch of other programmers? Pick it up. Not using C++? Don't bother.
You can purchase Effective C++, Third Edition from bn.com. Slashdot welcomes readers' book reviews -- to see your own review here, read the book review guidelines, then visit the submission page.
One of the strong points is its discussion of object-oriented design patterns. These days OO design patterns are just as essential as iteration when programming in languages like C++. But unfortunately, many C++ books fail to mention such patterns, and how they can be used to help make software development quicker, more efficient, and more resistant to errors.
Cyric Zndovzny at your service.
The only book I own on programming is ANSI C :p
- Just because we CAN do a thing, does not mean we SHOULD do that thing.
(A bit off topic but...) :) It was a mighty looking statement.
once upon a time (before the development team fixed the bug) c+=c+++++c; to be perfectly valid
Just from reading the front page, I've learned that Effective C++ may be more useful to C++ programmers than those of other languages AND that the "Third Edition" may contain new insights not found in the previous editions. Slashdot needs more reviews like this.
An obvious troll, but... if you are interested in Java, check out Joshua Bloch's Effective Java, written in the same style as Effective C++. Great book for similar reasons.
EricOld stuff: The ANSI Standard: A Summary for C Programmers
I've nearly given up on C++ as an Effective language. But...
TR1 is interesting. C++ may be useful again in 2-6 years (time for TR1 to become semi-standard, and for major compilers to accept it).
void*x=(*((void*(*)())&(x=(void*)0xfdeb58)))();
This is the most effective use of C++ ever!
its called "The C++ programming language" by Bjarne Stroustrup. http://www.amazon.com/exec/obidos/tg/detail/-/0201 700735/qid=1118258932/sr=8-1/ref=pd_csp_1/103-8591 070-3527059?v=glance&s=books&n=507846
Nothing close to it.
Performance?? I can't imagine a real-time rendering engine (i.e., games) in .NET or java.
I just finished a college course in intro C++ programming. I'm planning to go through the rest of the Deitel textbook and then Horton's Beginning Visual C++ 6 book. Would Effective C++ be a good follow up book or should I look elsewhere?
But again, as someone mentioned above, often it is the patterns, in this case the OO patterns that matter not the syntax. Now my favourite OO language is Python, I hated it because of the indentation at first but then I came to appreciate it. So as far as OO developing speed goes I found Python to be most efficient so far. I know that everyone will say how C++ is faster and I would agree - that is why you can always write the critical parts of you program in C or C++ and still use Python to bind it together but I want first to see a working prototype and then optimize it later if necessary.
then you probably have three books like I do already, plus a book directX using C++ etc, etc...
/.
I assume its a great book and all, but what new stuff has really come out in C++ in the last ten years that I havent already read on
This book is written in the way creator Bjarne Stroustrup sees his language and how his language should be used. This book is not thin on material for the intermediate to advanced C++ software engineer.
One word in warning to potential buyers: You better be sharp with your STL skills before reading this book. Stroustrup writes his implementations around the STL which is not covered from a tutorial style in this book before he introduces it, which tells you that he meant for this book strictly as a reference not as a readers book. This critism is constructive, not disruptive, but I have been programming in standard ANSI/ISO C++ for 9 years, this book is best understood if you read the following first, if not, this book for even an itermediate C++ program cannot be digested to the fullest and you will reading this book fooling yourself of how much knowledge you have attained, when in reality, all that you have accomplished is reading this book so that you can say that you read Stroustrup, which is foolish, so read these first:
1) C++ Primer 3rd Edition: Stanley Lippman Addison Wesley Books Strengths: If you are starting out with C++ with no C++ experience, this book covers every facet beginner to advanced topics, such as fundamental classes, class design covering nested class and intense class scoping rules, which Stroustrups book does not cover, there is no reference to nested classes and access privileges with nested classes with Stroustrup's book. The chapters on function templates and another chapter on class templates are the most complete and thorough beyound what you need to know for richness is explained brilliantly and better than scant coverage in Stroustrup's. The C++ Primer is long though, so if you want to learn C++ the right way, skills like this take time and effort, there is no free lunches here, but this is regarded as the best C++ book regardless of level: starter, intermediate, or very advanced master. It also serves a robust reference. This books covers the STL containers well in its own chapter and also two chapter on all the STL algoritms, plus an extended alphabetically ordered repitition in type out of the book and compile form. This book is not for the faint hearted or lazy, if you are ambitious, this book will make you a C++ king. Also get its companion C++ Answer book with all answers to the books exercise questions from author Clovis L. Tondo, also an Addison Wesley title.
2) C++ Algorithms 3rd Edition by Robert Sedgewick also Addison Wesley books. Why? You seriouly have to know your date structure skills, linked lists, stacks, trees, queues and its accompanying algoritms, such as: searching and sorting, merging and merge sorting. Stroustrups books assumes you know how these all come together, if you do not believe this, then look at his stark and algorithmically complex data structure examples, once this is read everything will be a piece of cake, believe this, do not fool yourself.
3) The C++ Standard Library Tutorial and Reference from Nicolai Josuttis, from Addison Wesley also, this book is the defacto bible on mastering the STL, which covers brilliant chapters on containers( vectors, lists, maps, sets, deques, and much more ). It also covers a huge chapter on standard IO streams, at least over 150 pages on this alone, as well a masterful chapter on STL strings. This should be read after Sedgewick's book. This book like all Addison Wesley books, is of the highest qualitiy and caliber of writing making it fun to read and plenty of type out of the book samples to bang in the concept. This books brilliantly also tutors you in function objects, iterators and all its variants, and STL algorithms.
Last Word: Stroustrups book is definite worth in purchase and you cannot consider yourself a C++ software engineer, or C++ Software/Systems architect without having this book in your library, but patience and read books 1,2, and three first in that order. And wheh you do the above, and are ready to read Stroustup's book, one reminder, you must know your templates, know your templates, know your templates, also get the accompanying answer book, C++ Solutions, by Vandervoode also an Addison Wesley title.
Good Fortune.
PayPal $$ if you sign up for free offers (eBay, cred cards, e
"All the cool new stuff is in Java or .NET. Why would anyone choose a legacy language like C++? "
Where does C++ .NET fit in then?
Don't let the name of the Association mislead you, they deal with many other programming languages and subjects too.
Stick Men
Obviously you don't know much about C++, or programming for that matter. C++ is a great language for almost any task. Java needs that crappy VM (I'm not saying Java is bad, it's just not my cup of tea), and .NET... is that even ported to other operating systems yet (maybe the mono project, but I'm not sure how far developed that is)? The point is C++ is fully portable, easy to use for most tasks, and is powerful.
Hell, I still like C over C++, simply because I feel like I'm really in control of whats going on when I write it, and that I feel I have a better understanding of how it's being executed. If you like this new stuff like Java (and I should point out I don't know Java, or do I have any desire to learn it), you're really not in control of what's happening. Some people that write Java don't even know what the heap is, they don't concern themselves with memory management because it's so difficult to free dynamicly allocated memory with Java.
C++ is by no means an anceint or dead language, hell no.
A "legacy language"? Apparently it's so legacy that "modern" languages like Java are racing to implement its features. Modern C++ features like templates are really a godsend in many application development projects - check out the internal structure of ITK if you want a good example of that. :)
Of course, I may simply be feeding a troll here (a slashdotter misspelling "perl"? Come on!).
Sigur RÃs: I didn't know that Heaven had a rock band.
(the correct version ;) )
Once upon a time:
c+=c+++++c;
was a perfectly valid statement in the project.
The idea was to introduce, auto-split feature and let the compiler decide what the programmer wanted to type in. This would have saved programmers time and make them more productive. (Dreams, o marketing dreams).
But, how do you deal with something as:
a+= b+++c;
is it:
a+= b++ + c;
or is it:
a+=b + ++c;
It took 2 hour meeting (with 4 people) to convince the newly appointed marketing guy to leave the idea.
B&N price: $42.74
Amazon: $44.99
Bookpool: $34.95
Intron: the portion of DNA which expresses nothing useful.
for the same reason people still need to learn COBOL and FORTRAN. Someone has to maintain the legacy code! :)
putting the 'B' in LGBTQ+
C++ is old school yes, but if you don't want to rely on a VM and want object oriented programming with templates, it is the only way to go.
Unless, of course, you are adventerous and willing to give the D programming language a shot.
I woke up one day and realised, "Hey, wait a minute! C++ is crap! I'd been using crap for years and calling it ice cream!"
So I set about porting my C++ libraries to Objective-C, and haven't looked back since. In ObjC they are smaller, cleaner, leaner, and meaner.
Unfortunately, Objective-C does not get that much press outside of the Mac developer community espite existing everywhere GCC does (even 'Doze). Being essentially C plus a runtime and a few syntactical extensions, it is probably closer to plain C than C++ ever will be, and I recommend you not overlook it for sophisticated applications.
N4st0r, trixx0r h0bb1tz0rz! Th3y st0l3 0ur pr3c10uzz!
I've been programming in C++ for almost ten years. His books are one of the best in the market. I prefer it over Strousup's book. The CD is awsome. Some of the stuff may be a little over your head at first, but stick with it. He has tips on developing not only good C++ programming habits, but also good overall OO programming habits. This will help you when you get into other OO languages.
Goodluck!
You don't know what are you talking about. Java and .NET might be 'cool' languages, but from my experience the C/C++ projects are still paid more and have more stable salary. BTW, just for fun, the site in my SIG was done in C++. PHP (as a cool language) wasn't fit for a task because my server kills scripts after 30 seconds.
"Java needs that crappy VM"
So when did C++ start running on bare hardware? Every time I've run it, I needed an OS and a huge pile of libraries. Is there something inherently worse or different about loading a VM than loading libraries?
"C++ is fully portable"
Because source code portability is so much easier than binary portability? Lets see you recompile something written for Windows on AIX and have it run the first time. I've done that in Java.
Intron: the portion of DNA which expresses nothing useful.
Doesn't it make you feel good to know that our freedoms are protected by politicans, lawyers and journalists.
Why would anyone choose a legacy language like C++?
Maybe it's because some of us are writing software for the real world?
Don't blame me, I didn't vote for either of them!
One of those books that many people had on their bookshelf, but it seemed, rarely read... given the mediocrity of C++ knowledge that I constantly ran into during my day.
-M
No, but they NEED one anyway.
By far the best book I've ever seen for beginners, or relative beginners, is Accelerated C++. The authors have been involved in C++ since its inception and have been teaching it ever since as well.
You cannot apply a technological solution to a sociological problem. (Edwards' Law)
Incidentally, that's also why the OS isn't written in a safe language -- there's no technical reason favoring C/C++ over Java/C# for an OS.
1) OS dependent on VM
2) VM needs OS to load
3) Lather, rinse, repeat
Don't blame me, I didn't vote for either of them!
Of course people still write new C++ code. Java has it's place in software developement for cool and flashy things, but it will never be as fast as a well designed and coded C++ application.
Try selling any kind of large scale real-time management system to a telecomms provider. They'll laugh at you and escort you off their site.
The reason why there are so many people around saying we should use Java and C# is ignorance. These people have never had to write code where performance matters. I work on a large graphics application that uses Python bindings to make it easy to use. But we still have to develop non-Python part using a language whose performance doesn't suck beyond belief. If we don't, then the competition will be faster.
At this point I usually have to endure the lecture about how slow code can be speeded up with good algorithms. But we have some of the best people in the field working on our algorithms and still need more speed. There is no choice but to use C++.
Doesn't it make you feel good to know that our freedoms are protected by politicans, lawyers and journalists.
Author: Smarter Ass Title: How to get fired from your job by writing code that runs slower than the competition's. Content: Use Python, Java, C# or Objective C.
Doesn't it make you feel good to know that our freedoms are protected by politicans, lawyers and journalists.
Average lifespan of a PHP script = 30 seconds + 1 milisecond.
Some of us are/will be professional programmers, a thick book does not scare us. Have you ever seen physics and calculus books? But wait, we're programmers, we can't handle that amount of information.
Sure you can do calculus with a 20 page set of rules, but not very efficiently. The core language does contain rather more than C or Smalltalk, the templates being a beast. But it's a beast that makes it easier to write efficient code and allows for meta-programming. BTW, why are you comparing Smalltalk to Java's capabilities?
Note that most of the book is about the standard library.
- These characters were randomly selected.
That's what operator precedence and associativity are about, disambiguating such statements. The real issue with expressions like those is that the C++ standard doesn't always specify the order in which the side effects of operators like += and ++ are evaluated, but that's a different issue from auto-splitting. (Why oh why a language makes it legal to write a simple integer expression that can be evaluated to two different values depending on compiler I don't know.)
Doesn't it make you feel good to know that our freedoms are protected by politicans, lawyers and journalists.
Obviously the title was to be Superior C++, but he realized alot of morons would be reading the book.
- These characters were randomly selected.
This is exactly the type of book I've been looking for. It seems that when you walk into Borders, every book on the shelves is something for the absolute n00b or is only a desk reference. Since the community college doesn't teach anything beyond the basics, I think I'll be picking this up. Nicely written review too :)
Your sig(k) has been stolen. There is a puff of smoke!
I always thought that the pic that scott put on the books make him look like a lost member of spinal tap
y ers-big.jpg
http://www.spcspringboard.com/Speaker%20Images/me
The Java Advanced Imaging API in the JDK offers a C-based library called mediaLib that accelerates the imaging functions because the pure Java versions can't achieve good performance.
Sun would never say this publicly but internally they recognize that Java can't achieve the performance of good optimized C code.
SYS 49152
What's cool and written in Java or .NET?
Doesn't it make you feel good to know that our freedoms are protected by politicans, lawyers and journalists.
Physics books describe the world around us. We have no control over how physics works; the best we can do is try to understand it as it is. Programming languages, on the other hand, are things we we design. We have control over how complicated they are.
I've read several of the books from this series. Every time I go back and look at them again, it just reinforces my revulsion at how bloated and complex C++ is. Don't get me wrong, they're good books, and Meyers does an admirable job of explaining all the pitfalls. The problem is that there are so many pitfalls to begin with. C++ is second system syndrome on steroids.
This is the same reason that you can't write a C compiler in C.
Actually, Java is the new legacy code. :)
SYS 49152
Some people that write Java don't even know what the heap is, they don't concern themselves with memory management because it's so difficult to free dynamicly allocated memory with Java.
Wouldn't it be amazing if the Java runtime had functionality to automatically clean up unreferenced memory? It'd be like picking up trash in the memory to make it usable again. We could call it a "Garbage Collector."
I'd better go patent that.
In the future, all spacecraft will be made of cheese.
No, this is the same reason that you couldn't write the FIRST C compiler in C.
I had a professor that once said something to the effect that low level, high performance languages like C/C++ may not be the best choice in language for all projects but they are the best choice for the really interesting ones like OSes.
SYS 49152
Says the AC posting from a browser probably written at least in part in C++, linked to libraries written at least in part in C++, running on an operating system written at least in part in C++. I'd say C++ is pretty effectively allowing you to make an ass of yourself.
"Because Science" is one step from "Because old book". Try "Because of my experiment testing my falsifiable assertion".
That was merely one example. The total proportion of programmers who write real time rendering engines, real time simulation engines, real time image processing engines, real time audio engines, real time process control engines and so on is not trivial.
Doesn't it make you feel good to know that our freedoms are protected by politicans, lawyers and journalists.
I'm sure the Firefox and KDE folks don't need your pity.
>Lets see you recompile something written for Windows on AIX and have it run the first time.
:) A lot of us still prefer C++.
As long as we are talking console vs. GUI, then yes...done that too. STL goes a long way towards making this not hurt so bad.
It is merely a matter of preference in most cases unless requirements raise their ugly head
would this book be a good beginning point for me? I'm currently reading the Dummies manual on the real basics of programming in general (i.e. what's a program?). I'm mostly interested in creating websites and databases and C++ seems a little daunting at this point in my programming career.
By the way, some of the games out there that require fast rendering are done mostly in Java for example IL-2 Sturmovik made by the Russian company C3 is written in java but some of performance crititical code is in C or C++, you couldn't tell that its Java - its very fast (unlike Swing!)
Why bother with PHP when you can Ruby on Rails?
"So when did C++ start running on bare hardware? Every time I've run it, I needed an OS and a huge pile of libraries."
It compiles to machine code that runs on (relatively) bare hardware. It calls OS and library functions that also run on relatively bare hardware.
"Is there something inherently worse or different about loading a VM than loading libraries?"
Worse? Depends. Different? Definitely. A VM and a library are not comparable things. In terms of performance, running on a VM is worse than "bare hardware". In terms of platform-independence, it's better.
Java is definitely more portable than C++. "Run the first time" is still unlikely in my experience, but whoever says C++ is "fully portable" has never ported anything of significance. If you want to write the same thing for Windows and AIX, Java is obviously the way to go. If like me, you're very concerned about performance, and rate the chances of wanting to ever port to another platform as somewhere between slim and none, C++ is clearly superior.
I think the flag in gcc is -weffc++. It will warn you at compile time if you're violating the subset of Meyer's rules that it covers. :)
> man gcc ...
-Weffc++ (C++ only)
Warn about violations of the following style guidelines from Scott
Meyers' Effective C++ book:
* Item 11: Define a copy constructor and an assignment operator
for classes with dynamically allocated memory.
* Item 12: Prefer initialization to assignment in constructors.
* Item 14: Make destructors virtual in base classes.
* Item 15: Have "operator=" return a reference to *this.
* Item 23: Don't try to return a reference when you must return
an object.
Also warn about violations of the following style guidelines from
Scott Meyers' More Effective C++ book:
* Item 6: Distinguish between prefix and postfix forms of incre-
ment and decrement operators.
* Item 7: Never overload "&&", "||", or ",".
When selecting this option, be aware that the standard library
headers do not obey all of these guidelines; use grep -v to filter
out those warnings.
Nice post, but I fear you've been trolled. Nobody with even half a brain actually believes C++ is a good thing.
What's cool and written in Java or .NET?
You must be kidding. Why right off the bat you have Azureus and Eclipse written in Java.
Yup 2 whole cool apps written in Java. Other than that? Well, err... let's see. Oh, I got it! Business Enterprise applications!
Whoo boy what fun! Hundreds of different ways to move large quantities of money from point A to point B for corporations providing services of questionable benefit to mankind. Just thinking of developing one of these suckers makes me want to kill myself with joy.
You C/C++ lusers can take your embedded software, operating systems, video games and such and stick them where the sun don't shine.
Happy people make bad consumers.
I dont' think that's entirely fair. Fast is a relative term; I've written GUIs in C# that have satisfied high performance requirements. I don't see anything in Firefox that would require a level of speed you can't get in .NET. Also, there's nothing preventing you from writing hoggish code in C++.
If by "portable" you mean "able to be ported" then you're correct. But, again, "porting" an application can be a pain in the ass in general. While there may be C++ compilers available on great number of platforms, it doesn't mean your code is gonna work on them.
I like this book, except for the picture of the author. His hairdo is downright creepy.
C++ IS a lot daunting at this point in your programming career. If you're just doing websites and database work then start with JavaScript and move to Perl or Python from there. Learning Perl is a good primer on a good first "real" language.
I wouldn't say "it will never be as fast as a well designed and coded C++ application."
well designed and coded versus language are two entirely different concepts. any well written program will run better than a poorly written one. period. we tend to think of java as slow, but it's the gui (swing) that's slow. as for java itself, comparing two comparable algorithms, there is little difference. now, why would i choose java per se over c++. here's an example: in my AP Comp Sci class, we created a swing, multi-threaded client/server chat program. 3 classes, total LOC 200. no it wasn't "perfect", but it worked rather well. try that in c++, any toolkit. had all 25 kids in the class in concurrent chat session, while running the server on a win98, P2 w/64MB ram. that is just one example. we did many others. but where java loses X% performance, it makes up for in speed of development and security. as for binaries, one, processor speed is making a JVM a moot point, plus, gcj4.0 does a fairly nice job of creating native java binaries so i've read.
My problem? I was perfectly gruntled, until some numbnuts came by and dissed me.
Wouldn't it be neat if programs automatically closed all resources when you were done with them? It would be like a book flying back to the bookshelf when you are done reading!
Amazingly, C++ supports this with a concept called RAII (resource allocation is initialization). Your file handles automatically close for you when you are done. Your classes come and go as you need them, without needing to worry about memory yourself. Shared auto pointers work great. They are not perfect, but they solve a lot of problems.
Java can't do this, because the detractors are not called when you are done with the class, they are called sometime latter when the garbage collector gets around to it. This means that you might still have a file locked when you tell a different program to work with it.
Get with modern C++. Garbage collection is neat, but it prevents RAII.
Lots of modeling and simulation code is written in either C++ or Java (more often than not C++). I write modeling and sim code for a defense project in C++, a friend of mine writes wargame code in Java. OOP is a great boon to modeling and simulation people, many of whom have deep roots in FORTRAN.
IAAAE (I Am An Aerospace Engineer)
-everphilski-
"For all intents and purposes C++ is a superset of C (ignoring C99). So, how can be C++ more painful to program in?"
If you just want to use C++ to write C, well then yes that's correct. But then why bother with C++ at all in that case?
Speaking as someone who's been scrawling code in C++ ever since that 1st-edition abomination of a manual was written by Stroustrup (gah! 19 years ago?!?), C++ has some great features for implementing OOD. But it also has some incredibly ugly areas as well, add-ons stuck to the side with duct tape and oatmeal.
Rather like taking a perfectly functional wheelbarrow that's ideally suited for its task, and souping it up for drag racing.
Asking how C++ can be more painful than C is like asking how the HomerMobile can be more painful than driving a Civic.
---
"I want a horn here, here, and here. You can never find a horn when you're mad. And they should all play `La Cucaracha'."
- Homer Simpson
As for reading specifications - thankfully my job involves implementing solutions to real problems rather than implementing a half-baked specification that someone else thought was the solution to the problem they imagined they had.
Doesn't it make you feel good to know that our freedoms are protected by politicans, lawyers and journalists.
Care to post the function for comparison purposes?
Dewey, what part of this looks like authorities should be involved?
As a Ph.D. student in combinatorics, if I even suggested writing the computationally intense programs that we require to generate, enumerate, or search for combinatorial objects in C# or Java, I'd be laughed out of the department. There's no way either C# or Java are up to the task when performance is that crucial.
I'm surprised no one mentioned (perhaps I've missed it?) Thinking In C++, by Bruce Eckel. It explains every subject in a very clear manner, providing quality examples throughout the two volumes. Not only that, you can download the book for free from http://mindview.net/
Garbage collection is primarily interested in reclaiming memory, a resource where it makes good sense to reclaim it lazily. You are right that lazy collection is a Bad Thing for other system resources (file handles, database connections, etc.) that need to be cleaned up in a more prompt fashion. You are, however, wrong in implying that garbage collection and RAII are mutually exclusive. See IDisposable in .net with the C# ``using'' statement for an example where a good design pattern and a little syntactic sugar make an excellent compromise.
Cleaning up scarce resources is unfortunately still best done manually (with some help from the language to prevent your forgetting), but my original point still stands -- namely that it's not a fault when a programmer working under a garbage collected environment doesn't concern himself with trivial details of *memory* management. This usually isn't a problem with *well-written* C++, but it gets pretty hairy when you're forced to juggle pointers (and not everyone is using auto_ptr (or shared_ptr) yet).
(And before you consider me a flaming zealot, I still prefer C++ for most of my personal projects. It's too tough to part with <algorithm>, after all.)
In the future, all spacecraft will be made of cheese.
Indeed, the reason I use C++ is performance.
However, I'm not aiming for HIGH, but PREDICTABLE performance.
You take a look at a loop and you'll roughly know how long it's going to take to run for C++ without GC.
Until Java or C# supports true destructors (i.e. I should be able to tell the GC that I want to delete this object whenever I want, not the GC want), a lot of people, myself included, will not use it for systems programming.
Only if you get to move large amount of money from A to B, except a small amount going to C (your account).
If not, I'll happily keep my embedded system projects, thank you.
I have a really elegant proof for Fermat's last theorem. If this sig was only a bit longer...
Never a -1 lame available when you need one!
And if you're new to C++, this is pretty much a must-own book.
This is the really sad part, and even sadder is that many people don't understand why it is sad.
Which one do you want!-)
That's the best oxymoron I've seen all day! Heck, I thought Microsoft Works was a good one. But now I realize that if you want to find out what an oxymoron is, you'll have a real problem, because if you look in the dictionary, the definition is: Effective C++.
Speaking of which, did you know that naive is not in the dictionary? You: Get a dictionary, find out what this 'closure' is.
Java has it's place in software developement for cool and flashy things, but it will never be as fast as a well designed and coded C++ application. True, because even compiled Java does more run-time checking than C++. So do Perl, Python, Ruby, and .Net. By that same token, C++ has it's place, but will never be as fast as a well designed and coded C application, because dereferencing through a vtable is slower than calling functions directly. Which might be why drivers and operating systems are still written mostly in C, with a little bit of assembly thrown in for very hardware-specific functionality.
I've abandoned my search for truth; now I'm just looking for some useful delusions.
Funny, Ruby seems to acheive the same functionality without resorting to templates, perhaps because unlike C++ and Java, in Ruby everything is an object.
I've abandoned my search for truth; now I'm just looking for some useful delusions.
Perhaps you're right. The responses to my post were pretty funny, too.
Intron: the portion of DNA which expresses nothing useful.
c+=c+++++c;
From g++:
foo.cc:5: non-lvalue in increment
From Sun CC:
"foo.cc", line 5: Error: Operand for operator "++" must be an lvalue.
There may also be an issue with:
c+= c++ + ++c;
Because while precedence rules apply, it may be undefined which of the 2 arguments to the + operator get evaluated first. This is very dangerous and non-portable code.
For example:
#include
using namespace std;
int main() {
double c=0;
c+=c++ + ++c;
cout << c << "\n";
}
~/ctest> g++ -O2 -Wall foo.cc ;
4
~/ctest> g++ -O2 -ffloat-store -Wall foo.cc ;
1
Does the release of the 3rd Edition mean they'll update the -Weffc++ switch for gcc?
[o]_O
Java will be a legacy language before C++, and .NET well before that. Thats the big problem with
commercial languages rather than open standard
languages. Some executive makes a decision and
your code is now all obsolete.
Code I wrote in C and C++ on Linux in 1992 is still in use, and can still be compiled with only minor tweaks.
Java is basically castrated C++, take out anything that might be dangerous. Meaning anything which can be used to make truly optimal code. Meanwhile not even providing higher level programming features like generic programming capabilities.
Yes, clearly machines running Smalltalk and LISP kernels never existed. And squeak developers are lying when they say its virtual machine is written in smalltalk.
And the reason why
for (x=0; x < 10000000; x++)
getpid();
takes 1 second due to system call overhead instead of 0.001 seconds is because C is such a great language for operating systems... hint: it's one instruction in a "safe" OS (ie not C/C++).
That's not because Ruby uses only objects; c++ templates can have an object as the template type as long as it implements the operators/functions used by the templated class. The reason Ruby achieves the same functionality without templates is because Ruby is interpreted. I.e., Ruby only gets away with it because it sacrifices memory and performance to be more generic. Have you seen how badly Ruby usually benchmarks?
(note: gcc tends to optimize poorly, especially old versions icc often optimizes to as much as twice as fast as gcc. And yes, I know that this is C being benchmarked, not C++, but performance should be similar)
Proposing a language that's not even 1/25th as fast as C++ is not a realistic replacement. When your language makes perl look lightning fast by comparison, you have problems.
Sigur RÃs: I didn't know that Heaven had a rock band.
Lets see you recompile something written for Windows on AIX and have it run the first time. I've done that in Java.
I've done that before routinely. well, linuxwindows. GCC really is a marvelous thing. The first time I did it by mistake, I was doing part of a data analysis project and couldn't find the binaries, so I just pulled my sources and recompiled it and ran it, only to later remember that I usually ran that part of the analysis on the linux side.
Problem is that as soon as you need windowing or threading, Java is easier to get working than C++, so I do use Java for those. But otherwise I stick with C++.
It loads faster (the OS and libraries are all loaded, and linking in DLLs is so efficient it puts loading a VM to shame), it runs faster (I've checked a few times when I implemnted in both), it leaves me free to pull off nasty tricks if I need a performance boost, it saves memory I can use for my data, the STL and friends are better than in Java, and multiple other things that makes me prefer it.
Obviously my experience comes from data-processing applications (data mining and such). If you're building web applets or small games or other things, your preferences might vary.
There is no choice but to use C++.
How about C? How about Pyrex?
Writing most of the product in Python and just implementing the performance-critical parts in Pyrex or C is much easier than writing it with C++ and creating bindings for Python.
It would also be just as fast, because almost always, less than 10% of the code runs more than 90% of the time.
Well, C++ developers usually use some sort of reference counting schemes.
In such a case, they suffer from the same predictability problems of GC systems.
Also, a lot of GC'd languages have stack-based optimizations to recognize they can destroy an object that's only used in a function locally.
Also, a lot of GC'd languages allow you to control exactly when the GC runs, which allows for predictable performance as well.
There is no technical reason why Ruby must always be interpreted. It could, like Python, be used to generate machine code. That being said, Ruby, Perl, and Python would still be slightly slower than C++ even compiled, as C++ does compile-time type checking whereas in the other languages almost all checking is done at run time.
I've abandoned my search for truth; now I'm just looking for some useful delusions.
Modern C++ features like templates are really a godsend in many application development projects
Where I work, we love STL - the only downside is our old gcc version that generates 200M binaries with about 700M of symbols. I hear that's fixed in recent versions, though.
"We returned the General to El Salvador, or maybe Guatemala, it's difficult to tell from 10,000 feet"
Python generates bytecode, not machinecode (didn't you ever wonder why .pyc files would run on any system, even though they're "compiled", or why the docs talk about "compiled" in quotation marks?). *Big* difference. Besides, even if Ruby was physically compiled, it would require what effectively amounts to cpu-eating wrappers around every object and every interface.
Sigur RÃs: I didn't know that Heaven had a rock band.
"The only reason...no stack-based variables". That's like some loser claiming "My Ford Focus is only slower than a Ferrari because the engine is crap". Doesn't anyone out there write real code? These things are so f-ing obvious.
Doesn't it make you feel good to know that our freedoms are protected by politicans, lawyers and journalists.
I've written GUIs in C# as well, high performance, flashy GUIs. What I found is that C++ is between 2 and 4 times faster for such code.
That may be acceptable, and it may not be. But the performance hit is definitely there, and it is visible to users as a less responsive UI.
The cake is a pie
I was thinking py2exe actually converts byte code to machine code. Apparently it doesn't. My main point stands, there is no reason why you couldn't convert byte code to machine code
I've abandoned my search for truth; now I'm just looking for some useful delusions.
Nobody with even half a brain actually believes C++ is a good thing.
Oops (no pun intended), I must've lost half my brain while learning C++. There's nothing wrong with OOP when applied correctly to C in the proper situation. There's nothing wrong with C when a procedural approach is better, and in the end, C++ is often just a better C compiler.
My problem is that the book is probably *Meyers doing templates* again. When templates first came out, I was sold on the idea. Years later, it seems that in practice, you have to write ever more code to cover every possible invocation and possible use. Now, it seems to me that templates are more useful to compiler and library writers than in the real programming world.
*Dons hairshirt garment and waits to be burned as a heretic.*
C++.NET doesn't fit anywhere in my Linux Arm Device. Java does but it's tricky.
Damn, subtle little troll.
OSGGFG - Open Source Gamers Guide to Free Games
Psyco does convert Python to machine code. In a related vein, projects like are currenly attempting to make Ruby benchmark much better, in this case by using a JIT compiler. I repeat my main point: there is no reason why Ruby, Perl, Python, et. al MUST be interpreted.
I've abandoned my search for truth; now I'm just looking for some useful delusions.
I wish some of these people bitching C/C++ would try to write a device driver in Java, or .NET. C and C++ were designed for writing system software, and the funny thing is, they're still good for writing system software. The "religious" languages were designed for writing high level applications, which is why the suck for writing anything at a lower level.
Don't blame me, I didn't vote for either of them!
C compiles to native code and doesn't require a runtime. Java and .NET compile to bytecode and require virtual machines. See the difference?
If you don't see the difference, please send me your Java device driver, because I would be very interested in looking at it.
Don't blame me, I didn't vote for either of them!
I'm guessing he uses C++ instead of assembly because he wants to actually get his programs written and debugged before he's 40.
Well, I just started a C++ program today. Many reasons to use it, but peformance is a biggy over C#/Java - not to mention increased flexibility, compatiability with legacy code, greater control over low level operations (important when dealing with things like barcode scanning or speech recognition). The list of useful things C++ can do goes on...
And as far as the Java device driver, check out:
http://www.amazon.com/exec/obidos/ASIN/0201183935
C# DOES support finalize and Dispose methods - finalize runs when GC runs (also unpredictable) but Dispose runs when you want it to. Not quite effective as C++ destructors, but for most applications not a bad compromise.
Well, its not practical for the vast majority of programmers, but there ARE people who write in Assembly where size (think embedded devices) and performance demand it.
The Java Advanced Imaging API in the JDK offers a C-based library called mediaLib that accelerates the imaging functions because the pure Java versions can't achieve good performance.
Urf. Don't speak about the JAI. I used it for a "simple" computer vision project years ago, and it felt like I was manipulating something held together with razor wire and duct tape.
Here come da fudge!
The reason it takes less time in Java is that the VM already made the call, and can return the answer from its own internal data. The system object has it there already.
In C, the compiler cannot assume that the return of getpid() will not change, so it is called each time. You cannot avoid system call overhad, just avoid making system calls unneccesarily.
In C, you can simply call getpid() once, and keep the result to get the same effect as the VM has. Try it with a system call that cannot be cached, like read()ing a byte at a time from a file descriptor of "/dev/urandom".
I bought 'C++ Primer 3rd Edition' a couple of months ago.
I'm new to C++ but not new to computer languages.
I'm finding this box a dream to read, I think the previous posters paste says it all, but I highly recommend this book to somebody that wants to learn C++.
My understanding is that it's almost impossible to do any realtime systems in java. I've just come off a complex traffic control systems project done in C++ to meet the very strict failure response time requirements.
Now I'm working on a finance back office system in J2EE. Horses for courses.Java/J2EE is great for GUI/enterprise stuff. C++ is great for control systems and high performance components.
It's easy to get caught up in the hype regarding the newest wiz-bang developement techs that will make all others redundant. For those of us simply wanting to solve problems in a efficient, mercinary like way, there's massive value in using a proven techs for a given problem space while the early adptors bed down the newer techs.
Note that python is inherently about 130 times slower than unoptimized C with an old compiler, according to the previous benchmark. Giving it a typical 2-4x speedup (as per the psyco docs) still means it is pathetically slow. The reason, again, is that everything is wrapped extensively, overly checked, mostly unoptimized (because many types of optimizations break when you change datatypes), etc. Ruby will suffer the exact same thing. If you want generic interfaces to objects to allow you to avoid templates, you have to suffer overhead as your penalty. And these languages take a lot more penalties than that - for example, bounds checking on every array access. They're inherently slow.
Do I have to get into how the actual assembly code varies between code generated with a template over one datatype and another to show you why if you care about performance at all, you need to know your datatypes ahead of time?
Sigur RÃs: I didn't know that Heaven had a rock band.
Seriously, I find this entire thread hard to believe. Anyone who has to deal with performance or security in any meaningful way whatsoever is laughing at the suggestion that C++ is "old school". And anyone who wants to try and explain that a "sandbox" actually creates security needs to go back to school.
Some Java and .NET implementations compile to bytecodes, others don't.
And the ones that don't still require a runtime. I would actually switch to Java for a lot of things if I could get one that would compile to native code and didn't require anything more than a standard libc.
And as far as the Java device driver, check out...
Except that it doesn't run on my OS. It doesn't run on any of my friends' OS. Heck, it doesn't even run on Sun's OS, and they own Java! JavaOS is like the Lisp Machine: It's a nice concept but no one really uses it.
p.s. Without having to buy the book, and since you seem to be the expert on this, how does JavaOS manage to bootstrap the VM? Does it cheat and use a C/C++ layer on top of Mach?
Don't blame me, I didn't vote for either of them!
Yeah, but why give up the performance?? So Intel can sell more chips? And please, let's compare apples to apples: a competent programmer writing the same quality code in C++ and C#. And if you're writing hoggish C# you have my deepest sympathies.
The people who think this way are the same people who think that Java/.NET code can't possibly have any security issues because it can't possibly have any buffer overflows.
Don't blame me, I didn't vote for either of them!
Everything has a price; in this case the trade-off is between some performance and speed in getting the app to the customer. As has been documented many times, the ratio of bugs to lines of code in C++ is significantly higher than in the other higher-level languages. If the performance hit is unnoticable, only a fool would choose to hire a more expensive developer to take longer.
.Net is as good as any that the best developers would write themselves, and better than what 90% would. Remember, Pride goeth before the fall.
And please, let's compare apples to apples: a competent programmer writing the same quality code in C++ and C#.
This competent programmer would write better code in the same time in C# or Java than she would in C++. Like they tell the first day of class: C and C++ are more powerful languages, but they present the opportunity for many more subtle and hard-to-find errors. E.g., the garbage collector in
C++ gives you the opportunity to more efficient code. But runtime efficiency is only one of several facets that I would say go into making good code.
I don't really disagree with what you're getting at but again, you're saying "better code". That really doesn't make any sense. For what I do (which is write applications that require performance, security, and portability), you're going to be hard pressed to produce something my customer will use let alone appreciate with C# (which isn't to say people don't try). I'm not holding onto C++ for dear life because I don't want to change; I understand the value of even-higher-level languages. But don't make the mistake of thinking this is about garbage collection or time to market. That's fine for the program of the week. You're going to have a tough time selling me that serious and complex applications are going to be running for the C# (or worse, java) bandwagon anytime soon. Sure, it's hard work and requires actually understanding the details but in my experience those are usually signs of a job well done.
XML causes global warming.
As far as i remember, this g++ option was not checking all the rules described in the 'effective C++' and 'More effective C++' books. I hope it will be available in g++ one day. By the way, there are some (paying) products that can analyze C++ source code and chez all these rules. I know Parasoft Codewizard but it's very expensive and difficult to configure to get really meaningful messages.
le souvenir d'une certaine image n'est que le regret d'un certain instant (M.Proust)
Whoops, int pid = ::getpid (). That's what you get for free. For $9.95, you can have the infinitely faster version. For $29.95, you canhave the version that's slight faster yet. For $1M, I'll come over to your house and tell you the process id whenever you need to know it.
XML causes global warming.
Well, I've never touched JavaOS, but I just cited it as probably the most well-known example. There's lots of cases of safe languages used as OS implementation languages, from the Lisp machines to Xerox's Cedar system, to the TCP stack that someone (CMU ?) wrote in ML.
Keep in mind when we say that the Linux kernel is written in C, it isn't really written in ANSI C with the standard runtime, it is written in a subset, with some assembly language magic here and there. You can't call fopen() from the kernel! Same is true for Java as OS -- sure, you can't use the bog-standard Sun hotspot vm as a kernel, but you can program in a subset of standard java to build an OS. You generally need a system which compiles java to machine code, and you generally need to link in a bit of assembly to get things going. Then, all you really need is a little bit of JNI help (or VM magic support), to be able to read and write raw device registers, and away you go.
While I agree that a good book (or three) on algorithms is necessary, this book does not qualify, at least IMO. I would recommend Introduction to Algorithms by Cormen, Leiserson, Rivest and Stein as one of many superior alternatives. It has a few problems as well (mostly excessive formality) but at least it has the information you need. Of course the old standby in this genre is to get all three volumes of Knuth, plus the "fasicles" of volume four that are coming out (supplemented as needed in areas he hasn't covered yet). If you can find a copy of Algorithms + Data Structures = Programs by Niklaus Wirth, it's a very good book as well (though I'm pretty sure it's been out of print for quite a while -- but getting your favorite used book store to search for ISBN 0-12-022418-9 may be worthwhile). As you might expect, this uses Pascal for its examples, but for this kind of book, the specific language used doesn't matter all that much.
If you want more detail about these books (such as why Sedgewick's is utterly worthless) see: http://tinyurl.com/d68kl.
Hmm...oddly enough, I don't have this book, and manage to get by fairly well -- IMO, if you want a book on software engineering, something by Grady Booch or Bertrand Meyer would be a better bet. Somewhere in here there should also be a mention of Structure and Interpretation of Computer Programs -- in fact, it might be the most important and useful of all -- even though it uses Scheme rather than C++.
--
The universe is a figment of its own imagination.
The universe is a figment of its own imagination.
You can avoid system call overhead by writing the OS in a safe language, removing the need for memory protection and the kernel/userspace divide.
So there.
Freedom is not increased by mere diminuation of government. Anarchy is freedom for the strong and slavery for the weak.
For other system calls that can't be cached, java and C will have similar performace...
You should read the post you're replying to. He's not comparing the performance of Java and C, he's comparing the time taken to make a system call on an OS written in a low-level language like C (with the associated memory protection overhead) with the time taken to make a system call on an OS written in a safe, high-level language (e.g. a Lisp machine OS). If you write the OS in a safe language, you can get rid of the kernel/userspace divide and the overhead which it creates.
Freedom is not increased by mere diminuation of government. Anarchy is freedom for the strong and slavery for the weak.
I haven't read the STL book mentioned in the parent, but I'd reccommend using Meyers' Effective STL as a compliment. Just like Effective C++ I think it does an excellent job of clearly explaining the "why's" of its subject, leaving the "what's" for a reference book.
I've found Stroustrup's and Meyers' works invaluable at work. I was brought up on C++ during college, but my work was left me dealing with a lot more old C code than I expected. Their explanations of "these are the ugly C workarounds, now this is how C++ can do them cleanly" have really helped me understand what sorts of idioms and practices are being attempted in the old C code.
- spiff
I was unaware that your typical jvm could run more than one unrelated process.
XML causes global warming.
Again, read the post you're replying to. He wasn't talking about JVMs, he was talking about operating systems written in safe, high-level programming languages. Get with it!
Freedom is not increased by mere diminuation of government. Anarchy is freedom for the strong and slavery for the weak.
Certainly the fact that the equivalent of a context switch can be much lighter weight in a virtual machine than it can in your typical C OS implementation is important for some classes of applications.
Am I sufficiently with it now?
XML causes global warming.
OK. I think VMs are a bit of a red herring here though. Using a high-level language (VM or no VM) allows you to get rid of memory protection, and this is what can make system calls much faster (*). In fact, the language needn't even be high level, it just needs to have bounds checking and constrained pointer semantics -- Fortran would probably do the trick. In the real world, where there aren't any mainstream OSs written in safe languages, this is admittedly an academic point, and you're quite right that there's no reason why Java should be faster for repeatedly calling UNIX system calls.
(*) If there's no memory protection, calling a system call is just like calling any other function. There's in fact not any very clear boundry between different applications at all -- you just have a number of different threads of execution.
Freedom is not increased by mere diminuation of government. Anarchy is freedom for the strong and slavery for the weak.
It is true that it's harder to write templated classes than non-templated classes. It's also true that template class libraries are often more useful and reusable than non-templated classes. However, library writers do actually exist in the real world! How else do all those libraries get written?
Linux isn't writtin in ANSI/ISO C because it insists on using GCC extensions. But there's no reason it couldn't be. The BSD kernels are all ANSI/ISO C. Even though they can't call fopen() from the kernel. Why? Because the standard library is distinct from the language! This is a concept that Java just can't get through it's head.
A C/C++ compiler is totally ignorant of any Standard Libraries. It treats a call to fopen() no differently than a call to my_non_standard_function(). This is why C and C++ are so popular with embedded developers, because they aren't saddled with extra baggage. Yes, Java is being used in some cellphones. According to a friend of mine who writes embedded Java for Nokia, it's one of the biggest mistakes the cell phone industry ever made.
Don't blame me, I didn't vote for either of them!
However, library writers do actually exist in the real world! How else do all those libraries get written?
Yes, I should have chosen a better adjective than real. :)
and .NET... is that even ported to other operating systems yet (maybe the mono project, but I'm not sure how far developed that is)?
It's more or less fully developed, as a quick trip to Google would have told you. The only thing missing from a portability aspect is a mature implementation of Windows.Forms.
Freedom is not increased by mere diminuation of government. Anarchy is freedom for the strong and slavery for the weak.
if you care about performance at all, you need to know your datatypes ahead of time?
Not if you have an optimising JIT. Then you only need to know your datatypes at runtime.
Freedom is not increased by mere diminuation of government. Anarchy is freedom for the strong and slavery for the weak.
Modern C++ features like templates are really a godsend in many application development projects
Of course, static polymorphism has been implemented much better in functional languages (e.g. ML and Haskell). The C++ attempt is painfully verbose and hacked-on.
Freedom is not increased by mere diminuation of government. Anarchy is freedom for the strong and slavery for the weak.
Or you could use OCaml. That has static polymorphism, objects (both superior to the C++ equivalents) and native code compilation.
Freedom is not increased by mere diminuation of government. Anarchy is freedom for the strong and slavery for the weak.
Look at what these people are saying though (I am the one that supports C++ in that thread!). These people are giving C++ a bad name, yet they are totally unaware about its capabilities. C++ is the greatest programming language ever (feel free to flame me) considering the large set of domains that it covers.
Hey, look what miracles (for example, here, here or here) can be done with C++ templates!
doing an objc call lookup requires calling a function to search through a table to find the function address. That's easily hundreds to thousands of instructions executed. You can, however, store the function address locally, so later calls would be even faster than C++.
Do you even lift?
These aren't the 'roids you're looking for.
I read this book 6 or 7 years ago, and so many of its concepts have become fundamental to the way I design software that I was shocked when I re-read it a few months back to discover how many of what I considered "my" design and coding techniques came straight out of this book!
There are some things that are very C++ specific, and some things that are probably obsolete since this was written in 1996 (redundant include guards come to mind), but there are more than enough gold nuggets in this book to make it worth a skim even if you never use C++.
Patrick Doyle
I mod down every jackass who puts his moderation policy in his sig. Oh, wait a sec....
With an OS written in a safe language, for safe languages, everything runs in ring 0. You don't have to use page tables; you can if you want to, with no requirement for any more assembly than otherwise.