Thread: Runtime error! Please help

  1. #1
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584

    Runtime error! Please help

    I wrote this program (it is unfinished, due to the runtime error). I compile fine, but then I get a runtime error that causes the program to end and crash.

    Please look at the code and, if you have time, compile and look for the problem. I believe that I comment very well, so it shouldn't be bad to read. The .c file is the attachment. Thanks.

    --Garfield
    1978 Silver Anniversary Corvette

  2. #2
    Unregistered
    Guest
    Ack!?!? GOTO!?!?! I stopped looking right then and there.

    Quzah.

  3. #3
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Oh, I'm sorry about the goto. I'll find a way around it later. But for now, can you or anybody find my problem with this runtime error? Please help.

    --Garfield
    1978 Silver Anniversary Corvette

  4. #4
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    I'm not sure if the rest of your program is correct but you're getting a runtime error when searching because you're not initialising the the next pointer to NULL when you create your initial student node, so in schstud() walk_stud is never equal to NULL and you are attempting to access an unintialised pointer.

    Evertime, you create a node you should initialise it's next pointer to NULL, so it can be used as a test for the end of the list.
    zen

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    13
    It runs fine on my compiler,,complete the whole program then send to me ,,I'll get a better idea on why it crashes.
    Does not give out any errors with me??

  6. #6
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    So, zen, what I have to do in main is correct it from:
    Code:
    int main()
    {
    	int test;
    	char strline[10];
    
    	/* first head struct of student */
    	struct student *init_stud, *temp;
    	init_stud = malloc(sizeof(struct student));
    /* etc..... */
    to this:
    Code:
    int main()
    {
    	int test;
    	char strline[10];
    
    	/* first head struct of student */
    	struct student *init_stud, *temp;
    	init_stud = malloc(sizeof(struct student));
            init_stud = NULL; /* Must make NULL, right? */
    /* etc... */
    And then in the function where I add the new list to linked list, I'll have to change it from:
    Code:
    void addstud(struct student *head_stud, struct student *insert_stud)
    {
    	struct student *temp_stud;	/* temporary stud to walk with */
    	temp_stud = malloc(sizeof(struct student));
    	/* loop to get to the last list */
    	while (1)
    	{
    		if (temp_stud == NULL)
    			break;
    		
    		else
    			temp_stud = temp_stud->next_student;
    	}
    
    	temp_stud->next_student = insert_stud;
    }
    to this:
    Code:
    void addstud(struct student *head_stud, struct student *insert_stud)
    {
            insert_stud->next_stud = NULL; /* next instance NULL */
    	struct student *temp_stud;	/* temporary stud to walk with */
    	temp_stud = malloc(sizeof(struct student));
    	/* loop to get to the last list */
    	while (1)
    	{
    		if (temp_stud == NULL)
    			break;
    		
    		else
    			temp_stud = temp_stud->next_student;
    	}
    
    	temp_stud->next_student = insert_stud;
    }
    Right? So then when I add another student, the next pointer (*next_stud) will equal NULL until another linked list is added on. Is this correct? Are these changes valid? Thanks so much. I really owe you one, zen.

    --Garfield
    1978 Silver Anniversary Corvette

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You've got a basic problem initialising the linked list, since you are forcing yourself to have a dummy node at the front of the list (this is the malloc in main).
    The reason for this difficulty is that addstud doesn't return a new head pointer (in the case when the list is currently empty).

    This is my version.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    /* struct grades -- the list of grades per student */
    struct grades {
        int grade;  /* the actual grade for this struct */
        struct grades *next_grade;
    };
    
    /* struct student -- the basis for the student data */
    struct student {
        char name[50];  /* holds the name of the student */
        struct grades *grade_list;  /* the student's grade list */
        struct student *next_student;   /* pointer to next student */
    };
    
    struct student *addstud ( struct student *head_stud, struct student *insert_stud )
    {
        struct student *new_node;
        new_node = malloc(sizeof(struct student));
    
        /* copy data to the new node */
        strcpy( new_node->name, insert_stud->name );
        new_node->grade_list = NULL;
        new_node->next_student = NULL;
    
        /* Add new node to the appropriate point in the list */
        if ( head_stud == NULL ) {
            /* empty list - the new node is the whole list */
            return new_node;
        } else {
            /* Find the last node in the list */
            struct student *end = head_stud;
            while ( end->next_student != NULL ) {
                end = end->next_student;
            }
    
            /* make the end node point to the new node */
            /* it will be the end node next time around */
            end->next_student = new_node;
    
            /* no change to the head of the list */
            return head_stud;
        }
    }
    
    int main ( ) {
        struct student *init_stud = NULL;   /* an empty list */
        struct student data;                /* values from somewhere */
        init_stud = addstud( init_stud, &data );
        return 0;
    }
    The key part is this
    &nbsp; init_stud = addstud( init_stud, &data );

    1. By returning the current list head each time, you allow addstud to return a proper first node as the new head of the list in the case when the list is initially empty.

    2. data is a normal variable - you don't malloc new nodes in main, its all done in addstud, where a new node is allocated, and the data copied to it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Thanks! I understand. Thanks again.

    --Garfield
    1978 Silver Anniversary Corvette

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Runtime formation and execution at runtime
    By Soham in forum C Programming
    Replies: 17
    Last Post: 08-27-2008, 08:45 AM
  2. link with C runtime library
    By George2 in forum C++ Programming
    Replies: 26
    Last Post: 02-05-2008, 01:56 AM
  3. Visual Studio and .Net Runtime Framework dependency
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 08-08-2007, 07:52 AM
  4. Change a control class during runtime
    By Joelito in forum Windows Programming
    Replies: 3
    Last Post: 01-12-2006, 02:13 PM
  5. FILES in WinAPI
    By Garfield in forum Windows Programming
    Replies: 46
    Last Post: 10-02-2003, 06:51 PM