Thread: Segmentation Fault

  1. #16
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Meaning, the only way to know it was null was via printf. I am sure you can do that with gdb too, it is just 10x more awkward.
    Run the command "backtrace full" after loading a core dump that occurred due to your NULL pointer. I guarantee that you would be able to fix your problem faster than you could with print statements. Even if you don't want to learn various gdb commands, the one command "backtrace full" will often times give you enough information to fix most issues. Just having the line number which caused the seg fault is extremely valuable. How long does it take you to narrow it down to the exact like with print statements? It takes me about 5 seconds with GDB.

    The best thing about GDB is that most of the time you don't even need to run your application in GDB to solve crash issues. Just load the core dump after your application crashed, and take a look at the backtrace.

  2. #17
    Registered User Homer_Simpson's Avatar
    Join Date
    May 2009
    Posts
    22

    Talking

    Quote Originally Posted by matsp View Post
    I have probably spent more time in front of a debugger window than I have spent actually writing code - because when you get to LARGE code projects, often you track down bugs in some part of code that you have NO IDEA what it does, and it may be 15-20 calls from YOUR code to the actual place where it starts going wrong
    Sounds like someone should have been using printf...

  3. #18
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Homer_Simpson View Post
    Sounds like someone should have been using printf...
    Yeah, because hey. Recompiling a twelve million line project because you inserted a printf() is oh so very efficient.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #19
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Homer_Simpson View Post
    Sounds like someone should have been using printf...
    I'm not talking about a few hundred lines of hobby code here. Just one of the smaller modules in the driver is 15K lines of code (about 400KB of code). Another module is 90K lines (about 3MB of code).

    The total size of the driver code is about 24MB, and if we interpolate the above bits, that means somewhere around 400K lines of code. None of which I, or anyone else at my comapny, has written [well, ok so we have a few hundred lines of interface code, but 99%+ of the code is written by people we can't talk to directly].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by MK27
    I also work in more languages than just C, so it is nice not to be dependant on a method which requires more software which may not exist. They all seem to have a "print", tho.
    Well, it is good to know how to use a hand driven screwdriver, but why should the possible lack of an electrically powered screwdriver stop you from using one when it is available and suitable for the job?

    Quote Originally Posted by MK27
    And printf does not fault on a null pointer, whereas some commands do. Meaning, the only way to know it was null was via printf.
    Use assertions to document assumed facts in your code.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 03:36 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM