Tag: C++

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…

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…

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…

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…

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…

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…

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”);…