Thread: debugging: sigsegv, segmentation fault???

  1. #1
    Registered User Gonzo's Avatar
    Join Date
    Sep 2003
    Posts
    5

    Exclamation debugging: sigsegv, segmentation fault???

    Hi, I'm relatively new to C programming, but have been working on producing a stats program as a way to experiment and put the things I've learnt into practice.

    The program I have compiles perfectly, and the complier does not detect any errors. However, everytime I run the program it crashes.

    I'm debugging with GDB, but haven't done any debugging before and don't really know what I'm seeing when I run the program through the debugger. Depending of whether I use scanf or gets to get the file name, I get one of the following two error messages a) "Program received signal sigsegv, segmentation fault, 0x780224ea in _size_of_stack_reserve__()" or b) "sigsegv, segmentation fault 0xbff7b983 in ?? ()".

    I have no idea what this means, any help in interpreting it would be great. The source code that is failing is below.

    Code:
    void Create_New_Excavation_File ( void )
    void Create_New_Excavation_File ( void )
    {
         
         /* Constants */
         FILE *fp;
         
    
         /* Variables */
         char *filename;
         char *path = "C:\\path_to_folder";
         char *file_extension = ".txt";
         char *full_file_path;
         
         /* Get the file name */
         puts( "Please enter an appropriate name for the new file you wish to create" );
         scanf( "%s", &filename );
         
         printf("%s", filename );
    
         /*full_file_path = ( strcat ( path, filename, file_extension ) );*/
    
         printf("Creating File.....\n");
         
         fp = fopen ( full_file_path , "a" );
         fprintf ( fp, "\n Test text tester- Line two.");
         fclose ( fp );
    I'm really lost at the moment so any help would be great.

    Also is there a better way for me to get a file name and new file.
    Last edited by Gonzo; 09-14-2003 at 04:50 PM.

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    You haven't allocated memory for filename or full_file_path.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    Segmentation faults mean you did something with memory you don't own.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User Gonzo's Avatar
    Join Date
    Sep 2003
    Posts
    5

    Smile Thanks for the help

    Thanks for the help guys, I've made some changes and the code now works fine.

    While I'm happy that the code works fine, I still feel as if the code isn't really as 'tight' as it could be. I've done quite a bit of work with VB, and to my mind it just seems as if this code is still a bit sloppy and could be more efficient.

    It could just be my uneducated eye, but as a beginning programmer I would appreciate any feed back you could give on the structure of my code, or any things that could be improved, even minor little things.

    Code:
    void Create_New_Excavation_File ( void )
    {
         
         /* Constants */
         FILE *fp;
         
         /* Variables */
         char filename[21];
         char path[36] = "C:\\path to folder ";
         char file_extension[5] = ".txt\0";
         
         /* Get the file name */
         puts( "Please enter an appropriate name for the new file you wish to create.\n(Maximum 20 characters)" );
         scanf ( "%20s", filename );
    
         strcat ( path,  filename );
         
                   
         printf( "\nCreating File %s.....\n", filename );
         
         
         strcat ( path, file_extension );
         
         printf( "%s", path );
         
         fp = fopen ( path , "a" );
         fprintf ( fp, "\n line of text ." );
         fclose ( fp );
    }
    thanks guys

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    A few pointers then:

    >>scanf ( "%20s", filename );
    The FAQ link I posted tries to get people to avoid scanf() for reading strings from the keyboard.

    Your comment:
    /* Constants */
    is not appropriate, as there are no constants.

    >>char file_extension[5] = ".txt\0";
    The \0 is not needed, it's added by the compiler.

    >>fp = fopen ( path , "a" );
    Mode "a" doesn't create a new file, like your function name suggests. Either make it mode "w" or change the function name.

    As for the size of the filename array, you can use the constant FILENAME_MAX like so:
    >>char filename[FILENAME_MAX];

    Another way to achieve a similar result (one of many):
    Code:
    #include <stdio.h>
    #include <string.h>
    
    void Create_New_Excavation_File(void)
    {
      FILE        *fp;
      const char  path[] = "C:\\Directory";
      const char  file_extension[] = ".txt";
      char        fullname[FILENAME_MAX];
      char        input[FILENAME_MAX - sizeof(path) - sizeof(file_extension)];
    
      puts("Please enter an appropriate name for the new file you wish to create.\n"
           "(Maximum 20 characters)");
    
      if (fgets(input, sizeof(input), stdin) != NULL)
      {
        char  *p = strchr(input, '\n');
        if (p) *p = '\0';
        sprintf(fullname, "%s\\%s%s", path, input, file_extension);
        printf("\nCreating File %s.....\n", fullname);
        if ((fp = fopen(fullname, "w")) == NULL)
        {
          perror(fullname);
        }
        else
        {
          fprintf(fp, "\n line of text .");
          fclose(fp);
        }
      }
    }
    
    int main(void)
    {
      Create_New_Excavation_File();
      return(0);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User Gonzo's Avatar
    Join Date
    Sep 2003
    Posts
    5

    Thumbs up

    Cool, thanks for the input. Its just the sort of help I'm looking for. I appreciate it.

  8. #8
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by Hammer

    >>fp = fopen ( path , "a" );
    Mode "a" doesn't create a new file, like your function name suggests. Either make it mode "w" or change the function name.
    According to the standard I have:
    mode:
    a -- append; open or create text file for writing

    And it's alwayse created for me.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  9. #9
    Registered User Gonzo's Avatar
    Join Date
    Sep 2003
    Posts
    5
    Incidentally, while I know that mode 'a' is append, it will actually create a new file if you name a non-existent file. However that may just be a unique trait to the Dev-C++ complier???

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Yes, mode "a" creates a new file is one doesn't already exist. My point is that the function is called Create_New_Excavation_File, which is misleading, as it won't create a new file is one exists. It may be obvious to the person who wrote that function, but other developers using it will simply assume that it does what it says it does.
    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. debugging segmentation fault
    By -EquinoX- in forum C Programming
    Replies: 4
    Last Post: 10-26-2008, 12:14 PM
  2. SIGSEGV, Segmentation fault
    By micmac700 in forum C Programming
    Replies: 3
    Last Post: 12-13-2006, 03:47 PM
  3. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  4. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM
  5. SIGSEGV, segmentation fault
    By StuBarnes4Prez in forum C++ Programming
    Replies: 4
    Last Post: 01-04-2002, 09:26 PM