what is the best way to track segmentation fault errors

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2005
Posts: 1
Reputation: luizebs is an unknown quantity at this point 
Solved Threads: 0
luizebs luizebs is offline Offline
Newbie Poster

what is the best way to track segmentation fault errors

 
0
  #1
Jan 6th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 436
Reputation: Chainsaw is an unknown quantity at this point 
Solved Threads: 11
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

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

 
0
  #2
Jan 6th, 2005
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:
  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. }
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



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC