[prog] C++ - linked list and segmentation fault

Kathryn Hogg kjh at flyballdogs.com
Tue Apr 8 12:26:13 EST 2003


I won't give you all the answers but one thing to look at is:


> int main(){
>     INVENTORY_ITEM* pStart;
>     createFile     (pStart);

Here you create a pointer pStart and pass it into createFile() but pStart
has not been set to anything so it's contents are undefined.

> void createFile(INVENTORY_ITEM *pStart){\
  <snip>


>     while (!in.eof()){
>         in >> temp.quantity;
>         in >> temp.price;
>         current=new INVENTORY_ITEM;
>         *current=temp;
>         current->nextItem=NULL;
>         p=pStart;
>         while ((p!=NULL)&&(strcmp(current->itemCode,p->itemCode)<0))
>                 p=p->nextItem;

Uh oh, here you are deferencing the pointer that was passed but it's
contents are undefined, so this is a likely place to core dump;



-- 
Kathryn


More information about the Programming mailing list