Count columns to be deleted to make each row sorted in C++

C++Server Side ProgrammingProgramming

The abnormal behavior of C++ programs often leads to program crash. You may have encountered problems like Segmentation fault, Aborted, Floating point exception etc. Following are sample programs that may help you to understand the reasons for a C++ program crash.

Exceptions

Exceptions in C++ are responses of a program when it encounters an abnormal condition. The program crashes due to such exceptions if they are not handled properly using try-catch blocks. Following program crashes due to divide by zero exception −

Example

#include <iostream>
int main(){
   int num1=10;
   int num2=0;
   int quotient=num1/num2;
   printf("\n Quotient is: %d",quotient);
   return 0;
}

Output

Floating point exception (core dumped)

Buffer Overflow

Buffer is a temporary storage area. When a program while writing data to the buffer exceeds the size that buffer can hold, the extra data goes out of buffer’s boundary. The data overwrites to adjacent memory locations. The following program changes its behavior when input exceeds the size, variable num can hold.

Example

#include <iostream>
#include <string.h>
int main(){
   int num=100;
   std::cout<<"\nValue for num:"<<num;
   char c[2];
   strcpy(c,"abcdefghijklmnopqrstuvwxyz");
   std::cout<<"\nValue for c:"<<c;
   return 0;
}

Output

Value for num:100
Segmentation fault (core dumped)

Stack Overflow

The Stack Overflow problem occurs when the call stack pointer exceeds the stack bound. The stack consists of a limited amount of space. When a program uses more space than available on the stack then the stack is said to overflow and cause the program to crash. The most common cause is infinite recursion.

The following program contains infinite calls to function factorial(). The return statement is not proper in this case.

Example

#include <iostream>
#include <string.h>
int factorial(int num){
   if(num==0)
      return 1;
   else
      return(factorial(num));
}
int main(){
    int n=10;
   int fact=factorial(n);
   std::cout<<fact;
}

Output

Segmentation fault (core dumped)

Segmentation Fault

Segmentation fault or core dump occurs when a program tries to access the memory locations that does not belong to it. In the following program, pointer str is incrementing and appending memory indefinitely.

Example

#include <iostream>
int main(){
   char *str;
   char name[]="iostream";
   str=name;
   while(1)
      (*str++)='a';
}

Output

Segmentation fault (core dumped)

Memory Leaks

Memory leaks occur when dynamically allocated memory is never deallocated. The memory must be freed when it is no longer in use. If we continuously allocate memory again and again, then these memory leaks over time will increase and eventually would cause the program to crash. The repetition of poor code like below causes memory leaks −

Example

#include <iostream>
int main(){
    int *node;
   node = (int *) malloc(9999999);
   // free(node);
}
raja
Published on 28-Jul-2020 14:03:17
Advertisements