Sunday, 30 September 2018

Variable Argument List Function

  
A function that can accept any number of arguments, means number of arguments are not predefined that is a variable argument list function . In a typical function we can only pass a fix number of arguments while calling.

For exp:-


Above is the gcc screenshot of a fixed argument function sum(), sum is a user defined function that can accept only two arguments. If we call sum() with more than two or less then two argument then it will generate Linker error.

We cannot call sum as sum(a,b,c).While doing so compiler generates error as below :-



Question is can we create our own function like printf(),scanf() that have no fixed number of arg.
*Can we create our own sum() function** which can accept n number of arguments ?
We can achieve this by using Variable length argument feature.


How to do that ? :-

******************************************************************************
Variable argument length(Variable argument list) feature allows the function to receive n number of arguments in a function.

How to define a variable argument function?

There are two ways :-
  1. We can pass a fixed argument that argument indicates how many variable arguments are there.
  2. Pass a null at the end to indicate number of variable arguments.
*********************************************************************************
Irrespective of the two ways.

We define a function using (...) three dots as one of the arguments to indicate its of variable argument length.
Exp:-
int sum(int,...);
int sum(char *n,..);

 -First we will discuss method 1



To understand method one we can undergo linux man page of va_start
cmd$  man va_start


Step 1: Create a variable(pointer similar to char*) of type va_list

va_list v;


Step 2 : Call function va_start to specify total no. of arguments to fetch .

va_start(v,n);

For the above program:
va_start(v,n)  n being the total no. of arguments starting from va_list v.
this value becomes va_start(v,3) for the sum(3,i,j,k) specifying to traverse from va_list v to n .

Step 3: Use va_arg to fetch the values of the type of argument passed as 2nd argument (integer in the case of the above program)

s=s+va_arg(v,int);

For the above program this function is used to fetch the value of all the variable argument ** provided the type of argument
va_arg( v , int ) Integer being the type of argument in function.
Implies function sum(3,i,j,k)  > sum(3,50,60,70) the values of the variable argument list is fetched.

Struct Hack

Blog post is related to structure hacking, its a trick of structure to reduce the size of structure.

We must understand few basic thing before discussing Struct Hack:-

What is char a[0]={'1','2','3','4'};
Its an array but what is the sizeof 'a' and what is the sizeof 'a[0]' .
sizeof(a)        =               0
sizeof(a[0])   =               1

Now :-

struct student
{
int      rollno;
int      name_length;
char   name[0];                             // some compiler provides error so there we can use name[1]
}stdnt;


sizeof stdnt is 4+4+0 = 8 .

When we create an array of zero length then its size is also zero, because size of an array depends upon the index multiplied by the size of type. When we create an array of zero length in structure it must be the last member of structure. This technique is known as "Struct Hack".




 Above is the screenshot of a working gcc program.

Use Of Struct Hack :-

It is use to create variable,according to the required length at run time.

In the above code name_length is not constant for every student, if we use pointer the 4 byte of data for pointer is also added and this is the benefit of Struct Hack. 
















Monday, 24 September 2018

Storage Classes (Register)

Register storage classes are used to stored the local variable in CPU register not in memory.

Default Value :-

  • Default Value of register storage classes is garbage.
  • We can declare it locally
Memory :-

  • Register storage class variable are always stored in CPU register
Scope :-
  •  Scope of register storage class variable is within a block or braces.  
Life :-

  • Life of register is same as an auto storage class.

Important Note  :-

We can not use the address of register storage classes variable because it stored in CPU.
Register storage class is fastest of all four storage type.

Function arguments are of register storage class.




Wednesday, 5 September 2018

Storage Classes 3 (Extern)

Extern Storage Class :-

Default Value :-

  • Default value of extern variable is zero (0).
  • We can declare global as well as local.

Memory :-

  • It stored in data section
Life :-
  • Life of extern variable start when program is loaded and end when program is execution completed.
Scope :- 
  • If we declare variable as global then all the file (which compile at a time) and function we can use easily.
  • If we declare local then we can access within the function only.


Important Note :- 

                           Global variable default an extern storage classes.
                           Default the function are extern but not variable.
                           By using extern we give the information to variable that its declaration may be present in other file.
                           If declaration is not present in other file as well so it will give an error.