background preloader

C++ FAQ

C++ FAQ

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:

CodeGuru: STL.NET: Combining Generics and Templates It has been jokingly suggested that when a C++ programmer is asked how to accomplish a given task, he or she will provide a list of a dozen or more potential solutions, and then proceed to outline the problem with each in excruciating detail. Visual C++ 2005, through the C++/CLI language bindings, introduces the concept of generics, allowing C++ programmers to further enhance their reputation as the programmers with the largest arsenal of language constructs. Brent Rector covered the differences between .NET generics and C++ templates in an excellent article in June 2004. The difference between templates and generics leads to the question that every C++ programmer who wants to target .NET will end up asking: Which technology should I pick? Leverage C++ Skills for .NET Development In the past, Visual C++ has offered some technologies that have enabled STL collections to work with other technologies. Bridging Templates and Generics Using STL.NET C++ Programmers' Ticket to the .NET Realm

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

Compilers and Compiler Generators Special Symbols | Contents | Diagrams | Symbol Font Edition | GIF Font edition | Downloadable files | Source Code | Courseware | Change List an introduction with C++ © P.D. Terry, Rhodes University, 1996 NEW (1 February 2005) The complete text of the book has been distilled into one PDF file (1MB) pdfvers.pdf, courtesy of Irwin Oppenheim of the Netherlands. NEW (23 November 2004) A complete revision of this book, using C# and Java and the versions of Coco/R for those languages, was published by Pearson Education (Addison Wesley) on 5th November 2004 under the title "Compiling with C# and Java" (ISBN 032126360X). A feature of this book is that it demonstrates the use of Coco/R to implement compilers for the JVM and CLR platforms. You can view the contents of the freely available "Resource Kit". You might also like to view the Pearson Education catalogue entry for this book. Order it from www.amazon.co.uk (February 2003) Local site reorganization: A previous ftp server has been decommissioned.

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.

Boost C++ Libraries 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. The C++ traits technique provides an answer. Think of a trait as a small object whose main purpose is to carry information used by another object or algorithm to determine "policy" or "implementation details". - Bjarne Stroustrup Note the use of numeric_limits. Add to that a specialisation for void: And that's it.

GCC Home Page 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 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. As I said, this was caught during the review process. Unfortunately, that’s wrong too. The call to TSuperFoo::operator=() has to go inside the try. The magic three

A Coding Convention for C++ Code This document was borrowed and slightly modified from the Berkeley Systems, Inc. coding conventions. If you're looking for something more comprehensive, try the Ellemtel C++ Rules and Recommendations: See also, Christopher Lott's collection of C and C++ style guides and the Mozilla portability guide. Purpose To make it easier for us to read each other's code, here are a few guidelines to follow when writing C++ code. Definitions Member functions are functions that are associated with classes. Identifier Names All variable and constant identifiers begin with a lowercase letter. Class Declarations Classes are declared in the following order: public member functions, protected member functions, private member functions, protected data members, and private data members. Control Structures The control structures are as follows: File Names C++ source files use the suffix .cc.

C++0x 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. 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. The paged organization of a deque offers significant benefits: 1. 2. 3. vector<C> c( 10000 ); 1. 2. 3.

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 !

cppcheck - Iceweasel

Related: