Bus Error: 10...Help please!

Hi all,

I am writing a phonebook program to store names and number using a list. Here is the code for the function which allows the user to enter the name and number (where the error occurs).

//THIS FUNCTION ADDS A NEW ENTRY TO THE phonebook_list
void insert(void){
    //variables
    int flag=0, length=0, menu=0, i=0, check=0;
    NODE *p,*q, *temp;

    //USER MUST ENTER FIRST NAME ONLY FOR ADDITION TO PHONEBOOK
    printf("\nPlease insert the <First Name> of the entry you would like to add not separated by a space.\n");
    if((temp=(NODE *)malloc(sizeof(NODE))) == (NODE *)NULL)
    {
        printf("Memory could not be allocated! Returning to menu...\n\n");
        return;
    }
    scanf("%s", temp->name);
    //check if name is too long for the array IF IT IS PROMPTS USER TO RE ENTER SHORTER NAME
    while(flag==0)
    {                                    
        length=strlen(temp->name);
        if(length<50){
            for(i=0;i<length;i++)
                temp->name = toupper(temp->name);
            flag=1;
        }else{                    
            printf("\nName is too long! Please re-enter (less than 50 characters).\n");
            scanf("%s", temp->name);
            length=strlen(temp->name);
        }
    }
    //prompt user for telephone number
    printf("%s",temp->number);
    printf("\nPlease insert the telephone number in the following format XXXXXXXXXX.\n");
    scanf("%s", temp->number);
    flag=0;

    //check if number has the correct number of chars
    while(flag==0){
        length=strlen(temp->number);
        if(length==10){
            flag=1;
        }else{
            printf("\nThis is an invalid number. Please re-enter.\n");
            scanf("%s", temp->number);
            length=strlen(temp->number);
        }
    }
    printf("%s", head->name);
    //IF PHONEBOOK IS EMPTY THEN THE HEAD WILL BE A NULL POINTER AND TEMP NODE WILL BE ADDED
    if(head==NULL)
        {
        if((head=(NODE *)malloc(sizeof(NODE))) == (NODE*)NULL)
        {
            printf("Memory could not be allocated!\nReturning to menu...\n\n");
            return;
        }else{    
            strcpy(head->number,temp->number);
            strcpy(head->name,temp->name);
            head->next=tail;
            free(temp);
            printf("Entry successfully added.\n\n");
            return;
        }
    }else{
        //IF NEW NODE IS NOT THE FIRST ENTRY(NODE) IN PHONEBOOK(LIST) WILL LOOP THROUGH PHONEBOOK TO FIND CORRECT SPOT TO ADD
        p=head;
        printf("%s", p->name);
        while(p->next!=NULL)
        {
            //IF NAME IS ALREADY IN PHONEBOOK ERROR IS GIVEN AND PROGRAM RETURNS USER TO MAIN MENU
            if(strcmp(p->name,temp->name)==0)
            {
                printf("This name already exists in phonebook!\nReturning to menu...\n\n");
                return;
            //OTHERWISE THE WHILE LOOP WILL CONTINUE THROUGH LIST UNTIL IT FIND THE ADDITION THAT IS ALPHABETICALLY HIGHER
            //AND WILL ADD THAT NODE IN ITS CORRECT SPOT
            }else if(strcmp(p->name,temp->name)<0)
            {
                printf("\nTRUE\n");
                temp->next=p;
                temp->prev=p->prev;
                p->prev->next=temp;
                p->prev=temp;
                printf("Entry successfully added.\n\n");
                return;
            }
            p=p->next;
        }
        //IF NO NAME IS ALPHABETICALLY HIGHER, THAN IT SHOULD REACH THE LAST NODE AND WILL BE ADDED TO THE END OF THE PHONEBOOK(LIST)
        if(p->next==NULL)
        {
            if((temp=(NODE *)malloc(sizeof(NODE))) == (NODE *)NULL)
            {
                printf("Memory could not be allocated!\nReturning to menu...\n\n");
                return;
            }
            temp->next=tail;
            temp->prev=q;
            q->next=temp;
            free(temp);
            printf("Entry successfully added.\n\n");
            return;
        }
    }
}

The program allows the user to enter the name once, but the second time around, after entering the number I get the error Bus Error: 10. I've been debugging for a few hours here and I've narrowed it down to something having to do with how the number is stored. I'm also not completely sure I understand the function of free(), because when I attempt to free(temp) and them printf("%s",temp->name) right after free(temp), it still prints whatever name had been stored in name originally.

Help please!

Thanks in advance!
Khaaliq

Where's the code for 'head'? You printf it and it has not been declared or written.

Sorry, this is the rest of the program I have so far, had and tail are global variables declared as NULL.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define NODE struct node

void insert(void);
/*
int delete(void);
int list(void);
*/

NODE *head = NULL;
NODE *tail = NULL;

struct node{
	char name[51];
	char number[11];
	char *next;
	char *prev;
	
};


int main(void){
	//variables
	int menu=0, flag=0;


		//INSTRUCTIONS for user in main program
	printf("Welcome to our phone book.\n\n");

	while(flag==0){
		printf("Press (1) to add a new name and number.\n");
		printf("Press (2) to delete an entry.\n");
		printf("Press (3) to list all entries.\n");
		printf("Press (4) to quit.\n");
		scanf("%d", &menu);	
	

	//switch statement for menu selection -- calls functions to main that perform the functions described in INSTRUCTIONS above
		switch(menu){
	
			case 1:
				insert();
				break;
			
			case 2:
				counter = delete();
				break;
	
			case 3:
				list();
				break;
			
			case 4:
				flag=1;
				break;
			
			default:
				break;
		}
	
	}	
	return 0;
}

This code is still missing variables and functions, and has numerous compiler errors, so is probably not the code you're actually compiling...

I fixed the blatant errors and stripped out the functions you didn't paste, and it runs without crashing, but without those functions I can't thoroughly test it either.

Your input code isn't working either, probably because you're using scanf, which leaves things stuck in the input buffer whenever things go wrong. fgets() to read entire lines, and sscanf() to process the lines, is a good combination.

My suggestion would be to build functions to separate your linked list from your input code.

NODE *create_node(const char *name, const char *num, NODE *prev, NODE *next)
{
        int n;

        NODE *mem=malloc(sizeof(NODE));
        if(mem == NULL) return(NULL);

        strncpy(mem->number, num, sizeof(mem->number));
        mem->number[sizeof(mem->number)-1]='\0';

        strncpy(mem->name, name, sizeof(mem->name));
        mem->name[sizeof(mem->name)-1]='\0';
        for(n=0; mem->name[n] != '\0'; n++)
                mem->name[n]=topper(mem->name[n]);

        mem->next=next;
        mem->prev=prev;
        return(mem);
}

void insert(NODE *node)
{
        node *h=head;

        if(h == NULL) // List is empty, just set it to this node
        {
                head=node;
                return;
        }

        // Loop until the end of the list,
        // but STOP at the last one.  h will not be NULL after this loop.
        while(h -> next != NULL)
        {
                int v=strcmp(node->name, h->name);

                if(v == 0) return; // Reject duplicate
                else if(v < 0)
                {
                        h=h->next;
                        continue;
                }

                // We want to change a <-> b <-> d <-> e into
                // a <-> b <-> c <-> d <-> e
                // We are at node d.

                // point ahead to d
                node->next=h;
                // point back to b
                node->prev=h->prev;

                // make b point ahead to us
                if(h->prev != NULL) h->prev->next=node;
                // make d point back to us
                h->prev=node;

                return;
        }

        if(h->next == NULL) // append to list
        {
                h->next=node;
                node->prev=h;
                return;
        }
}

gdb or dbx 'where' is a stack trace, which for your core file and unstripped, unoptimized code will gladly tell you where it crashed, if it did not say at crash time.

This sort of thing is why they invented OO, containers, PERL, C++ and JAVA !