background preloader

C++

Facebook Twitter

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.

The Assignment Operator Revisited

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. 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.

An introduction to C++ Traits - Iceweasel

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. Error and Exception Handling - Iceweasel. References The following paper is a good introduction to some of the issues of writing robust generic components: D.

Error and Exception Handling - Iceweasel

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.'' 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.

Debugging Native Memory Leaks, Part 1: LeakDiag « Ofek's Visual C++ stuff - Iceweasel

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? 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!

Debugging Memory Leaks, Part 2: CRT support « Ofek's Visual C++ stuff - Iceweasel

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. Comparing floating point numbers.

Windows

C++ Frequently Questioned Answers. C++ FAQ. Using Vector and Deque. What is the difference between vector and deque?

Using 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. Guru Questions. A Guide to Undefined Behavior in C and C++, Part 1. Also see Part 2 and Part 3.

A Guide to Undefined Behavior in C and C++, Part 1

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 $ .