Thread: seg fault in program

  1. #1
    Registered User
    Join Date
    May 2015
    Posts
    11

    seg fault in program

    I have the following program:
    Code:
    #include "ten.h"
    
    int main(int argc, char **argv)
    {
        off_t offset;
        int counter, bytes;
        char c;
    
        if (argc != 3) {
            fprintf(stderr,"Usage: %s [file1] [file2]\n", argv[0]);
            exit(1);
        }
    
        if ( (openfiles(argv[1], argv[2])) == -1)
            exit(1);
    
        if ( (offset = lseek(files->fdin, offset, SEEK_END)) == -1) {
            perror("lseek in main 1");
            exit(1);
        }
    
        printf("%d\n", (int)offset);
        /*counter holds each printable digit. 
        for (counter = 0; counter < MAX; ++counter) {
        */
    
        return 0;
    }
    
    #include "ten.h"
    
    filedes *files;
    
    int
    openfiles(char *infile, char *outfile)
    {
        if ( (files->fdin = open(infile, O_RDONLY)) == -1){
            perror("open in openfiles 1");
            return -1;
        }
    
        if ( (files->fdout = open(outfile, O_WRONLY)) == -1) {
            perror("open in openfiles 2");
            return -1;
        }
    
        return 1;
    }
    
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    #define MAX 10
    
    int openfiles(char *, char *);
    
    typedef struct {
        int fdin;
        int fdout;
    } filedes;
    
    typedef enum {
        OUT, IN
    } iorout;
    
    extern filedes* files;
    I think there's a problem with the line: fd->fdin but I'm not sure how to fix it.
    Last edited by Brian Tong; 05-04-2015 at 12:48 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should avoid global variables as they make it difficult to reason about and maintain your program, i.e., change files into a local variable, e.g., one that is local to the main function.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2015
    Posts
    11
    I'd still like to know why this program fails.

  4. #4
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Quote Originally Posted by Brian Tong View Post
    I have the following program:
    Code:
    …
        off_t offset;
    …
        if ( (offset = lseek(files->fdin, offset, SEEK_END)) == -1) {
            perror("lseek in main 1");
            exit(1);
        }
    …
    offset is uninitialized and you seek to an bad position.
    Other have classes, we are class

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Brian Tong
    I'd still like to know why this program fails.
    Avoiding the use of global variables can help you come closer to understanding that. Also, pay attention to compiler warnings, e.g., your compiler would probably have warned you that you used offset before setting its value here:
    Code:
    if ( (offset = lseek(files->fdin, offset, SEEK_END)) == -1) {
    Furthermore, it can help if you elaborated on the problem, e.g., post the test input, expected output and actual output (or in this case, the place where the problem was detected).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    May 2015
    Posts
    11
    I ran this in a debugger and it's failing in the opefiles() function. I tried printing the value of files->fdin and it says It can't access the memory

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Brian Tong
    I ran this in a debugger and it's failing in the opefiles() function. I tried printing the value of files->fdin and it says It can't access the memory
    files is a pointer. What does files point to? Again, files should not have been a global variable in the first place.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    May 2015
    Posts
    11
    I'm not sure...I thought to access the fdin in the filedes struct you'd use:
    files->fdin;

  9. #9
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    956
    files is null, which means it doesn't point to a valid object. Where do you want it to point?

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Brian Tong
    I'm not sure...I thought to access the fdin in the filedes struct you'd use:
    files->fdin;
    As far as I can tell, files does not point to a filedes object. Since it is global, it was initialised to be a null pointer, so files->fdin dereferences a null pointer. This is unequivocally a bug, albeit one that is not so easy to spot at a glance because files is a global variable. You should avoid global variables.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    May 2015
    Posts
    11
    I want to use fdin as a member of the filedes struct. Should I allocate the size of an int w/ malloc to do this?

  12. #12
    Registered User
    Join Date
    May 2015
    Posts
    11
    You should avoid global variables.
    I'll keep this in mind but want to know where i went wrong w/ this program. I'll switch the global to a local variable later

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Brian Tong
    I want to use fdin as a member of the filedes struct. Should I allocate the size of an int w/ malloc to do this?
    Why an int? files is a pointer to filedes, therefore you need a filedes object, not an int. For example, perhaps you could declare a filedes variable in main, then declare files in main and initialise it to the address of that filedes variable.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    May 2015
    Posts
    11
    I tried using this in the openfiles function which is a sepearate file:
    filedes *files;
    files = malloc(sizeof(filedes));

    it still won't compile

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Brian Tong
    I tried using this in the openfiles function which is a sepearate file:
    filedes *files;
    files = malloc(sizeof(filedes));

    it still won't compile
    What is the error message? Note that files would likely need to be used in the main function, so declaring it in openfiles is not likely to be the right approach. Rather, you should add a parameter:
    Code:
    int openfiles(filedes *files, char *infile, char *outfile)
    The openfiles function then assumes that files already points to a valid filedes object.

    Have you ever used standard C I/O with FILE, fopen, etc? The idea would be similiar.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault in my program
    By Dr.JacKaL in forum C Programming
    Replies: 4
    Last Post: 10-29-2014, 02:31 PM
  2. segment fault with the program
    By sanddune008 in forum C++ Programming
    Replies: 4
    Last Post: 03-25-2011, 10:23 AM
  3. Seg Fault AFTER the program finishes..
    By CaptainMorgan in forum C++ Programming
    Replies: 1
    Last Post: 12-02-2006, 01:03 PM
  4. Simple program - Can't see the fault
    By Beckyanne in forum C Programming
    Replies: 8
    Last Post: 09-29-2006, 09:43 AM
  5. Segmentation fault on my program
    By blackswan in forum C Programming
    Replies: 2
    Last Post: 05-11-2005, 04:47 PM