jaredpar's WebLog

Code, rants and ramblings of a programmer.

Browse by Tags

Tagged Content List
  • Blog Post: Yet another way to defeat C++ const

    One of my favorite C++ features, and one I feel is terribly underutilized in many code bases, is the const mechanism.  It’s a great mechanism for defining dual roles for the same object type: a mutable and (ideally) non-mutable one.  But as useful as const is it’s also very easy to circumvent...
  • Blog Post: Discriminated Unions in C++

    Earlier this week I started writing a function which needed to represent three states in the return value, two of which had an associated value. In my mind I immediately came to the following solution type BuildAction = | Reset | LinkOneWithNext of Statement | LinkManyWithNext of Statement seq...
  • Blog Post: Disable copy construction when the type is not copy safe

    A couple of days ago I finished coding up a feature in our C++ code base, hit F5 and was met with a nasty memory corruption debugger dialog. After about an hour of investigation it appeared one of my types was living past the lifetime of it’s owning heap. I decided the next step was to debug on...
  • Blog Post: Type safety issue when assigning CComPtr<T> instances

    Recently while making a bug fix to our selection tracking code I discovered an unexpected behavior with CComPtr<T> instances.  The crux of the fix included creating a new tracking mechanism exposed via COM in the type ISelectionTracking.  The old interface, lets call it IOldTracking,...
  • Blog Post: Questions not to hinge a C++ interview on

    People love to chat about how to conduct a C++ interview on newsgroups .  Eventually these topics will shift into a discussion about what questions a candidate must know in order for them to get a hire from a particular interview.  Unfortunately these questions tend to be items like the following...
  • Blog Post: Redefining Success

    Spent about an hour debugging a bit of code today.  I was attempting to read data from a particular source and kept getting back failure codes.  After some debugging I discovered the data didn't actually exist in the source I was reading from.  This put me back to investigating where I...
  • Blog Post: Where does the * go?

    This is a more amusing than functional debate I enter into from time to time. On a line where you declare a pointer type in C++, where should the * go? Next to the type (i.e. Type* p1;) Next to the variable name (i.e. Type *p1;) Who cares For the moment lets ignore #3 (after all they don't care). I'm...
  • Blog Post: Bit by void*

    Recently I got bit by void* again because of another C++ quirk I didn't think through.  I had a class which wrapped a void* which could be one of many different structs.  The structs were POD and didn't have any shared functionality hence I didn't bother creating an inheritance hierarchy. ...
  • Blog Post: do {} while(0) what?

    A recent check in of mine raised a few eye brows during reviews. I checked in a few macros which ended with/contained a "do{}while(0)" and people were curious as to why. In my experience there are two main uses for it. Insert an empty statement with no runtime penalty Group a set of statements...
  • Blog Post: Saved by PowerShell

    Recently I made a very large update to our code base.  Our code base lacked a standard way of guarding entry and exit points into the various components.  Having said guards is useful for error handling, tracing, reducing redundancy, etc ...  The edit standardized our entry points by adding...
  • Blog Post: C++ Programming Books

    I was reading a post on Coding Horror the other day about programming books and how developers don't read enough of them.   I readily agree with the first two points in the article that 1) most programming books suck and 2) books are sold by weight not by volume.  Reading is an integral part...
  • Blog Post: Gotcha: CComAutoCriticalSection and copy constructors

    While investigating a crash during a suite run I found the stack walk included the destructor for a CComAutoCriticalSection . This is a fairly reliable class so I immediately suspected my code. I did a couple of quick checks for a double free and didn't find any. Then I looked a little closer at CComAutoCriticalSection...
  • Blog Post: Thread Local Storage template

    Thread local storage is another method of synchronization between threads. It is different that most synchronization cases because instead of sharing state between threads it enables developers to have independent, thread specific pieces of data which have a similar or common purpose. The uses of thread...
  • Blog Post: Gotcha: CComPtrBase<T> assignment

    Today what started out as a crash due to a pure virtual call turned into finding a gotcha in CComPtrBase<T>. Essentially the code in question boiled down to the following. Can you spot the problem? void GetAStudent(CComPtrBase<T> &spStudent) { CComPtr<Student> spLocal; // Do...
  • Blog Post: Reference values in C++

    Reference values are a powerful feature of C++ but I find they have one significant detractor. A developer can not look at an API call and determine if a parameter is being passed by reference or value (VB has the same problem). IMHO this is one item that C# got 100% correct. In C# developers must say...
  • Blog Post: API Problems: CComObject::CreateInstance

    CComObject::CreateInstance is a light weight method for creating instances of COM objects in your code. Unfortunately the design of the API makes it easy to introduce subtle errors into your code. The two problems are it encourages manually ref counting and the object initially has a ref count of 0....
  • Blog Post: Multiple paths to IUnknown

    ATL has a lot of great tools for COM programming and CComPtr is a good example. It's a smart pointer class which manages the reference count of an underlying COM object. One of it's limitations though is it will only work properly when the inheritance chain for a class has only one path to IUnknown...
  • Blog Post: Mixing SEH and C++ Exceptions

    Recently I had a half day adventure trying to catch a SafeIntException in code I was writing. The particular function involved a bit of math with user controlled values. Writing a bunch of IfFailGo's with several TryAdd style API's was getting tiresome so I decided to just use SafeInt, catch an overflow...
  • Blog Post: C++ Refactoring: Default booleans and pointers

    This is another recount of an experience I had refactoring some C++ code recently. In some ways this is also a follow up of my previous post about why you shouldn't use boolean parameters (especially default value ones). I recently refactored a large set of API's in our code base to remove a common parameter...
  • Blog Post: C++ Placement New: Tracking down a crash

    See my previous two posts on an introduction to placement new if you are unfamiliar with the subject. http://blogs.msdn.com/jaredpar/archive/2007/10/16/c-new-operator-and-placement-new.aspx http://blogs.msdn.com/jaredpar/archive/2007/10/17/c-placement-new-and-allocators.aspx Recently I did a bit of work...
  • Blog Post: C++ Placement New and Allocators

    This is a follow up for my previous post about operator new and placement new. This post will discuss the role of adding a custom allocator and using it with new. It's handy to use custom allocators in C++. Certain operations can be done more efficiently on a different type of allocator than the rest...
  • Blog Post: C++ New Operator and Placement New

    Originally I was going to write this article to detail a particular problem I had recently with placement new in C++. A page or two of writing later I decided it would be best to start with an introduction to the "new" operator itself and the in/outs of overloading/replacing it. The C++ new operator...
  • Blog Post: C++ Literal Number Grammar

    Recently I was investigating a bug and ended up investigating the possible values of C++ literals for char and number types. The following page has a very useful and detailed grammar of C/C++ literals that came in handy. http://cpp.comsci.us/etymology/literals.html
  • Blog Post: C++ Frustration: Unions and Constructors

    Using PreFast internally we recently came across a class of bugs. In several places we were using uninitialized structures as local variables. The error came about because the types in question had no default constructor. For instance take the following structure struct s1 { int m1; int m2; };...
  • Blog Post: C++ preprocessor and template identifiers

    A couple of hours of tracking down a compiler error a couple of days ago taught me something about the preprocessor I'd like to pass on so others can avoid the ... learning :). I kept getting confronted by "constant expression must be followed by a ,". Not the exact error but in the right range. This...
Page 1 of 1 (25 items)