944,183 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 27488
  • C++ RSS
Jan 6th, 2005
0

what is the best way to track segmentation fault errors

Expand Post »
I am using linux.
I ve already tracked down the code, and I know which class instantiation is causing the problem. But I cannot go further than that, since the same code used to work before I did an upgrate of the opensource framework over which I am developing a new class (this one causing failure).

So is there a way to find out more about this problem? (I never used a debugger, I debug codes in the old style, printing messages... )

Regards,
Luiz
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
luizebs is offline Offline
1 posts
since Jan 2005
Jan 6th, 2005
0

Re: what is the best way to track segmentation fault errors

Generally, segmentation faults are because a pointer of yours is either NULL, or points to random memory (probably never initialized to anything), or points to memory that was deleted.

like:
C++ Syntax (Toggle Plain Text)
  1. void Test()
  2. {
  3. char* p1 = NULL; // initialized to null, which is good but not dereferencable on many systems
  4. char* p2; // not initialized at all
  5. char* p3 = new char[20]; // great, it's allocated
  6. delete [] p3; // but now it isn't anymore
  7.  
  8. // now, referencing any of these variables could cause a segmentation fault.
  9. // here's some other possabilities:
  10.  
  11. char* p4 = new char[20];
  12. char c = p4[21]; // reference off the end. may depend on how FAR off the end
  13.  
  14. // this one is more subtle, because we have 20 bytes allocated to a string
  15. // and we use strcpy to copy in a 20 byte string. But, wait! There is a
  16. // null terminator copied too, making it 21 bytes! This may not fail immediately,
  17. // but may fail later when you allocate more memory or delete this memory.
  18. strcpy( p4, "12345678901234567890" );
  19. }
Reputation Points: 36
Solved Threads: 11
Posting Pro in Training
Chainsaw is offline Offline
436 posts
since Jun 2004

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: Which components of Visual C++ should i install
Next Thread in C++ Forum Timeline: Have an error in my program





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


Follow us on Twitter


© 2011 DaniWeb® LLC