strange segmentation fault

This is a discussion on strange segmentation fault within the C Programming forums, part of the General Programming Boards category; I'm new in C and would appreciate if anyone can help me out on this one: My program haults with ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    3

    strange segmentation fault

    I'm new in C and would appreciate if anyone can help me out on this one:

    My program haults with a "segmentation fault". I was using malloc() etc so I suppose there was sth wrong with that. But what really puzzles me is that the problem would disappear if I comment out certain lines of code that weren't even executed at all. And when I add some dummy codes back in, the problem reappears, sometimes at a different point. The errors occur either at "free()" or when it was accessing some dynamically allocated memory.

    Can someone tell me why such errors would appear/disappear with addition/deletion of dummy codes that weren't even executed at all? And maybe point to me what may be going wrong?

    thanks a lot.

  2. #2
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    Can someone tell me why such errors would appear/disappear with addition/deletion of dummy codes that weren't even executed at all? And maybe point to me what may be going wrong?
    please post the code

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It happens because the addition of those lines change the executable, and how it's in memory when running. Thus, what may have "worked" once, crashes another time. Meaning:
    Code:
    #include <stdio.h>
    int main( void )
    {
        int array[2];
        array[10] = 300;
        printf("%d", array[10] );
        return 0;
    }
    This may "run fine" one time you run it, but another it may crash. That's what you get when you play with memory you shouldn't be. A segmentation fault means you're accessing memory you're not supposed to be. (Like in the above example where we run past the end of the array.)

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

  4. #4
    01000011 00100000 0010000
    Join Date
    Jul 2004
    Posts
    38
    Yes, Quzah is correct. Basically just run back through your code and make sure you are not accessing an element in an array that is beyond the bounds of the array's declarated length.

  5. #5
    ---
    Join Date
    May 2004
    Posts
    1,379
    i had a problem once where all it was, was a mis use of an operator
    eg;
    Code:
     int array[10];
     for (i=0;i<=10;i++)     // '<=' should be '<'
     {
       ...
     }
    the point is something so stupid took me ages to find without a debugger!

    ps: learn to use a debugger!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange segmentation fault
    By Ron in forum C Programming
    Replies: 24
    Last Post: 06-15-2008, 03:10 PM
  2. strange segmentation fault
    By Lateralus in forum C Programming
    Replies: 2
    Last Post: 06-10-2005, 10:19 AM
  3. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  4. strange segmentation fault error!
    By jayjay in forum Linux Programming
    Replies: 1
    Last Post: 10-20-2003, 04:25 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21