Home > Software engineering >  Program stops execution halfway through in C
Program stops execution halfway through in C

Time:06-01

This might be a dumb question but basically this program which uses pointer lists but stops execution after the first use of my showliste function which I use to I print out the list and I have no idea why. If I remove the showliste function then it runs the rest of the code just fine however I really have no idea why since I don't modify anything in that function and its only purpose is to print out the elements.

If somebody could help me out that would be very useful. Thank you in advance!

Here is my code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define max 10

struct book{
    char name[50];
    float price;
    struct book *next;
};
typedef struct book BOOK;

BOOK * createlist(BOOK *);
void showliste(BOOK *);
BOOK * deleteElem(BOOK *);

int main()
{
    BOOK *pstart = NULL;
    pstart = createlist(pstart);
    printf("\nHere is the ordered list: \n");
    showliste(pstart); //stops excution after this for some reason
    pstart = deleteElem(pstart);
    printf("\nHere is the list with the element deleted: \n");
    showliste(pstart);
    return 0;
}

BOOK * createlist(BOOK *pdebut)
{
    int i, choice = 0;
    BOOK *pparcour = NULL, *pprecedent = NULL, *pnew = NULL;
    for(i = 0; i < max && choice == 0; i  )
    {
        pnew = (BOOK *)malloc(sizeof(BOOK));
        printf("Enter name: ");
        fflush(stdin);
        gets(pnew->name);
        printf("Enter Price: ");
        scanf("%f", &pnew->price);
        if(pdebut == NULL)
        {
            pdebut = pnew;
        }
        else{
            pparcour = pdebut;
            pprecedent = NULL;
            while(pparcour != NULL && pnew->price > pparcour->price)
            {
                pprecedent = pparcour;
                pparcour = pparcour->next;
            }
            if(pprecedent == NULL)
            {
                pnew->next = pparcour;
                pdebut = pnew;
            }
            else{
                pprecedent->next = pnew;
                pnew->next = pparcour;
            }
        }
        printf("Do you want to continue? \n");
        printf("0 - Yes                1 - NO\n");
        printf("Choice: ");
        scanf("%d", &choice);
    }
    return pdebut;
}

void showliste(BOOK *pdebut)
{
    while(pdebut != NULL)
    {
        printf("Name: %s\n", pdebut->name);
        printf("Price: %.3f\n\n", pdebut->price);
        pdebut = pdebut->next;
    }
}

BOOK * deleteElem(BOOK *pdebut)
{
    char cible[50];
    BOOK *pprecedent = NULL, *pparcour = NULL;
    printf("Enter the name of the book you want to delete: ");
    fflush(stdin);
    gets(cible);
    pparcour = pdebut;
    pprecedent = NULL;
    while(pparcour != NULL && strcmpi(cible, pparcour->name))
    {
        pprecedent = pparcour;
        pparcour = pparcour->next;
    }
    if(pparcour == NULL)
    {
        printf("\nEntered name is not in the list!!!!\n");
    }
    else{
        if(pprecedent == NULL)
        {
            pdebut = pdebut->next;
            free(pparcour);
        }
        else{
            pprecedent->next = pparcour->next;
            free(pparcour);
        }
    }
    return pdebut;
}
  • pdebut is the head of the list.
  • pparcour is a pointer which I use to go through my list without modifying it.
  • pprecedent is basically the element just before pparcour, mainly used to add a new book in the correct position in a ordered list (if the price of the new book is smaller than the price located in pparcour->price)

CodePudding user response:

These lines

pparcour = pdebut;
/* ... */
pparcour = pparcour->next;

setup access to the uninitialized next member of the recently malloc'd structure, which contains an indeterminate pointer value. Attempting to to read a price member via this indeterminate pointer value

while(pparcour != NULL && pnew->price > pparcour->price)

will invoke Undefined Behaviour on subsequent iterations of the loop.

Use calloc, or manually set the newly allocated node's next member to NULL.

for(i = 0; i < max && choice == 0; i  )
{
    pnew = malloc(sizeof *pnew);
    pnew->next = NULL;
    /* ... */

CodePudding user response:

Any chance that one of the pointers in the list points back to the head of the list or an element before it? If so, this creates a pointer loop, and you will never find an element that has a null value.

  •  Tags:  
  • c
  • Related