Spread the love

Errors

____________________________________________________________________________________________________

 

Stack overflow

A stack overflow error can occur due to excessive memory usage on the call stack, which is where information is stored relating to the active subroutines in the program. The call stack has a limited amount of memory available to it.

                 Typically, when a stack overflow error occurs, the program crashes and can either freeze or close the program. Any unsaved data or work is lost. 

Causes of Stackoverflow:

1). The most common cause of stack overflow is excessively deep or infinite recursion. As we know the procedure of function calling, each time we call any function(subroutine), its returning address, parameters, etc. are stored in stack(specific area of RAM). So as the function calling increases, its data also increased and at the end memory of stack is full and after that stack overflow occurs.

int hello( )
{

     return hello();
}

 

 2). The other major cause of a stack overflow results from an attempt to allocate more memory on the stack than will fit, for example by creating local array variables that are too large. For this reason some authors recommend that arrays larger than a few kilobytes should be allocated dynamically instead of as a local variable.

int foo( )
{
       double x[1000000];

}

 

Segmentation fault

A segmentation fault or access violation occurs when a program attempts to access a memory location that is not exist, or attempts to access a memory location in a way that is not allowed.

  /* "Array out of bounds" error 
       valid indices for array foo
       are 0, 1, ... 999 */
  int foo[1000];
  for (int i = 0; i <= 1000 ; i++) 
    foo[i] = i;

Here i[1000] not exist, so segfault occur

 

Causes of segmentation fault:

  • it arise primarily due to errors in use of pointers for virtual memory addressing, particularly illegal access.
  • De-referencing NULL pointers – this is special-cased by memory management hardware.
  • Attempting to access a nonexistent memory address (outside process’s address space).
  • Attempting to access memory the program does not have rights to (such as kernel structures in process context).
  • Attempting to write read-only memory (such as code segment).

Memory leak

In computer science, a memory leak occurs when a computer program incorrectly manages memory allocations in such a way that memory which is no longer needed is not released.

                 Memory is allocated but not released eventually causing the system to page virtual memory to the hard drive which slowing the application or crashing the application when than the computer memory resource limits are reached. The system may stop working as these limits are approached.

/* Memory Leak */

#include <stdlib.h>
#include <string.h>

char *oldString = "Old String";
char newStrig = strdup(oldString);
if(newString == ENOMEM) ... // Fail!!!!

free(newString);

 

Fragmentation or Memory Corruption

Memory when altered without an explicit assignment due to the inadvertent and unexpected altering of data held in memory or the altering of a pointer to a specific place in memory.

Example 1:

Overwrite beyond allocated length - overflow.

char *a = malloc(128*sizeof(char));

memcpy(a, data, dataLen); // Error if dataLen too long.

Example 2:

Index of array out of bounds: (array index overflow - index too large/underflow - negative index)

ptr = (char *) malloc(strlen(string_A)); // Should be (string_A + 1) to account for null termination.

strcpy(ptr, string_A); // Copies memory from string_A which is one byte longer than its destination ptr.

If you like this Article, then don’t forget to Click on Social likes buttons.