Difference between Call by value Vs Call by reference

Call by ValueCall by Reference
In this case, a copy of actual data is created and passed to the function.In this case, address of actual data is passed to the function.
In this case, any changes in data modified by the function will not be reflected to actual data.Since, address of actual data is available with the function, hence any change in data will be reflected to actual data also.
Since in this case, copy of actual data is created, hence for large size of data its inefficient as it will need more memory and extra CPU cycles to copy all data.Since, in this case, only address of actual data is passed, hence it’s not dependent of data size as pointer size will always be equal to size of an integer.

For more information related to Call by value and Call by reference Click here.

Difference between Call by value Vs Call by reference Read More

Call by value and Call by reference explained with simple example

This is one of very basic concept of C/C++ programming language which is kind of building block of any program or software. Often unknowingly programmers make mistakes in these concept and introduce bugs in softwares 🙂

Basically in C, there are two ways to pass data to a function:

1) Call by value: In this case only value will be passed to the function and the function will store this value in its own local variable. This function will work on this copied data (not original) and set some new value based on some operations. But since this function is working on copied data and not on original data, hence it can’t update the original data in this method.

2) Call by reference: In this case address of the data is passed to the function and function will define a pointer to access this memory location. Since, this function has direct access to the original data memory location, hence it can update the value of original data. Thus, this method is called Call/Pass by reference as we are passing reference of the memory instead of direct data.

(more…)

Call by value and Call by reference explained with simple example Read More

Difference between constexpr vs inline functions

constexpr functions are implicitly inline functions only. However, inline functions are simply removes the overhead of function calls by pasting the function body at the called place. But, these code will be executed at run time. Hence, performance gain will be because of reduced function calls.

constexpr functions are evaluated at the compile time to ensure no computation at runtime and hence gives better performance optimization.

Any member functions can be made inline. However, only those functions whose return types and argument types are all of literal types.

For more information related to constexpr click here.

Difference between constexpr vs inline functions Read More

Difference between constexpr vs const

constexprconst
If a member function is declared as constexpr then it will be treated as const as well.If a member function is declared as const then it will not be treated as constexpr.
If a variable is declared as constexpr then it will be treated as const as well.If a variable is declared as const then it will not be treated as constexpr.
constexpr is mainly used for optimization purpose where computation can be done at compile level to optimize run time performance.However const is mainly used to ensure that there are no accidental changes done by the member method to member data.

For more information related to constexpr click here.

Difference between constexpr vs const Read More

constexpr explained with simple example

In C++11(Introduction to C++11), constexpr is added which improves the run-time performance of the code as it allows the compiler to compute the associated code at compile time if possible. constexpr specifier can be used with both variables and functions where only constant expressions are used which can be evaluated at compile time.

Few important points:

  1. A constexpr function return type and argument type must be literal types.
  2. A constexpr function cannot be virtual but it can be recursive.
  3. A constexpr function cannot have uninitialized variables. Till C++11 it can have only one return statement while from C++14 it can have more than one return statement.
  4. Any variable can be declared as constexpr, if it is initialized. In case of constructor initialization, the constructor also must be declared as constexpr.
  5. constexpr can also be used with references if the referenced object is initialized.
  6. Any constexpr function can only call other constexpr functions.
  7. constexpr function can also be used with non-const variables. No need to define two different functions for same.

(more…)

constexpr explained with simple example Read More

Difference between Copy assignment operator vs Move assignment operator

 

Copy Assignment OperatorsMove Assignment Operators
Copy assignment operator takes lvalue reference as an argument.Move assignment operator takes rvalue reference as argument.
Copy assignment operator create a new object from passed object by copying each and every item into a new memory location.Move assignment operator create a new object by using as much memory from passed object.
Since, copy assignment operator use a lot of new memory allocation(for new object). Hence, copy constructors gives bad performance compared to move constructorsSince, move assignment operator use most of memory blocks from passed object. Hence, move constructors gave better performance compared to copy constructors.
Since, copy assignment operator doesn’t make any change to passed object. Hence, passed object can be used after copy operations also.Since, move assignment operator utilizes memory blocks from passed object. Hence, passed object can’t be used after move operations.

For more information related to move assignment operators click here.

Difference between Copy assignment operator vs Move assignment operator Read More

Difference between Copy constructor vs Move constructor

 

Copy ConstructorsMove Constructors
Copy constructors takes lvalue reference as an argument.Move constructors takes rvalue reference as argument.
Copy constructors create a new object from passed object by copying each and every item into a new memory location.Move constructors create a new object by using as much memory from passed object.
Since, copy constructors use a lot of new memory allocation(for new object). Hence, copy constructors gives bad performance compared to move constructorsSince, move constructors use most of memory blocks from passed object. Hence, move constructors gave better performance compared to copy constructors.
Since, copy constructors doesn’t make any change to passed object. Hence, passed object can be used after copy operations also.Since, move constructors utilizes memory blocks from passed object. Hence, passed object can’t be used after move operations.

For more information related to Move constructors click here

Difference between Copy constructor vs Move constructor Read More

Move Assignment Operator explained with simple example

In C++11 (Introduction to C++11) , move assignment operator are added which can improve any code’s performance drastically. Typically a move assignment operator is same as copy/move constructor but before reusing the memory blocks from passed object, it releases all the memory which is owned by this object. Since, memory reallocation is avoided in case of move assignment operators. Thus it gives better performance.

However while implementing move assignment operators we need to take care of one important point to ensure that the original object can be correctly destroyed.

Let’s take an example to see how this memory reallocation is avoided in case of move assignment.

(more…)

Move Assignment Operator explained with simple example Read More