A segmentation fault ( SEGFAULT ) occurs when you are trying to access memory which you should not be trying to access ( the memory which you have not allocated ) . But, you might ask which memory are we not allowed access to ? or when are we trying to access memory which does not belong to us ?
The answer to the first is that you are not allowed to access memory which has not been allocated yet. If you try to access them, then it will lead to a SEGFAULT.
For the next part,
Well. there are many cases, but the common ones are
These are the basic and most common causes of a Segfault.
Now lets explain the dereferencing a NULL pointer
If you try something like
int *ptr = NULL;
*ptr = 4;
You are trying to change the value using *ptr
, but ptr
is a NULL pointer, and this might cause you a SEGFAULT
An uninitialized pointer is a pointer which you have not referenced to anything
char *ptr;
*ptr = 'a';
With this, you will be trying to give the value a
to *ptr
, but as ptr
is not initialized, it is pointing to some memory which you do not have access to.
Now lets move on to the Dangling Pointers
int *ptr;
if ( 1 )
{
int a = 5;
ptr = &a;
}
*ptr = 6;
The variable a
has a scope inside the if block. Once you go out of the if block, a
is removed from memory and ptr
now points to something which no longer exists. When you try to change the value like in the code, you will get a SEGFAULT.
Next is a Buffer Overflow.
char a[8] = "INCORRECT";
a
can only store 8 bytes of data, but we are trying to store 10 bytes ( ‘I’ ‘N’ ‘C’ ‘O’ ‘R’ ‘R’ ‘E’ ‘C’ ‘T’ ‘\0’ ). This can also lead to SEGFAULT.
Next is Stack Overflow ( Please do not mistake StackOverflow.com for this )
The stack is the part of memory which holds the return address at function calls, arguments passed to the function, local variables for the function, etc. But there is a limit to the amount of memory available in the stack. So if you try something like
int myfunc()
{
return myfunc();
}
it is an infinite recursion, and each time the function returns, memory from stack is used to store the return address of the function. Since this is infinite, the memory in stack will run out and you get a Stack Overflow.
Now, changing a constant using pointers.
char *ptr = "Yay";
*ptr = 'Oh no" // SEGFAULT
ptr
points to a const string, but you try to change it, leading to SEGFAULT.
Accessing free’d memory is just like dangling pointers. When you try to access a dereference a pointer which points to something which has already been deleted or Freed, you get a SEGFAULT.
These are the general causes of a SEGFAULT.
You can also read these links for some reference
What is segmentation fault?
Segmentation fault, Wikipedia
Segmentation Fault (Core dumped) in c++
The third link provides an example of Segmentation Fault (Core dumped) . The problem there is caused mainly due to trying to access a part of string which is out of bound.
Segmentation Fault (core dumped) C++ This is also an example.
You can find many such examples of this if you Google it.
But this is as far as I can tell you as you have not shown any code. You are most likely trying to do something like the 3rd example link, or some part of your code does not do what you think and your code ends up doing this. Well, this is all I can help you with as of now.
Hope you find this useful.