Tuesday, March 19, 2013

stack unwinding

“When an exception is thrown and control passes from a try block to a handler, the C++ run time calls destructors for all automatic objects constructed since the beginning of the try block. This process is called stack unwinding. The automatic objects are destroyed in reverse order of their construction.
If an exception is thrown during construction of an object consisting of subobjects or array elements, destructors are only called for those subobjects or array elements successfully constructed before the exception was thrown. A destructor for a local static object will only be called if the object was successfully constructed.
If during stack unwinding a destructor throws an exception and that exception is not handled, the terminate() function is called.”

Stack Unwinding

program looks to see if the exception can be handled immediately (which means it was thrown inside a try block). If not, it immediately terminates the current function and checks to see if the caller will handle the exception. If not, it terminates the caller and checks the caller’s caller. Each function is terminated in sequence until a handler for the exception is found, or until main() terminates. This process is called unwinding the stack

Monday, March 18, 2013

Shared pointers ,Smar Pointer

Shared pointers (std::shared_ptr) implement shared pointer ownership — they keep the object alive as long as there are alive references to it, because there is no single owner. It's usually done with reference counting, which means they have additional runtime overhead as opposed to unique pointers. Also reasoning about shared ownership is more difficult than reasoning about exclusive ownership — the point of destruction becomes less deterministic.
  
Shared pointers are slightly more costly as they hold a reference count. In some case, if you have a complex structure with shared pointer at multiple recursive levels, one change can touch the reference count of many of those pointers.
Also in multiple CPU core architectures, the atomic update of a reference count might become not slightly costly at least, but actually really costly, if the multiple core are currently accessing the same memory area.
However shared pointers are simple and safe to use, whereas the assignment properties of auto pointers is confusing, and can become really dangerous.

Smart pointers usually is frequently used just as a synonym of shared pointer, but actually covers all the various pointers implementation in boost, including the one that's similar to shared pointers.

Smart pointer is a term that encompasses all types that behave like pointers, but with added (smart) semantics, as opposed to raw T*. Both unique_ptr and shared_ptr are smart pointers.

new operator vs operator new

The method for dynamically allocating memory is to use the new operator. This operator can be overloaded. To distinguish between the default operator and an overloaded version, the default is called “new operator” and the overloaded version is called “operator new”

The new operator calls a function to perform the requisite memory allocation, and you can rewrite or overload that function to change its behavior. The name of the function the new operator calls to allocate memory is operator new.

The new operator: C++ supports dynamic allocation of objects using the new operator. The new operator allocate memory for objects from a pool called the free store. The new operator calls the special function operator new.

operator new: If the request is for zero bytes of storage, operator new returns a pointer to a distinct object (that is, repeated calls to operator new return different pointers). If there is insufficient memory for the allocation request, operator new returns NULL or throws an exception. The first argument to operator new must be of type size_t (a type defined in STDDEF.H), and the return type is always void *.
When do you call copy constructors?
Copy constructors are called in these situations:
i.)when compiler generates a temporary object
ii.)when a function returns an object of that class by value
iii.)when the object of that class is passed by value as an argument to a function
iv.)when you construct an object based on another object of the same class

Name the implicit member functions of a class.

i.) default ctor
ii.) copy ctor
iii.) assignment operator
iv.) default destructor
v.) address operator

Explain storage qualifiers in C++.

i.) Const - This variable means that if the memory is initialised once, it should not be altered by a program.
ii.) Volatile - This variable means that the value in the memory location can be altered even though nothing in the program code modifies the contents.
iii.) Mutable - This variable means that a particular member of a structure or class can be altered even if a particular structure variable, class, or class member function is constant.

Explain dangling pointer.

When the address of an object is used after its lifetime is over, dangling pointer comes into existence. Some examples of such situations are: Returning the addresses of the automatic variables from a function or using the address of the memory block after it is freed.

In what situations do you have to use initialization list rather than assignment in constructors.

When you want to use non-static const data members and reference data members you should use initialization list to initialize them.

When does a class need a virtual destructor?

If your class has at least one virtual function, you should have a virtual destructor. This allows you to delete a dynamic object through a baller to a base class object. In absence of this, the wrong destructor will be invoked during deletion of the dynamic object.

What is the type of “this” pointer? When does it get created?

It is a constant pointer type. It gets created when a non-static member function of a class is called.

Explain Stack unwinding

Stack unwinding is a process during exception handling when the destructor is called for all local objects between the place where the exception was thrown and where it is caught.

Program to an interface, not an implementation

Programming to Interfaces rather than concrete implementations is a good design practice.

While developing an application we will divide the whole system into sub-systems and one sub-system will talk to other sub-system to complete the whole task.
Each sub-system is separated in such-way that it should be responsible for certain kind of tasks.

So when you design the architecture of a system, first we need to divide the whole system into several sub-systems and we need to clearly define the responsibilities of each sub-system without bothering about how to implement those responsibilities. In this way we develop a prototype/blueprint of our system to be developed.

Once we have a prototype, then we need to turn these design into coding. To do this we need to use Interfaces rather than classes as we don't know the actual implementations.
And also this approach will expose the contract between the sub-systems on how to interact with each other.

Exception Handling

An exception is a problem that arises during the execution of a program. A C++ exception is a response to an exceptional circumstance that arises while a program is running, such as an attempt to divide by zero.
Exceptions provide a way to transfer control from one part of a program to another. C++ exception handling is built upon three keywords: try, catch, and throw.
  • throw: A program throws an exception when a problem shows up. This is done using a throw keyword.
  • catch: A program catches an exception with an exception handler at the place in a program where you want to handle the problem. The catch keyword indicates the catching of an exception.
  • try: A try block identifies a block of code for which particular exceptions will be activated. It's followed by one or more catch blocks.
Assuming a block will raise and exception, a method catches an exception using a combination of the try and catch keywords. A try/catch block is placed around the code that might generate an exception.

Exceptions can be thrown anywhere within a code block using throw statements. The operand of the throw statements determines a type for the exception and can be any expression and the type of the result of the expression determines the type of exception thrown.