-3

I have seen it sometimes. But I did not understand the meaning of this term.

koustav das
  • 3
  • 1
  • 3

2 Answers2

2

Quoting Wikipedia,

In computing, a segmentation fault (often shortened to segfault) or access violation is a fault, or failure condition, raised by hardware with memory protection, notifying an operating system (OS) the software has attempted to access a restricted area of memory (a memory access violation).

For example, the following will result in a segmentation fault on machines with memory protection (such as the one you are using right now):

char* s = NULL;
size_t len = 0;
for (size_t i=0; s[i]; ++i) { // SEGFAULT: s[i] tries to dereference a NULL
   ++len;
}

Quoting Wikipedia,

In computing, a core dump, crash dump, memory dump, or system dump consists of the recorded state of the working memory of a computer program at a specific time, generally when the program has crashed or otherwise terminated abnormally. In practice, other key pieces of program state are usually dumped at the same time, including the processor registers, which may include the program counter and stack pointer, memory management information, and other processor and operating system flags and information.

Having a core dump allows one to debug a program that crashed earlier. Note that the actual creation of the code dump is often skipped (for privacy, security and space reasons) unless a setting is enabled.


There was a discussion in the comments as to whether it's fair to say that a program crashes or not.

The process is what is receiving the SIGSEGV signal, and it's what is being dumped, so it's fair to say "the process crashed".

As the driving force of the process, the instructions being executed by the process can be said to cause the crash. The collection of these instructions are known as "the program", so it's fair to say "the program crashed".

Both expressions are valid and refer to the same event.

ikegami
  • 367,544
  • 15
  • 269
  • 518
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/202757/discussion-on-answer-by-ikegami-what-does-the-term-segmentation-fault-core-dum). – Samuel Liew Nov 20 '19 at 20:58
2

It happens when you are trying to access an invalid address. It may look like this:

int* a;
*a = 4;

Or this:

struct s {
  int a;
}

int main() {
  struct s* foo;
  foo->a = 4;
}

The variable a in the first example and foo in the second example are not initialised. They don’t point to any memory. If you try to assign a value to it, a Segmentation Fault will occur and your program crashes.

The correct way is to first initialise it.

int* a = malloc(sizeof(int));
*a = 4;
// ...
free(a);
Henry Fung
  • 380
  • 3
  • 12