Like Tree8Likes

Why am I getting a segmentation fault?

This is a discussion on Why am I getting a segmentation fault? within the C Programming forums, part of the General Programming Boards category; A year ago, I thought that but this sites mentions the topic about once every two months. NOTE: I still ...

  1. #16
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,763
    A year ago, I thought that but this sites mentions the topic about once every two months.

    NOTE: I still believe it can be something other that 1 on NON C standard compilers.
    Just to be a C standard compiler is must be 1.

    Can anyone confirm this idea?
    Well, on such a system, char would not be one byte in size. The unit sizeof uses is bytes. That doesn't really mean anything by itself.

    In some ways if you actually have something other than 8-bit bytes, though, you're warring with historical precedent.

  2. #17
    Registered User
    Join Date
    Nov 2012
    Posts
    51
    No matter what I try I keep getting a segmentation fault 11 error. This is very frustrating as I cannot continue with writing this program I have tried multiplying by num_students, putting the mallocs in a loop so that it allocates space everytime, etc. I cannot continue I am very frustrated with programming right now and my TA is inaccessible as it is spring break.

  3. #18
    Registered User
    Join Date
    May 2009
    Posts
    2,779
    Quote Originally Posted by C0__0D View Post
    No matter what I try I keep getting a segmentation fault 11 error. This is very frustrating as I cannot continue with writing this program I have tried multiplying by num_students, putting the mallocs in a loop so that it allocates space everytime, etc. I cannot continue I am very frustrated with programming right now and my TA is inaccessible as it is spring break.
    If you have given up; no reason to post.
    If not, post your current code using either fixed size name C-String (char array) or using a for loop with dynamic memory for name C-string.

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  4. #19
    Registered User
    Join Date
    Nov 2012
    Posts
    51
    I have not given up. I don't give up THAT easily but am frustrated beyond imagination. Okay, I put the name arrays in the loop, here's where I am at:

    Code:
    # include <stdio.h>
    # include <stdlib.h>
    # include <string.h>
    
    
    # define MAX_CHARS_IN_NAME 30
    
    
    typedef struct
    {
        char *first_name;
        char *last_name;
        char month[10];
        int day;
        int year;
    } person;
    
    
    int main()
    {
        // Create a file pointer and open the file
        FILE *ifp = fopen("birthday.txt", "r");
    
    
        // Test to see if the file opens
        if (!ifp)
        {
            printf("The file is unable to open!\n");
            return -1;
        }
    
    
        // Create variables for the number of classes, number of students in the class, birthday month/day/year, number of queried students, queried students
        int i, num_classes, num_students, num_of_queried;
        char queried_student;
    
    
        // Read in the number of classes. PROPERLY READ IN (TESTED)
        fscanf(ifp, "%d", &num_classes);
    
    
        // Read in the number of students in the class. PROPERLY READ IN (TESTED)
        fscanf(ifp, "%d", &num_students);
    
    
        // Allocating space for the students in the classroom
        person *student = malloc(sizeof(person)*num_students);
        
        /*
        // Allocating space for the queried student
        person *queried_student = malloc(sizeof(person));
        queried_student->first_name = malloc(sizeof(char) *MAX_CHARS_IN_NAME);
        queried_student->last_name = malloc(sizeof(char) *MAX_CHARS_IN_NAME);
        */
        
        // Loop through the text file
        for (i=0; i< num_classes; i++)
        {
            int j;
    
    
            for (j=0; j< num_students; j++)
            {
    
    
                student->first_name = malloc(sizeof(char) * MAX_CHARS_IN_NAME);
                student->last_name = malloc(sizeof(char) * MAX_CHARS_IN_NAME);
    
    
                printf("j = %d\n", j);
    
    
                // Scan in the student's first name
                fscanf(ifp, "%s", student[j].first_name);
                printf("student[%d]->first_name = %s\n", j, student->first_name);
                
                // Scan in the student's last name
                fscanf(ifp, "%s", student[j].last_name);
                printf("student[%d]->last_name =  %s\n", j, student->last_name);
    
    
                // Scan in the student's birth month
                fscanf(ifp, "%s", student[j].month);
                printf("student[%d]->month = %s\n", j, student->month);
    
    
                // Scan in the student's birth day
                fscanf(ifp, "%d", &student[j].day);
                printf("student[%d]->day = %d\n", j, student->day);
                
                // Scan in the student's birth year
                fscanf(ifp, "%d", &student[j].year);
                printf("student[%d]->year = %d\n", j, student->year);
    
    
                // Scan in the number of queried students
                //fscanf(ifp, "%d", &num_of_queried);
            
                /* int k;
    
    
                for (k=0; k< num_of_queried; k++)
                {
    
    
                } */
    
    
            }
            
            
        }
    
    
    
    
        // Sort the list in order of birthday, IGNORING THE YEAR!!!
    
    
        // print output
    
    
    
    
        fclose(ifp);
        
        return 0;
    }
    Also tried this:
    Code:
    for (i=0; i< num_classes; i++)
    	{
    		int j;
    
    
    		for (j=0; j< num_students; j++)
    		{
    
    
    			// Allocating space for the students in the classroom
    			person *student = malloc(sizeof(person)*num_students);
    			student->first_name = malloc(sizeof(char) * MAX_CHARS_IN_NAME);
    			student->last_name = malloc(sizeof(char) * MAX_CHARS_IN_NAME);
    
    
    			printf("j = %d\n", j);
    
    
    			// Scan in the student's first name
    			fscanf(ifp, "%s", student[j].first_name);
    			printf("student[%d]->first_name = %s\n", j, student->first_name);
    			
    			// Scan in the student's last name
    			fscanf(ifp, "%s", student[j].last_name);
    			printf("student[%d]->last_name =  %s\n", j, student->last_name);
    
    
    			// Scan in the student's birth month
    			fscanf(ifp, "%s", student[j].month);
    			printf("student[%d]->month = %s\n", j, student->month);
    
    
    			// Scan in the student's birth day
    			fscanf(ifp, "%d", &student[j].day);
    			printf("student[%d]->day = %d\n", j, student->day);
    			
    			// Scan in the student's birth year
    			fscanf(ifp, "%d", &student[j].year);
    			printf("student[%d]->year = %d\n", j, student->year);
    
    
    			// Scan in the number of queried students
    			//fscanf(ifp, "%d", &num_of_queried);
    		
    			/* int k;
    
    
    			for (k=0; k< num_of_queried; k++)
    			{
    
    
    			} */
    
    
    		}
    		
    		
    	}
    If it matters, my text file is:
    Code:
    2
    3
    JOHN SMITH MARCH 7 1968
    PETER THOMPSON MAY 12 1977
    STEVEN WILSON FEBRUARY 18 1959
    2
    JOHN SMITH
    STEVEN WILSON
    4
    DANIEL MARNER MAY 12 1968
    JOHNSON ELLINGTON JUNE 28 1960
    BILLY CLINT AUGUST 10 1950
    PETER FOUTS MARCH 10 1951
    1
    BILLY CLINT
    Last edited by C0__0D; 03-05-2013 at 05:06 PM.

  5. #20
    Registered User
    Join Date
    May 2009
    Posts
    2,779
    Fixed so it does not seg fault for me; still has logic errors.

    The two lines that were obvious wrong to me.

    Code:
                student->first_name = malloc(sizeof(char) * MAX_CHARS_IN_NAME);
                student->last_name = malloc(sizeof(char) * MAX_CHARS_IN_NAME);
    Code:
                student[j].first_name = malloc(sizeof(char) * MAX_CHARS_IN_NAME); /* Tim S Fixed */
                student[j].last_name = malloc(sizeof(char) * MAX_CHARS_IN_NAME); /* Tim S Fixed */
    Tim S.

    Code:
    # include <stdio.h>
    # include <stdlib.h>
    # include <string.h>
    
    
    # define MAX_CHARS_IN_NAME 30
    
    
    typedef struct
    {
        char *first_name;
        char *last_name;
        char month[10];
        int day;
        int year;
    } person;
    
    
    int main()
    {
        // Create a file pointer and open the file
        FILE *ifp = fopen("birthday.txt", "r");
    
    
        // Test to see if the file opens
        if (!ifp)
        {
            printf("The file is unable to open!\n");
            return -1;
        }
    
    
        // Create variables for the number of classes, number of students in the class, birthday month/day/year, number of queried students, queried students
        int i, num_classes, num_students, num_of_queried;
        char queried_student;
    
    
        // Read in the number of classes. PROPERLY READ IN (TESTED)
        fscanf(ifp, "%d", &num_classes);
    
    
        // Read in the number of students in the class. PROPERLY READ IN (TESTED)
        fscanf(ifp, "%d", &num_students);
    
    
        // Allocating space for the students in the classroom
        person *student = malloc(sizeof(person)*num_students);
    
        /*
        // Allocating space for the queried student
        person *queried_student = malloc(sizeof(person));
        queried_student->first_name = malloc(sizeof(char) *MAX_CHARS_IN_NAME);
        queried_student->last_name = malloc(sizeof(char) *MAX_CHARS_IN_NAME);
        */
    
        // Loop through the text file
        for (i=0; i< num_classes; i++)
        {
            int j;
    
    
            for (j=0; j< num_students; j++)
            {
    
    
                student[j].first_name = malloc(sizeof(char) * MAX_CHARS_IN_NAME); /* Tim S Fixed */
                student[j].last_name = malloc(sizeof(char) * MAX_CHARS_IN_NAME); /* Tim S Fixed */
    
    
                printf("j = %d\n", j);
    
    
                // Scan in the student's first name
                fscanf(ifp, "%s", student[j].first_name);
                printf("student[%d]->first_name = %s\n", j, student->first_name);
    
                // Scan in the student's last name
                fscanf(ifp, "%s", student[j].last_name);
                printf("student[%d]->last_name =  %s\n", j, student->last_name);
    
    
                // Scan in the student's birth month
                fscanf(ifp, "%s", student[j].month);
                printf("student[%d]->month = %s\n", j, student->month);
    
    
                // Scan in the student's birth day
                fscanf(ifp, "%d", &student[j].day);
                printf("student[%d]->day = %d\n", j, student->day);
    
                // Scan in the student's birth year
                fscanf(ifp, "%d", &student[j].year);
                printf("student[%d]->year = %d\n", j, student->year);
    
    
                // Scan in the number of queried students
                //fscanf(ifp, "%d", &num_of_queried);
    
                /* int k;
    
    
                for (k=0; k< num_of_queried; k++)
                {
    
    
                } */
    
    
            }
    
    
        }
    
    
    
    
        // Sort the list in order of birthday, IGNORING THE YEAR!!!
    
    
        // print output
    
    
    
    
        fclose(ifp);
    
        return 0;
    }
    Last edited by stahta01; 03-05-2013 at 05:22 PM.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  6. #21
    Registered User
    Join Date
    May 2009
    Posts
    2,779
    I would use my normal fgets and sscanf instead of fscanf as you are doing to get input from file.

    The only example I found on this site of it in the FAQs FAQ > Validate user input - Cprogramming.com

    Note: The project directions might rule this method out.

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  7. #22
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    This
    Code:
    student[j].first_name = malloc(sizeof(char) * MAX_CHARS_IN_NAME);
    student[j].last_name = malloc(sizeof(char) * MAX_CHARS_IN_NAME);
    is worse than this
    Code:
    typedef struct
    {
        char first_name[MAX_CHARS_IN_NAME];
        char last_name[MAX_CHARS_IN_NAME];
        char month[10];
        int day;
        int year;
    } person;
    You are gaining nothing by allocating a maximum space, and therefore wasting efficiency by the extra calls to malloc. I recommend you abandon the allocation for first_name and last_name, stop worrying about efficiency, and just define the struct with fixed length arrays.

    Next is this
    Code:
    // Read in the number of classes. PROPERLY READ IN (TESTED)
        fscanf(ifp, "%d", &num_classes);
    
    
        // Read in the number of students in the class. PROPERLY READ IN (TESTED)
        fscanf(ifp, "%d", &num_students);
    
    
        // Allocating space for the students in the classroom
        person *student = malloc(sizeof(person)*num_students);
    


    My guess is that your file contains X classes each with Y students? If so, then your allocation is only for Y students (num_students).

    So when you do this
    Code:
    for (i=0; i< num_classes; i++)
        {
            int j;
    
    
            for (j=0; j< num_students; j++)
            {
    ...

    On each iteration of the outer loop, you simply overwrite the previous population of num_students structs. So you end up with just the last num_students students. Which, by the way, makes your first_name/last_name allocation all the worse, as you just abandon that allocated memory.

    stahta01 likes this.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault
    By BigAll in forum C++ Programming
    Replies: 1
    Last Post: 12-10-2006, 06:25 AM
  2. Segmentation Fault
    By blaksheep423 in forum C Programming
    Replies: 11
    Last Post: 12-07-2006, 05:28 AM
  3. segmentation fault?
    By kalleanka in forum C Programming
    Replies: 4
    Last Post: 11-20-2006, 03:03 PM
  4. Segmentation Fault
    By ammalik in forum C++ Programming
    Replies: 1
    Last Post: 11-06-2006, 05:06 PM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM

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