what is segmentation fault?
Answers were Sorted based on User's Feedback
Answer / kamal
access to the memory which is not valid, will cause
segmentation fault.
Let us take example, in dynamic memory allocation like
malloc, we allocate memory and then we free it using:
free(fp);
if we write free() one more time say ,
free(fp);
free(fp);
it will cause segmentation fault. because the pointer was
already freed and now its not valid, but we are trying to
free an invalid location.
| Is This Answer Correct ? | 74 Yes | 5 No |
Answer / mohan
In C There are different types of error like
1.compile time error
2.Linker error
3.Run time error
4.Logical error
The sigmentation fault comes under Run tile error.
Let see a simple example
#include<stdio.h.
int main()
{
int i;
scanf("%d",i);
printf("%d",i);
return(0);
}
if you compile this program ,compiler can't report any
error.If you run this program in Linux Platform you got
sigmentation fault error.
This is because ,in scanf() function call instead passing
the address of the variable ,value of variable is going to
scanf() .so you got this error.
In Linux sigmentation fault is run time error reported by
memory management.
most of the time this error occurs at illegal memory access.
| Is This Answer Correct ? | 34 Yes | 7 No |
Answer / hitesh
This refers to a program crash when an application tries to
access resources on a computer that it does not have
permission to access. For the sake of the security of the
computer, the operating system will crash the offending
computer and create a segmentation fault
| Is This Answer Correct ? | 30 Yes | 10 No |
Answer / dheeraj gahtori
Segmentation fault occurs when we address to an invalid
memory location. for example if we address a file address
which has already been freed , will give us segmentation fault.
| Is This Answer Correct ? | 25 Yes | 7 No |
Answer / amro
it might be casing of buffer overflow which consumed the
memory buffer and case segmentation fault
| Is This Answer Correct ? | 26 Yes | 12 No |
Answer / sunil kumar sharma
The physical memory in a computer is managed by the
operating system.It has different segments for different
purposes like for storing data,variables,text,garbage
values etc.When we try to access the memory location
outside of any allocated space segmentation fault does
occurs.
Like in Unix environment pointers must be handled carefully.
char *p;
We can't use this pointer variable without initializing it.
If we do so, we will get a segmentation fault error.
| Is This Answer Correct ? | 11 Yes | 4 No |
Answer / vijaya aliveni.m
The physical memory in a computer is managed by the
operating system.It has different segments for different
purposes.
Here is an example of ANSI C code that should create a
segmentation fault on platforms with memory protection:
int main(void)
{
char *s = "hello world";
*s = 'H';
}
In the above ex. string "s" will hold the "hello world".
But in bellow we are trying to replace the letter "H" in the
place of "h".
But in that, pointer is only readonly we are not able to
change the charters.
So , while executing it will show the segmentation fault.
| Is This Answer Correct ? | 8 Yes | 3 No |
Answer / vinaymb
Consider an example
#include "stdio.h"
main ()
{
char *ptr = "BANGLAORE";
*ptr = "b";
}
This Program will compile without any error. But on running
this program you will get the Segmentation error. Because
the ptr will hold the address which is read-only and we are
changing the value at the read-only which will result in
segmentation fault.
| Is This Answer Correct ? | 5 Yes | 1 No |
Answer / siva
segmentation fault occurs when voilating the rules and
regulations of the Operating System.
eg:
1. divide a number by zero
2. when accessing a memory non exist.
| Is This Answer Correct ? | 22 Yes | 19 No |
Answer / jailani
in many situation segmentation faults can occur. the main
situations are
1. when you are trying to access the null pointer
2.access to memory that already freed
3.dereferencing null pointer and uninitialized pointer
4.and also some compiler give segmentation fault when stack
overflow will occur
the concept behind the segmentation fault is that every
program has some space on memory for it execution like
storing variables in stack and heap. when you are try to
access the out of memory that has allocated.
| Is This Answer Correct ? | 3 Yes | 0 No |
what are the different ways the Linux can swich from User Space to Kernel Space & vice-versa ?
How to give the root permision to users?
if one of the critical section code (linked list) is under ISR & another one in kernel thread ? How you will synchronize for this critical section code ?
what is mean by raid and what are all raids available even in software and hardware?
how to create a ftp user on redhat linux 4.0?
how to configure ftp server on redhat linux?
what are Softirq's and how many major numbers be allocated in the linux kernel(2.4) ?
what is stored in /proc ? what is significance of /proc in linux ?
how to create samba server in fedora linux 9 ?
Explain process management system calls in linux?
what is the diff b/w MUTEX and semaphore?
how to configure http server on red hat linux4.0?