Segmentation fault !, program works:S

This is a discussion on Segmentation fault !, program works:S within the C Programming forums, part of the General Programming Boards category; Hi, my program compiles but when i run it, i get segmentation fault ? can anybody help > thanks in ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    5

    Exclamation Segmentation fault !, program works:S

    Hi, my program compiles but when i run it, i get segmentation fault ? can anybody help > thanks in advance.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(void)
    {
    
    float winddata[6572][15];
    int undays = 0;
    FILE *wind_data;
    
    wind_data = fopen("wind_data.txt","r");
    
    if(wind_data == NULL){
            printf("Error: can't open file.\n");
            return(1);
    }
    else{
            int i;
            int j;
            while(!feof(wind_data))
            {
            for(i = 0; i <6573; i++){
            for(j = 0; j <15; j++)
            {
            fscanf(wind_data, "%f", &(winddata[i][j]));
            }
            }
    
    
    }
            for(i =0; i<6573; i++){
            if(winddata[i][3] < 5.828){
            undays++;
            }
            }
            fclose(wind_data);
            printf("Unproductive days %d\n", undays);
            return(0);
    
    }

    when i run it, the program displays the Unproductive days, but on a next line i get
    "Segmentation fault.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,256
    you are accessing data outside the buffer. The buffer is declared as 6572, but your for loop goes to 6573. You get a seg fault when you try to access winddata[6572][0]

  3. #3
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,812
    your overstepping the array bounds
    This:
    Code:
    for(i =0; i<6573; i++)
    Should be:
    Code:
    for(i =0; i<6572; i++)
    Remeber arrays start at 0
    Woop?

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    84
    Quote Originally Posted by jspri (I took the liberity of simpifying it a little)
    Code:
    while (!feof(wind_data))
    {
        for (i = 0; i < 6573; i++)
            for(j = 0; j <15; j++)
                fscanf(wind_data, "%f", &(winddata[i][j]));
    }
    This is some baaad code...
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351

  5. #5
    ---
    Join Date
    May 2004
    Posts
    1,379
    Quote Originally Posted by iwabee
    well, if we are going to get picky then
    Code:
    int i;
    int j;
    should be declared at the top with
    Code:
    float winddata[6572][15];
    int undays = 0;
    FILE *wind_data;

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by sand_man
    well, if we are going to get picky then
    Code:
    int i;
    int j;
    should be declared at the top with
    Code:
    float winddata[6572][15];
    int undays = 0;
    FILE *wind_data;
    If we're going to get picky, it could be C99, in which case, it wouldn't matter.

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

  7. #7
    Registered User
    Join Date
    Jun 2004
    Posts
    84
    Quote Originally Posted by sand_man
    well, if we are going to get picky then
    Picky? Are you on drugs or something? If wind_data.txt have even one return at the end of the file, then his code will go whole forfor loop all over again reading who knows what who knows where.

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    5

    all is good.

    thanks guys

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Quote Originally Posted by sand_man
    well, if we are going to get picky then
    Code:
    int i;
    int j;
    should be declared at the top with
    Code:
    float winddata[6572][15];
    int undays = 0;
    FILE *wind_data;
    But they are at the top.... of their own code block.

    >>fscanf(wind_data, "%f", &(winddata[i][j]));
    Always check the return code from these type of functions, to make sure they worked correctly.

    >>if(winddata[i][3] < 5.828)
    A word of warning:
    http://www.cygnus-software.com/paper...ringfloats.htm
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation Fault Problem: Urgent Help
    By bodydrop in forum C Programming
    Replies: 3
    Last Post: 05-05-2006, 09:02 PM
  2. Segmentation Fault??
    By SteveP4444 in forum C Programming
    Replies: 5
    Last Post: 04-01-2006, 02:24 AM
  3. Segmentation fault on my program
    By blackswan in forum C Programming
    Replies: 2
    Last Post: 05-11-2005, 05:47 PM
  4. Segmentation Fault Error
    By jcramer in forum C Programming
    Replies: 2
    Last Post: 11-23-2003, 02:16 PM
  5. debugging: sigsegv, segmentation fault???
    By Gonzo in forum C Programming
    Replies: 9
    Last Post: 09-16-2003, 07:56 AM

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