Tuesday, 31 December 2013


1. Size of is an unary operator. unary operator is a operator that acts upon the single operand at a time Eg: postincrement, logical not and so on

2. size of can be used as following formats
sizeof(int), sizeof int, sizeof i,sizeof(arr),

3. It is used to calculate the size of the data type.

4. Size is returned in the bytes which is of the form “size_t”. size_t is an unsigned type which is always positive.

5. Sizeof as return type
You can also return the sizeof array using the size_t as datatype.
size_t  my_function()
{
  size_t mysize=sizeof(int);
  return mysize;
}

6. size of can be applied to any datatypes such as basic, derived, Enumerated and void

7. Need for sizeof
Even though the size of the datatypes are predefined by the c standard already Not every implementation of the compiler follows the correct standard. So size of is useful at a time of allocating the dynamic memory. so whenever we need to allocate the memory dynamically we should use the sizeof function to find the size of the datatype.

8. We cannot use the size of operator in preprocessor expressions. Because the preprocessor does not know about the sizeof

9. Difference between sizeof and sizeof()
we can use sizeof as both keyword “sizeof int”and function type sizeof(int). Although we use the sizeof as function type it doesnot mean that it is an function. Because according to the C standard function should only take arguments as value not as datatype.

10. When sizeof is applied to the static array or structure, it will  return the size of  the entire array and entire structure.

11. Using Sizeof to find the number of elements in a array:
sizeof(arr)/sizeof(arr[0]);
You can also write a macro to that defines the size of 
#define arraysize(array) sizeof(array)/sizeof(*array)
In one of the above statement I have mentioned that c preprocessor does not know about  the sizeof. But here #define just expands the macro at runtime. Here the sizeof will work.

12. Cannot apply sizeof to structure without members:
Applying sizeof is invalid in the following cases.
struct x;
int arr[];
Although the above declaration is leagal in C, Applying sizeof is illegal here. Because without knowing the member of the array and structure it cannot find the size.

13.  C++ 11 standard allows to find the sizeof to the structure member too.
For eg: 

struct s
{
  int a;
  int b;
}
int main()
{
  cout<<sizeof(s::a);
}

From childhood, we learned about  prime number program. But still we are not efficiently programmed that one. 
Here are the some efficiency handled for prime number program.
This program is designed for finding the prime number of  one followed by ten digits.







#include<iostream>


#include<math.h>

using namespace std;

int main()

{

   bool flag=true;

   unsigned long  long int start=1000000000001ULL,end=1000000010000ULL,i,k,sqrt_num;

   for(i=start;i<=end;i=i+2)

   {

      flag=true;

      sqrt_num=(unsigned int)sqrt(i*2);

      for(k=3;k<=sqrt_num;k+=2)

      {

         if(i%k==0)

         {

            flag=false;

            break;

         }

      }

      if(flag)

      {

         cout<<"\n PRIME:"<<i<<"\n";

      }

   }

   return 0;
}



EFFICIENY HANDLED 1:
bool flag=true;
Use bool datatype instead of int. Because int takes more bytes than bool. This may be simple but saves some bytes.

EFFICIENY HANDLED 2:
sqrt_num=(unsigned int)sqrt(i*2);
There is a rule for prime number “If the number cannot be divisible  by below  the sqrt double that number then it is an prime number”.
So check up to the sqrt of double that number.

EFFICIENY HANDLED 3:
 unsigned 
use unsigned datatype. Because prime number cannot be negative. It saves the memory.

EFFICIENY HANDLED 4:
Iterate the loop starting with 3. Because expect 2 there wont be any even prime number. so no need to check whether number is divisible by 2.

EFFICIENY HANDLED 5:
  for(i=start;i<=end;i=i+2)
Don’t check the prime number for even numbers. Because other that 2, No even number is prime number.

EFFICIENY HANDLED 6:
  for(k=3;k<=sqrt(i*2);k+=2) // Don’t do this mistake
When you do like this then for every iteration it will call the sqrt function for checking the condition.
It will eat of much off the execution time.

In addition to that there are lot of efficiency can be handled. There is lots of algorithm for prime number such as Sieve of Eratosthenes and so on. This article is focuses only on the writing basic prime number program with efficiency.

Wednesday, 25 December 2013

Library are precoded, precompiled files that are ready for use.

Programmers in addition to their own source code they will use the others code by including the file in their source code. It is possible to do like that if only one or two programs have to be referenced. In order to include multiple files it becomes more complex.

So, UNIX allows the property of creating the library objects. There are two kinds of library objects.

1. Static library (.a extension)

2. Dynamic Library (.so extension)

Static library:


Archive library is a static library. These files end withextension .a , then archive library are called along with the program so they become the part of the program execution. The archive library are smaller in size when compared to the shared library.

You can create your own library files by using the following,

STEP1: Create your own object file by compiling yourprogram.

gcc –c filel.c file2.c

The command line option –c is given to produce the object file. The above command produces the two object file file1.o and file2.o

STEP2: ARCHIVE THE FILES TO BE USED FOR LIBRARY OBJECT

ar -q libmyarchive.a file1.o file2.o

The above command creates the library libmyarchive.a with two object files file1.o and file2.o. The command line option q is used for quickappend. It is used to append the member to the archive.

To view the members of the obect use the

ar -t libmyarchive.a

This will display the members of the libmyarchive such asfile1.o and file2.o

Other useful command line options:

-d : deletes the member of archive

-x : extracts the member

-V : modifies the version number of the archive

After the successful creation of libray you can include in asource file by the following

gcc newfile.c libmyarchive.a

gcc newfile.c –lmyarchive

The second option will work if you include the library underthe /opt/usr/lib and remember that the file name you have created should prefixwith a lib.

Using ranlib option:

The ranlib option is used for creating the index for the archive file you have been created. The ranlib stands for randomize library.

If you have 100 object file in archive then loader needs to look at all the 1000 files to load the library object. In order to speed up the process ranlib program in GNU creates the index for the archive. Its like the table of content in a book.

ranlib libmyarchive.a

Dynamic library:

Shared library is a dynamic library. These files end with extension .so, then shared library is not called along with the program, they are called whenever they are needed. The archive library are larger in size when compared to the archive library.

STEP1 : Compile the program as PIC:

So are you wondering what is the PIC. PIC is the process independent code. It means it will not depend on the memory where the code was stored. All shared library objects should be a PIC.

g++ -c –fpic test1.cpp

Yes fpic is the command line option that is used for creating the process independent code and PIE stands for process independent executable.

STEP2: Creating the shared Library using –shared

Now you have to modify the .o file into .so file. I mean object file in to the shared object file. To do so,

g++ -shared –o libtest_shared.so test.o

Here libtest_shared.so is created from file test.o

STEP3: Linking and exporting TO LD_LIBRARY_PATH(Loader Library path)

Now you have to inform the compiler about the shared library about where he is present to access it. For that unix uses the LD_LIBRARY_PATH environmental variable.

SET LD_LIBRARY_PATH=’/path/to/shared/library/’

After informing the compiler you should export the LD_LIBRARY_PATH to your current shell.

Try to learn about rpath too. It is the command line option that is used to give the path of the shared library at run time.

library naming convention:

Library are stored under the /usr/lib/. Every library object is prefixed with a lib.

Eg: libm.a (maths libary),  libpthread.a (thread library)

They are called while compilation by the following format.

g++ file1.cpp –lm –lpthread



Saturday, 7 December 2013

Although new programming languages are evolving every day, still C++ plays a vital role in every part of our life. Each and every application you were using in day to day life is made of C and CPP.
Even though other program are easy to use people depend mostly on cpp because of the following reasons,

 #  Performance

 #  Execution speed

 Picture2.png
No language compromised the above two parameters with C and C++.
Here are very few areas where c++ is used.


Area
Application
Email Client
Mozilla Thunderbird
Operating System
Most Mac OS, Symbian OS and even windows.
Music
Winamp media player
Database
Mysql
Browser
Mozilla firefox , Chromium browser
Search engine
Google(few parts)
Kernel
Unix kernel completely.
Emmbedded softwares
Washing machine, Microwave oven
Social Network
Facebook
Application software
Word, Excel

 Be proud for being a C and CPP programmer..

Friday, 29 November 2013

Are you still keeping the mobile  numbers, birthday, Vehicle numbers as a password to your online accounts. First of all i will tell you the disadvantages of keeping that

When you share a password with your friend for emergency he will easily remember the password when he use them.

So are you worrying in keeping a secret password even when you share your password to your friends?



Here the Tricks,

Keep the password like this..

!VHKRG#

The above password  is my name (VINOTH KUMAR G).
Wonder how i make it.

V- First Letter of my First name
H- Last Letter of my First name
K- First Letter of my Last name
R- First Letter of my Last name
G- Initial.

Form passwords like this. Be secured.