Segmentation Fault(segfault)

Jan 14, 2025
  • Segmentation fault or segfault is a type of error generated by programs.
  • Segmentation fault happen when program tries to access the memory that doesn't belong to it.
  • Segfault occurs in those programming languages where you can directly access and manipulate the memory.

Example

  1. Referencing a NULL pointer:
int *p = NULL;
*p = 1;
  1. When program tries to access read-only memory:
char *str = "hello"; // constant strings are read only.
*str = "bao"; 
  1. Dangling Pointer:
char *p = NULL;
{
	char c;
	p = &c;
}
*p = 1; // Now p is dangling pointer as it cease to exist after upper block.
  1. Accessing the address that doesn't exists in the memory.
https://ashraddhansh.github.io/posts/feed.xml