Re: Doubt on segmentation fault



deepak wrote:
Hi,

This is my sample program.

main()
{
char *a;
int i;

for (i=0; i<5; i++) {
a[i] = i;
}

return 0;
}

This process fails due to SIGSEGV. Here how kernel is creating
segmentation fault.

I'm thinking like cpu will look into tlb for finding address of 'a'
and since it's absent will look into page-table where page-table finds
that it's not an address belonging to this process, it
generates a segmentation fault.

Please correct if my understanding is wrong.

The address is not "absent", it's just ... sort of ... unspecified: as you have not initialized "a", it will have whatever value was there before. If you're unlucky, it might be a valid data address and your program will happily over-write soem other data. If you're lucky, the address will either be an invalid address (i.e. not withing a virtual address range allocated to your process) or it will be the address of a read-only segment, e.g. a code segment. In that case, you'll get a SIGSEGV.

For the rest: you're right.

--
These are my personal views and not those of Fujitsu Technology Solutions!
Josef Möllers (Pinguinpfleger bei FTS)
If failure had no penalty success would not be a prize (T. Pratchett)
Company Details: http://de.ts.fujitsu.com/imprint.html
.



Relevant Pages

  • Re: when does the system send SIGKLILL
    ... The memory seems to be fine with "free" but I just caught a seg fault ... Program received signal SIGSEGV, Segmentation fault. ... If your SEGV resulted in corrupting the stack, GDB will not be able to ...
    (comp.unix.programmer)
  • Re: gfortran compiler error: pointer arguments
    ... I checked wikipedia for SIGBUS vs. SIGSEGV. ... And SIGBUS is thrown for ... I believe the non-I/O half was protected by segment registers, that's why early Unix called it a segmentation fault. ...
    (comp.lang.fortran)
  • Re: Fly Segfaults?
    ... # Warren Block: ... > % cat flytest ... Program received signal SIGSEGV, Segmentation fault. ...
    (freebsd-questions)
  • Re: Doubt on segmentation fault
    ... This process fails due to SIGSEGV. ... Here how kernel is creating ... generates a segmentation fault. ... The address is not "absent", ...
    (comp.os.linux.development.system)