Wednesday, 26 February 2014

Undefined behavior in Programming

 



Undefined behavior is nothing but  we cannot deduct how a application will react to some situation. Code will work fine without any semantic errors. But the output of the code was unpredictable.

C and C++ are most appropriate examples of such behavior.

For instance. If you declare a variable in c or c++, then you can use that variable without initializing it. Code will work fine for that situation. But the output of the code was unpredictable. It vary time to time. This behavior is called undefined behavior

HOW TO OVERCOME UNDEFINED BEHAVIOUR IN PROGRAMMING:
It is in the hands of the programmer to overcome the undefined behavior in programming.
There are also some tools available like valgrind,gdb  for pointing out these issues. Then always compile with –Wall (if your compiler is gcc) to overcome some of this issues.


WHY UNDEFINED BEHAVIOR:
The undefined behavior is allowed mainly to increase the efficiency and performance.
For eg consider the order of evaluation of function parameters.  some architecture will be fast when reading from left to right and vice versa.

DIFFERENCE BETWEEN UNSPECIFIED AND UNDEFINED BEHAVIOUR:
The undefined behavior and unspecified behavior both are different.
unspecified behavior is something where the implementation is undefined but it should conforms to standard. For eg evaluation of function parameters. Here the order of evaluation of function parameter is unspecified. It is up to the implementation.

Undefined behavior is something where we cannot predict the results. Here although it conforms to standards, here the result are unpredictable.
UNDEFINED BEHAVIOUR IN C AND C++:
1. USING THE VARIABLE WITHOUT INITIALIZATION:

when you use a variable without initializing then it leads to a undefined behavior. Because it use the garbage value. so its result is unpredictable at all time.

int a,b;b=a+10; // garbage value to b

2. FUNCTION WITHOUT A  RETURN STATEMENT:
As per semantics, the compiler will allow without return statement even if the return type of the function is other than void say int,float,double etc., But this will result in unpredictable results always.
int myfunction(){ // No return statement}int main(){ int a=myfunction();
}


3. VARIABLE SIZE OVERFLOW:

When the variable size overflows  then it will result in unpredictable results. For eg size of MAX_INT  is 65535. Then the following code will result in overflow. Here the if value of N is greater than the MAX_INT, the value of i never fails in the condition i<N because after variable i reaching the limit, it starts to count from 0 (INT_MIN). So the loop never ends.
for(i=0;i<N;i++){//Loop runs infinite times if N is greater than MAX_INT}

4. DEREFERENCING A DANGLING POINTER

When you free a memory after malloc. The memory will get freed. But the pointer will point to the same location even after the freeing process. Since the pointer refers to the freed memory. the pointer is called as dangling pointer. When you dereference a dangling pointer you will get the unpredictable results always.
char *p=(char *)malloc(sizeof(char)*5);
free(p); // Here memory deallocated but p points to same location. So p is a dangling pointer
*p=*p+1; //Unpredictable results
5. DEREFERENCING THE WILD POINTER:

The wild pointer is nothing but the uninitialized pointer. When you use a uninitialized pointer then it will return some garbage value
int *p;*p=*p+1; // undefined behaviour
6. RETURNING A ADDRESS OF LOCAL VARIABLE

When you return the address of the local variable, then it is also an undefined behavior.
int callme(){int a;return &a; // Scope of dies here. No use of passing a’s address}

7. CHANGING THE CONST CHAR POINTER
When you attempt to change the const char pointer then the result will be unpredictable.

char *p=”HELLO”;p[1]=’S’; // Lead to segmentation fault

in above snippet, the HELLO is string literal stored in read only memory. Here we are attempting to change the read only memory. Code works without any compilation error. But the segmentation fault error will occur.