Thread: Seg Fault at Start of Function

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    11

    Seg Fault at Start of Function

    Hi All,
    The following abstract of code compiles with no warnings using gcc (C90). 'printf' reveals that it refuses to enter the function without a seg fault being produced. I can't get my head around it. Surely it must enter the function?!

    Any hints would be appreciated.

    Code:
    List * makelist( char* *wrd, List* *lst ) {         /*seg fault here*/
       List* dest = NULL ;
       List* temp_lst = NULL ;
       
       /*malloc for lst, plus other stuff here*/
    
    return *lst
    }
    
    int main() {
       char* wrd ;	
       List* lst = NULL ;	
    
       /*wrd becomes a pointer to a string. wrd can be NULL.*/
    
       lst = makelist( &wrd, &lst ) ;
    
    return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    It's probably in the /*other stuff here*/ part, probably when you try to follow a NULL pointer, probably when you pretend that wrd or lst can be used.

  3. #3
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    Tabstop is often right

    Getting /* Seg fault here */ does not mean that the actual error ocurred in that spot. It means that OS detected it there. You may have corrupted some places of the memory which were used at that spot. It's not at all rare to get segfaults in places that do not seem to have anything in common with the actual error.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Post your actual code. And if you can, perhaps try to run your program in GDB. Once it segfaults, use "print" to look at the values of variables to try and see what went wrong . . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    Oh, and printf is not really reliable way of seeing where segfault occurs. Printf does not immediately display prints since it is buffering text due to performance issues. It becomes a bit more reliable when you fflush(stdout) after prints.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    That may as well have just been:
    Code:
    /*somestuff here*/
    
    int main()
    {
        /*other stuff here*/
    }
    The thing about being unable to solve the problem yourself is that the problem is inevitably not what or where you are looking at. If it were then you would probably have solved it already.
    You need to post a "minimal complete example" that compiles and that you have confirmed definitely produces the problem, before posting it.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  3. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  4. seg fault with simple function template
    By waxydock in forum C++ Programming
    Replies: 3
    Last Post: 06-29-2005, 05:48 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM