Thread: Problem with a function ("Segmentation fault")

  1. #1
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218

    Problem with a function ("Segmentation fault")

    whenever I use this function:

    Code:
    bool register_script(FILE *file, char *argv)
    {
        if( (file = fopen(argv,"r+")) == NULL)
        {
            return false;
        }
        else
        {
            return true;
        }
        return true;
    }
    I get the error (when I run the program) "Segmentation fault". Anyone know what causes this?

    I'm using GCC 3.3 (Linux).

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Try using gdb and locating where the fault occurs.
    Since this is a C forum I take it you are enum your own bool?
    For the following code I recieved a seg fault on the attempt of fclose(). Is this a similar makeup to your program?
    Code:
    #include <stdio.h>
    
    typedef enum
    {
      false = 0,
      true = 1
    }bool;
    
    bool register_script(FILE *file, char *argv)
    {
          if( (file = fopen(argv,"r+")) == NULL)
                {
                          return false;
                              }
              else
                    {
                              return true;
                                  }
                  return true;
    }
    
    int main (void)
    {
      FILE *fptr;
      char argv[]="Test.txt";
      bool retval;
      retval=register_script(fptr, argv);
    
      printf("%d\n", retval);
    
      if (retval)
        fclose(fptr);
    
      return 0;
    
    }
    Last edited by Thantos; 02-20-2004 at 11:52 PM.

  3. #3
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218

    my code...

    Code:
    #include <stdio.h>
    #include "ms.h"
    
    bool register_script(Script *script, char *argv[])
    {
    	if( (script->file = fopen(argv[1],"r+")) == NULL)
    	{
    		return false;
    	}
    	return true;
    }
    
    int main(int argc, char *argv[])
    {
    	Script *script;
    	
    	if(argc == 1)
    	{
    		return 0;
    	}
    	
    	if(!register_script(script,argv))
    	{
    		printf("ERROR: Could not register script (wrong filename?)");
    		return 0;
    	}
    	return 0;
    }
    and, my bool is:
    Code:
    typedef unsigned char bool;
    #define true 1
    #define false 0
    [ms.h has the structures, prototypes, typedefs, etc.]

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Ok I figured it out.

    This is what is happening. When you pass a pointer to a function it makes a copy of that pointer. So when you call fopen() it modifies the copy to point somewhere else but it never changes the pointer in main(). The change is really simple:
    Code:
    #include <stdio.h>
    
    typedef enum
    {
      false = 0,
      true = 1
    }bool;
    
    bool register_script(FILE **file, char *argv)
    {
      FILE *fptr;
    
          if( (fptr = fopen(argv,"r+")) == NULL)
          {
            return false;
          }
          else
          {
            (*file) = fptr;
            return true;
          }
    }
    
    int main (void)
    {
      FILE *fptr=NULL;
      char argv[]="Test.txt";
      bool retval;
      retval=register_script(&fptr, argv);
    
      if (retval)
        fclose(fptr);
    
      return 0;
    
    }
    Basically I'm passing a pointer to the memory loc that holds the pointer to the file. Then in the function I'm dereferencing it and modifing the actual file pointer.

    If you don't want to use the local variable you can just do the deferencing in the same line as the fopen
    Code:
    if( ((*file) = fopen(argv,"r+")) == NULL)
    Is that clear as mud yet?

  5. #5
    Registered User
    Join Date
    Feb 2004
    Posts
    46
    So when you call fopen() it modifies the copy to point somewhere else but it never changes the pointer in main().
    Correct on all points. However, this isn't the problem. The code that kinghajj posted passes a pointer to a structure and calls fopen on a member of that structure. The changes will transfer back to the calling function. The real problem is a typical wild pointer access.
    Code:
    #include <stdio.h>
    #include "ms.h"
    
    bool register_script(Script *script, char *argv[])
    {
             /* script points to nothing predictable, dereferencing will likely segfault */
    	if( (script->file = fopen(argv[1],"r+")) == NULL)
    	{
    		return false;
    	}
    	return true;
    }
    
    int main(int argc, char *argv[])
    {
    	Script *script; /* Declare a pointer -- OK */
    	
    	if(argc == 1)
    	{
    		return 0;
    	}
    	
    	if(!register_script(script,argv)) /* Use the wild pointer -- not OK */
    	{
    		printf("ERROR: Could not register script (wrong filename?)");
    		return 0;
    	}
    	return 0;
    }
    The solution is to either allocate memory to script in main, or use a stack variable instead.
    Code:
    #include <stdio.h>
    #include "ms.h"
    
    bool register_script(Script *script, char *argv[])
    {
    	if( (script->file = fopen(argv[1],"r+")) == NULL)
    	{
    		return false;
    	}
    	return true;
    }
    
    int main(int argc, char *argv[])
    {
    	Script *script;
    	
    	if(argc == 1)
    	{
    		return 0;
    	}
    	script = malloc(sizeof *script);
             if(script == NULL)
             {
                      /* Handle allocation error */
             }
    	if(!register_script(script,argv))
    	{
    		printf("ERROR: Could not register script (wrong filename?)");
                      free(script);
    		return 0;
    	}
             free(script);
    	return 0;
    }

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Heh look at the time stamps He posted his while I was typing mine up
    The problem with the orginal code was exactly as I described.

  7. #7
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218

    damn...

    (lol) OK, that makes sense now. Thanks for all of your help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Problem with function pointers
    By vNvNation in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2004, 06:49 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM