1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Segmentation fault, how come?

Discussion in 'C' started by Fader_Berg, Aug 31, 2010.

  1. Fader_Berg

    Fader_Berg New Member

    Aug 31, 2010
    Likes Received:
    Trophy Points:
    I'm new to C and have stumbled over a problem I can't get a grip of.

    I've written a function that reads floating point numbers from a file and put them in a allocated (malloc) match_t array. It works fine so far. But when writing a unit test for it (yes wrong order, I know), strange things happens.

    typedef struct match_s {
        double home;
        double draw;
        double away;
    } match_t; 
     	void test_readMatchStats() {
        int c;
        match_t *matchArray;
        match_t facit[] = {
            {0.11, 0.12, 0.13},
            {0.21, 0.22, 0.23},
            {0.31, 0.32, 0.33},
            {0.41, 0.42, 0.43},
            {0.51, 0.52, 0.53}
        matchArray = readMatchStats("test.txt");
    When the facit array gets over 4 in legth I get a segmentation fault, otherwise it works. If I comment out the call to readMatchStats, I can make it bigger. Why is that?

Share This Page