Saturday, July 16, 2016

What are the Smart pointers in C++

Smart pointers

Smart Pointers:


Smart pointer are objects that looks & feel like pointers, but are smarter. To be smarter than regular pointers, smart pointers need to do thing that regular pointers do not. The most common bugs in c and C++ are related to pointers and memory like dangling pointers, memory leaks, allocation of memory failures and others. A smart pointers take care of these things. [std::auto_ptr]

Saturday, May 28, 2016

Calling Convention in C and C++

Calling Convention in C and C++

Calling Conventions:

Keyword Remarks
__cdecl Default calling convention for C and C++ programs.
__stdcalll Used to call win32 API functions.
__fastcall Argument to functions are to be passed in register when possible.
thiscall Default calling conventions used by C++ member functions that do not use variable arguments. It is not explicitly defined in a program, because it is not a keyword.


Keyword Stack Cleanup Parameter Passing
__cdecl Caller R To L
__stdcalll Callee R To L
__fastcall Callee Stored in registers, then pushed on stack
thiscall Callee Pushed on stack, this pointer stored in ECX

Note: __cdeclspec (extended attribute) declarator

If specifies that an instance of a given type is to be stored with a Microsoft specific storage class attribute.

Monday, May 23, 2016

What is Deep Copy and Shallow Copy

What is Deep Copy and Shallow Copy
The term "Deep Copy & Shallow Copy" refers to the way objects are copied.

Shallow Copy:


Shallow Copy also called "Bitwise Copy" simply copies chunk of memory from one location to another. For example memcpy().

A Shallow Copy of an object copies all members field values. It uses default copy constructor & assignment operator.

It works well with if the fields are values but not if the fields that points to dynamically allocated memory.

Deep Copy:


Deep copy also called "Member Wise Copy" copies all fields, & make copies of dynamically allocated memory pointed by the fields. To make a deep copy must write a copy constructor & overload the assignment operator.