Segmentation Fault

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: May 2005
Posts: 86
Reputation: vicky_dev is an unknown quantity at this point 
Solved Threads: 2
vicky_dev's Avatar
vicky_dev vicky_dev is offline Offline
Junior Poster in Training

Segmentation Fault

 
0
  #1
Mar 25th, 2006
Hi all, what exactly is segmentation fault or access violation and exactly when does it occur?

Why does the following program work correctly :
  1.  
  2. #include <stdio.h>
  3. #include <conio.h>
  4.  
  5. int main( )
  6. {
  7. int a[5];
  8. a[20] = 20;
  9. a[-20] = 30;
  10. printf("%d %d", a[20], a[-20] );
  11. getch( );
  12. return 0;
  13. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,829
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 750
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Segmentation Fault

 
0
  #2
Mar 25th, 2006
>what exactly is segmentation fault or access violation and exactly when does it occur?
A segmentation fault is when you access memory outside of your address space and the OS politely asks you not to do that.

>Why does the following program work correctly
Undefined behavior can do anything, including work the way you expect it to. Don't rely on a single implementation to tell you what works and what doesn't. We have a standard document for that.
New members chased away this month: 3
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Segmentation Fault

 
0
  #3
Mar 25th, 2006
> Why does the following program work correctly
There is a big difference between "produces expected output" and "works correctly". Your program is not correct, even though it seems to pass the "works for me" test.

When run with some diagnostic software, I get
  1. $ valgrind ./a.out
  2. ==20047== Memcheck, a memory error detector for x86-linux.
  3. ==20047== Copyright (C) 2002-2005, and GNU GPL'd, by Julian Seward et al.
  4. ==20047== Using valgrind-2.4.0, a program supervision framework for x86-linux.
  5. ==20047== Copyright (C) 2000-2005, and GNU GPL'd, by Julian Seward et al.
  6. ==20047== For more details, rerun with: -v
  7. ==20047==
  8. ==20047== Invalid write of size 4
  9. ==20047== at 0x804838F: main (in ./a.out)
  10. ==20047== Address 0x52BFE7E4 is just below %esp. Possibly a bug in GCC/G++
  11. ==20047== v 2.96 or 3.0.X. To suppress, use: --workaround-gcc296-bugs=yes
  12. 20 30==20047==
  13. ==20047== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 13 from 1)
  14. ==20047== malloc/free: in use at exit: 0 bytes in 0 blocks.
  15. ==20047== malloc/free: 0 allocs, 0 frees, 0 bytes allocated.
  16. ==20047== For counts of detected errors, rerun with: -v
  17. ==20047== No malloc'd blocks -- no leaks are possible.
  18.  

For your example, adding more code will no doubt show up all manner of weird problems, most likely close to a deadline.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC