Segmentation fault, how come?

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

  1. Fader_Berg

    Fader_Berg New Member

    Joined:
    Aug 31, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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.

    Code:
    typedef struct match_s {
    
        double home;
        double draw;
        double away;
    
    } match_t; 
     	Code:
     	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

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice