C++ small gotcha

Suppose str1 and str2 points to the same zero terminated string, will str1 and str2 be the same value after running below while loop? while(*str1) { str1++; }; while(*str2++); Answer: Str1 points to the terminating zero of the string. But str2 points to the memory address after the zero. See the assembly of “while(*str2++);” will…

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