Thread: Segmentation fault

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    77

    Segmentation fault

    Hello to all,

    I am having a problem with my program. I am receiving an error message "Segmentation fault" when I run it. Here is my code:

    Code:
    main( int argc, char **argv )
        {
            FILE *input ; 
            FILE *output ;
            char buffer[1000] ;        
            char sequence[100000000] ;
            char c ;
            int i = 0 ;
                    
            if( ! ( input = fopen( argv[1], "r" ) ) )
                { 
                    printf( "COULD NOT OPEN FILE %s - Exit!\n", argv[1]) ; 
                    exit(1) ; 
                }        
    
            while(fgets(buffer, 1000, input))
                            {
                            // start obtaining bases after ORIGIN
                            if(strstr(buffer, "ORIGIN")) 
                                {                                                  
                                   int n = 0 ;
                                   while((c=getchar()) != '/')
                                        {
                                        if(c >= 'a' && c <= 'z')
                                            {
                                            sequence[i++] = c ;
                                            }
                                        }                            
                                }           
                            }
            printf("Here is the sequence:\n" ) ;     
            return(0) ;
        }
    The sequence I am trying to build up is going to be about 5 million characters and the file I am inputing is pretty big; about 12 MB. Any help would be great.

    Thanks

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Youre probably blowing the stack. Try using malloc to assign large areas of memory for use, see if that helps.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    char sequence[100000000] ;
    That's really, really big for an array -- especially an array stored on the stack. As mike_g has indicated, if you use malloc or even declare that array globally it might solve your problem. (That is, put that line outside of main(), right before it.)

    Personally, I suggest you use malloc(). A simple solution would go like this:
    Code:
    long filesize;
    char *data;
    /* open the file input in here somewhere */
    
    fseek(input, 0, SEEK_END);  /* set the file pointer to the end of the file */
    filesize = ftell(input);  /* store the current file position -- i.e., the size of the file in bytes */
    
    /* allocate one byte of memory for every byte in the file */
    data = malloc(filesize * sizeof(*data));
    
    /* do stuff with the data array, reading the file etc */
    
    free(data);  /* don't forget to clean up after yourself . . . . */
    That's not an optimal solution, of course; you don't need enough memory to store every byte in the file, just every alphabetic character. But a better solution would involve realloc() and I don't feel like explaining that here . . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Unfortunately, 100000000 is about 95 MB--too big for the stack! The stack can hold about 1 MB by default, but varies from OS to OS and settings in some linkers/compilers.
    And I really doubt you need all that memory either. You need a dynamic memory approach. Allocate a reasonable size with malloc, keep track of size, if your array exceeds the size, reallocate using realloc and free later.

    And main returns int, so it should be "int main" and not "main".
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 03:36 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM