944,153 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 2309
  • C RSS
Mar 25th, 2006
0

Segmentation Fault

Expand Post »
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. }
Similar Threads
Reputation Points: 28
Solved Threads: 2
Junior Poster in Training
vicky_dev is offline Offline
86 posts
since May 2005
Mar 25th, 2006
0

Re: Segmentation Fault

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 25th, 2006
0

Re: Segmentation Fault

> 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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Reducing if statements..again
Next Thread in C Forum Timeline: Link list Query





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC