Design pattern in .NET: iterator

The iterator pattern plays very important role in STL. In STL, iterators are the glue between algorithms and containers. STL algorithms and containers are independent of each other; each STL container implements its own iterator and the STL algorithms use iterators to access the elements of the containers. For example, there is a for_each algorithm…

0

How to get high resolution timing info

We know that we can use GetTickCount to get timing info but the resolution of this API is limited to about 10 milliseconds, see MSDN: http://msdn.microsoft.com/en-us/library/ms724408(VS.85).aspx The resolution of the GetTickCount function is limited to the resolution of the system timer, which is typically in the range of 10 milliseconds to 16 milliseconds. To get…

0

C++ Tips: Adjustor thunk: what is it, why and how it works

When you debug C++ applications, you may notice below unusual functions generated by the compile. Have you even wondered what the adjustor thunk function is. Why it is needed, and how it works? 00ce10a4 8b06            mov     eax,dword ptr [esi]00ce10a6 8b10            mov     edx,dword ptr [eax]……00ce10ab 8bce            mov     ecx,esi00ce10ad ffd2            call    edx {test![thunk]:Derived::Func1`adjustor{4}’ (00ce10c0)} When virtual function…

1

C++ tips: Why the pop method of STL stack does not return a value

Or why they don’t combine the functionality of pop/top method into one function? We know that the pop method pops the top element of the stack, change the stack’s internal state but returns nothing; and the top method returns a reference to the top element on the stack, but does not change the stack’s internal…

0

C++ tips: why you should not leak exception from a destructor

One of the reasons: we know the destructor of an auto object can be called in two situations:  1. When the object is out of scope normally.  2. During the stack unwind when handling an exception. For situation 2, if the destructor throws another exceptions when it is handling the first exception, C++ will call…

0

Design pattern in .NET: template method

GoF’s classical design pattern book has discussed template method pattern: define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm’s structure. We see this pattern in .NET programming also: 1. The dispose pattern Any type that wraps…

0

Structured Exception Handling (SEH) and C++ exception handling

C++ exception handling builds on top of the OS SEH support. If you are writing C++ code, it is recommended you use C++ exception handing since SEH does not know how to handle C++ objects properly. For example, for below code: #include “stdafx.h”   class MyClass { public:         ~MyClass()         {                printf(“Myclass dtor\r\n”);…

0

C++ tips: Disable "Enable Incremental Linking" linker option to get better symbols

By default, Visual Studio does not generate very friendly symbol names. For example, if you build below code, then run it under windbg: #include “stdafx.h” class MyClass{public: MyClass(); void Func1();}; MyClass::MyClass(){ printf(“in MyClass::MyClass\r\n”);} void MyClass::Func1(){ printf(“in MyClass::Func1\r\n”);} int _tmain(int argc, _TCHAR* argv[]){ MyClass obj1; obj1.Func1(); return 0;} If you check the assembly of wmain, you will see some unfriendly symbol names as…

1

C++ tips: AddVectoredExceptionHandler, AddVectoredContinueHandler and SetUnhandledExceptionFilter

I write below code to verify the behaviors of AddVectoredExceptionHandler, AddVectoredContinueHandler and SetUnhandledExceptionFilter: #include “stdafx.h” LONG WINAPI MyVectorContinueHandler(PEXCEPTION_POINTERS p){ printf(“in my vectored continue handler\r\n”); return EXCEPTION_CONTINUE_SEARCH;} LONG WINAPI MyVectorExceptionFilter(PEXCEPTION_POINTERS p){ printf(“in my vectored exception filter\r\n”); return EXCEPTION_CONTINUE_SEARCH;} LONG WINAPI MyUnhandledExceptionFilter(PEXCEPTION_POINTERS p){ printf(“in my unhandled excepiton filter\r\n”); return EXCEPTION_CONTINUE_SEARCH;} LONG MyExceptFilter(){ printf(“in my filter\r\n”); return EXCEPTION_CONTINUE_SEARCH;} int _tmain(int argc, _TCHAR* argv[]){ LPTOP_LEVEL_EXCEPTION_FILTER pOriginalFilter = SetUnhandledExceptionFilter(MyUnhandledExceptionFilter);  AddVectoredExceptionHandler(1,MyVectorExceptionFilter); AddVectoredContinueHandler(1,MyVectorContinueHandler);…

2