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
- Referencing a NULL pointer:
int *p = NULL;
*p = 1;
- When program tries to access read-only memory:
char *str = "hello"; // constant strings are read only.
*str = "bao";
- Dangling Pointer:
char *p = NULL;
*p = 1; // Now p is dangling pointer as it cease to exist after upper block.
- Accessing the address that doesn't exists in the memory.