Thread: seg fault with any function call

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    2

    seg fault with any function call

    im a bit new to c, so this is hopefully something simple.
    to familiarize myself with the language im writing a program to backup my data (everythings being written in and for linux, if it matters).
    whenever i call either of two functions, one to tar and bzip2 data and one to move a file, i get a seg fault as soon as the first function within is called. im pretty sure its not buffer overflow, which is what i came up with from google, as the problem happens repeatedly in the same place between compiles and its entirely predictable and limited to these two functions. also, ive verified that everything is allocated properly and initialized. because of the simple nature of one of the functions, and the fact that it's happening right at the first function call, i dont thiink its a problem with these two functions but with some other aspect of my program, ive just run out of ideas as to what could happen that would have such specific and limited effects.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Make a small test program that only calls the function in question, or provides the absolute minimum information required to make the function work. If it works fine, it's something else in your program. You might also consider compiling with debug flags enabled so it dumps a core file, which you can use with GDB.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Sounds to me like you have a stack overflow - that will fail when you call a new function, because that will try to write to the lowest address in the stack.

    Of course, it could be 101 other things too.

    --
    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.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You could try using a debugger to find out more about the problem and perhaps post your code.

    You could also try commenting out parts of the code to see whether it makes the program behave correctly. But this may not always work: a few hours ago I "narrowed down" a segfault to a line that looked like x = 1; With that line uncommented everything appeared to work normally. (The real error most probably was completely elsewhere: I had forgotten to override a function in a different class that according to the manual must always be overridden.)
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    2
    im an idiot. it was stack overflow.

    i just read something about that a few minutes before posting this, but it didnt even occur to me to test it.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It is fairly typical what you describe - all is well within one function, then call another and it blows up. I have seen it in kernel code quite a few times. I usually don't get it in user-mode code, but it can happen.

    --
    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.

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. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  3. Help calling function is asm
    By brietje698 in forum C++ Programming
    Replies: 24
    Last Post: 12-06-2007, 04:48 PM
  4. how to get function call stack
    By George2 in forum C Programming
    Replies: 18
    Last Post: 11-11-2006, 07:51 AM
  5. Replies: 5
    Last Post: 11-04-2006, 06:39 PM