简体   繁体   中英

Why am I getting a Segmentation fault when I try to add a new node to my linked list?

Just trying to figure out why this piece of code is returning a segmentation fault. Gdb says the line giving me the fault strcpy(newNode -> data -> ticket_name, name); it says in function strcat() . Any ideas? I'd also like to point out that I'm very new to C.

void add_ticket(tm_type * tm)
{
    char name[TICKET_NAME_LEN+1], type, zone[TICKET_ZONE_LEN], buffer[5], *ptr, *pos;
    int price;
    BOOLEAN check = TRUE;
    printf("Add Ticket\n");
    printf("----------\n");
    printf("\n");
    printf("Ticket name (1-40 characters): ");
    if(fgets(name, sizeof(name), stdin) != NULL){
        if(name[0] != '\n'){
            if((pos=strchr(name, '\n')) != NULL){
                *pos = '\0';
            }

            printf("\nTicket type (1 character): ");
            type = fgetc(stdin);
            if(type != '\n'){

                read_rest_of_line();
                printf("\nTicket zone (1-3 characters): ");
                if(fgets(zone, sizeof(zone), stdin) != NULL){
                    if(zone[0] != '\n'){
                        if((pos=strchr(zone, '\n')) != NULL){
                            *pos = '\0';
                        }

                        printf("\n\n");
                        printf("Price (in cents): ");
                        if(fgets(buffer, sizeof(buffer), stdin) != NULL){
                            price = strtol(buffer, &ptr, 10);
                            if(buffer[0] != '\n'){

                                //create new node
                                stock_node *newNode = (stock_node*)malloc(sizeof(stock_node));

                                if(newNode == NULL){
                                    fprintf(stderr, "Unable to allocate memory for new ticket.\n");
                                    exit(EXIT_FAILURE);
                                }

                                strcpy(newNode -> data -> ticket_name, name);
                                newNode -> data -> ticket_type = type;
                                strcpy(newNode -> data -> ticket_zone, zone);
                                newNode -> data -> ticket_price = price;
                                newNode -> data -> stock_level = DEFAULT_STOCK_LEVEL;
                                newNode -> next_node = NULL;

                                //check for first insertion
                                if(tm -> stock -> head_stock -> next_node == NULL){
                                    tm -> stock -> head_stock -> next_node = newNode;
                                    printf("First ticket added.\n");
                                }

                                else{
                                    //else loop through the list and find the last
                                    //node, insert next to it
                                    stock_node *stream = tm -> stock -> head_stock;
                                    while(check == TRUE){

                                        if(stream -> next_node == NULL){
                                            stream -> next_node = newNode;
                                            printf("Ticket added.\n");
                                            check = FALSE;
                                        }
                                        stream = stream -> next_node;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

You allocated space for newNode , but you never did any sort of initialization. Because of this newNode -> data contains garbage, so when you dereference it with the second arrow, you're you get a seg fault. My c is rusty, but I believe you want

// create new node
stock_node *newNode = (stock_node*)malloc(sizeof(stock_node));

newNode -> data = (data_struct*)malloc(sizeof(data_struct));

...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM