Cppcheck - Iceweasel. Cplusplus.com. C++0x FAQ. The Assignment Operator Revisited. The Assignment Operator Revisited by Richard Gillam Advisory Software Engineer, Text & International IBM Center for Java Technology–Silicon Valley If you think you know it all in the C++ world, it must mean you’re not talking to your colleagues very much. If I had any pretensions to knowing it all when I wrote my assignment-operator article ("The Anatomy of the Assignment Operator," C++ Report, Nov/Dec 1997), they didn’t last long afterwards. The assignment-operator article drew a huge response, with a lot of people sending me corrections and disagreements of various kinds. The issues have been mounting up, so I thought maybe a follow-on article to discuss the issues would be appropriate.
The big mistake One I heard about almost instantly from several people (and which I’m really glad I heard about before delivering a talk on this subject at C++ World) was a rather serious mistake. This was wrong, and it was caught in the review process. Unfortunately, that’s wrong too. The magic three. C++ and the Perils of Double-Checked Locking. C++CLIRationale.pdf. An introduction to C++ Traits - Iceweasel. It is not uncommon to see different pieces of code that have basically the same structure, but contain variation in the details. Ideally we would be able to reuse the structure, and factor out the variations. In 'C' this might be done by using function pointers, as in the C Standard Library qsort function or in C++ by using virtual functions. Unfortunately this differed to runtime what is known at compile time, and became with a runtime overhead. C++ introduces generic programming, with templates, eliminating the need for runtime binding, but at first glance this still looks like a compromise, after all, the same algorithm will not work optimally with every data structure.
Sorting a linked list is different to sorting an array. Sorted data can be searched much faster than unsorted data. The C++ traits technique provides an answer. Most C++ programmers are familiar with std::numeric_limits, which at first glance simply provides the same service, implemented differently. And that's it. Error and Exception Handling - Iceweasel.
References The following paper is a good introduction to some of the issues of writing robust generic components: D. Abrahams: ``Exception Safety in Generic Components'', originally published in M. Jazayeri, R. Loos, D. Guidelines When should I use exceptions? The simple answer is: ``whenever the semantic and performance characteristics of exceptions are appropriate.'' An oft-cited guideline is to ask yourself the question ``is this an exceptional (or unexpected) situation? '' A more appropriate question to ask is: ``do we want stack unwinding here? '' How should I design my exception classes? What About Programmer Errors? As a developer, if I have violated a precondition of a library I'm using, I don't want stack unwinding. Sometimes it is necessary to have resilient APIs which can stand up to nearly any kind of client abuse, but there is usually a significant cost to this approach.
How should I handle exceptions? Often the best way to deal with exceptions is to not handle them at all. Debugging Native Memory Leaks, Part 1: LeakDiag « Ofek's Visual C++ stuff - Iceweasel. Leaking memory is probably the single most painful aspect of native code – its the reason managed was ever born. Sadly, this is useful only in the handful of cases where the code allocates directly. What if the offending allocation is performed via some common container routine? Even worse – what if the leak is properly de-allocated in destructors at shutdown time?
The CRT support would be of no use. There are two powerful, free, and vastly different tools from Microsoft, that achieve just that. Enter LeakDiag! I’m actually not sure how public this tool is. LeakDiag does its magic by using Detours technology (fascinating read!) To demonstrate, consider the leaking code here (adapted from a UMDH demo) : Start LeakDiag and run your program.
This activates the API interception in your selected process (LeakyApp.exe), for your selected API (CRT allocator). Focus back to LeakyApp.exe and press any key. Click anything again to end the program. Start LDGrapher. Like this: Like Loading... Debugging Memory Leaks, Part 2: CRT support « Ofek's Visual C++ stuff - Iceweasel. Problem and Immediate Solution If you’re developing MFC apps, the way you’ll usually notice any leaks is by terminating your app and seeing the following in the output window: Detected memory leaks! Dumping objects -> C:\myfile.cpp(20): {130} normal block at 0x00780E80, 64 bytes long. Data: < > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD Object dump complete. the {130} is emphasized on purpose – it is the serial number of the leaking allocation. Well, it might not be that easy. Now if the serial number is consistent, what allocations exactly does it count? Happily, there is an easy solution to the second set of worries.
_CrtSetBreakAlloc(130) Mid-depth Dive The allocation counter resides in the undocumented _heap_alloc_dbg, which channels calls from all C allocators – namely new, malloc, and their brethren (_malloc_dbg, aligned /offset flavours, etc.). When you do set an allocation breakpoint, you’d (naturally) break in the counting function itself, _heap_alloc_dbg. Bonus Like this: Comparing floating point numbers.
C++ Frequently Questioned Answers. C++ FAQ. Using Vector and Deque. What is the difference between vector and deque? When should you use each one? And how can you properly shrink such containers when you no longer need their full capacity? These answers and more, as we consider news updates from the standards front. Problem JG Question 1. In the standard library, vector and deque provide similar services.
Guru Questions 2. Vector<C> c( 10000 ); c.erase( c.begin()+10, c.end() ); c.reserve( 10 ); 3. Warning: Answers 2 and 3 may be subtle. Solution In Most Cases, Prefer Using deque (Controversial) The C++ Standard, in section 23.1.1, offers some advice on which containers to prefer. Vector is the type of sequence that should be used by default... deque is the data structure of choice when most insertions and deletions take place at the beginning or at the end of the sequence. vector and deque offer nearly identical interfaces and are generally interchangeable. deque also offers push_front() and pop_front(), which vector does not. 1. 2. 3.
Vector<C> c( 10000 ); 1. A Guide to Undefined Behavior in C and C++, Part 1. Also see Part 2 and Part 3. Programming languages typically make a distinction between normal program actions and erroneous actions. For Turing-complete languages we cannot reliably decide offline whether a program has the potential to execute an error; we have to just run it and see. In a safe programming language, errors are trapped as they happen. Java, for example, is largely safe via its exception system. In an unsafe programming language, errors are not trapped. The C FAQ defines “undefined behavior” like this: Anything at all can happen; the Standard imposes no requirements. This is a good summary. For now, we can ignore the existence of compilers. The execution of a program consists of simple steps such as adding two numbers or jumping to a label. If any step in a program’s execution has undefined behavior, then the entire execution is without meaning. As a quick example let’s take this program: $ cc test.c -o test $ .
So is this: $ cc test.c -o test $ . And this: THIS IS WRONG. (b !