Segmentation fault?!

This is a discussion on Segmentation fault?! within the C Programming forums, part of the General Programming Boards category; Hi all. I'm a hopelessly stuck C newbie. I'm trying to make a program that will read one line at ...

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    13

    Question Segmentation fault?! Please help!!!

    Hi all. I'm a hopelessly stuck C newbie. I'm trying to make a program that will read one line at a time from a file, and store each line as a char array in a singly linked list. I'm now totally stumped by a segmentation fault. Any help would be greatly appreciated. The code is as follows:

    Code:
    #include <stdio.h>
    
    
    struct listNode {
    	char data[20];
    	struct listNode *nextPtr;
    };
    
    typedef struct listNode ListNode;
    typedef ListNode *ListNodePtr;
    
    
    int main()
    {
    
    	ListNodePtr *startPtr = NULL, newPtr, previousPtr, currentPtr;
    	FILE *fp;
    	fp=fopen("dictionary.dat","r");
    
    	if (fp!=NULL){	
    
    		while(!feof(fp)){	
    
    			newPtr = (ListNode *)malloc(sizeof(ListNode));		
    
    			if (newPtr!=NULL){				
    
    				fscanf(fp,"%s",newPtr->data);				
    
    				newPtr->nextPtr = NULL;		
    
    				printf("%s\n",newPtr->data);
    
    				previousPtr = NULL;
    
    				currentPtr = *startPtr;
    				while((currentPtr!=NULL)){
    					previousPtr = currentPtr;
    					currentPtr=currentPtr->nextPtr;
    				}
    
    				if (previousPtr == NULL){
    					*startPtr = newPtr;
    				}
    				else {
    					previousPtr->nextPtr = newPtr;
    				}
    			}
    
    			else{							
    				printf("\nWarning:  Not enough memory available to complete loading.\n\n");
    				break;
    			}
    
    		}
    	}
    	else{	
    		printf("\n\n\tERROR:  Dictionary file not found. Exiting.\n\n");
    		exit();				
    	}
    	fclose (fp);
            return 0;
    }
    I added the printf in the middle to try to help me figure out when it was going wrong. It displays the first word in the file but none of the others.

    I figure when i've got the singly linked list working, it wont be so hard to update it to a doubly linked list... hopefully.

    Thanks.
    Last edited by turmoil; 05-24-2003 at 04:32 AM.

  2. #2
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    169
    The segmentation fault is on this line

    > currentPtr = *startPtr;

    Memory needs to be allocated for the following pointer pointer or pointer array.

    > ListNodePtr *startPtr

    I'm not sure why a pointer array is being used but allocate like this:

    startPtr = (ListNodePtr *) malloc(sizeof(ListNodePtr *)*10);

    This will allocated an array of 10 ListNodePtr pointers.

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    13
    Hmmm... I don't understand.

    I've tried playing around with the things you mentioned, but i'm not getting anywhere with it.

    I want a single startPtr to point to the first line stored from the file as a listNode, and then pointers on each one to point to the next. I don't understand why i would need an array of ListNodePtr's.

    I'm really quite confused. I've been stuck on this for a couple of days and can't figure any way around it. Please help me out!!

    Thanks.

  4. #4
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    169
    Try something like this
    Code:
    int main()
    {
         ListNodePtr startPtr = NULL, newPtr, previousPtr, currentPtr;
         FILE *fp;
         fp=fopen("dictionary.dat","r");
    
         if (fp!=NULL){     
              while(!feof(fp)){     
                   newPtr = (ListNode *)malloc(sizeof(ListNode));          
    
                   if (newPtr!=NULL){                                       
                        fscanf(fp,"%s",newPtr->data);                    
                        newPtr->nextPtr = NULL; 
        
                        if(startPtr == NULL){
                             startPtr = currentPtr = newPtr;
                        }
                        else{
                             currentPtr->nextPtr = newPtr;
                             previousPtr = currentPtr;
                             currentPtr = currentPtr->nextPtr;
                        }
                   }
    
                   else{                                   
                        printf("\nWarning:  Not enough memory available to complete loading.\n\n");
                        break;
                   }
              }
         }
         else{     
              printf("\n\n\tERROR:  Dictionary file not found. Exiting.\n\n");
              exit(0);                    
         }
         fclose (fp);
    
         //Show contents of list
         currentPtr = startPtr;
         while(currentPtr)
         {
              printf("%s\n",currentPtr->data);
              currentPtr = currentPtr->nextPtr;
         }
    
        return 0;
    }
    Last edited by Scarlet7; 05-24-2003 at 10:40 AM.

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    13
    a/s/l?

    Marry me?

    Seriously, thanks heaps. I looked at that and thought it couldn't possibly work. Guess that's why i was stuck though. I don't understand at all how it works though -
    currentPtr->nextPtr = newPtr;
    previousPtr = currentPtr;
    currentPtr = currentPtr->nextPtr;

    ..actually now i do. That's amazing. So simple. Thanks again Scarlet, you're a legend.

  6. #6
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    169
    Thanks..

    I noticed a slight problem with the code on my last post. I've edited the code.
    Last edited by Scarlet7; 05-24-2003 at 10:42 AM.

  7. #7
    Registered User
    Join Date
    May 2003
    Posts
    13
    One more question please, and a much more basic one too.

    I'm now trying to compare a word to the words in the linked list. I have no problems traversing the list or anything, but i can't figure out what exactly i need to write to get compare the words.

    eg. i have a variable 'word' with say, apple stored in it. As i compare it to currentPtr->data as it traverses, it will pass apple every time without finding/matching it. This is using the line:
    if ( currentPtr->data == word )

    ...I have also tried
    if(strcmp(currentPtr->data, word)==1)
    and if(strncmp(currentPtr->data, word,n)==1)
    but I think they're comparing memory addresses or the amount of memory each word uses as i'm not getting any sensible output.

    So the question is, what should i be writing there? I'm told Java has a .= operator that does it - is there a C equivalent?

    Thanks.

  8. #8
    Registered User
    Join Date
    May 2003
    Posts
    13
    Ok, thanks for reading it, but after much frustration i've managed to solve it myself. I used
    if ( strncmp ( current->data , word , n ) == 0 )

    where n = 20.
    I found if n was small i lost accuracy and a large n value didn't stop it so hey, why not be stupidly accurate.

    I didn't realise that strncmp had to be set to 0, i had previously assumed it would be boolean.

    Cheers anyway.

  9. #9
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Originally posted by turmoil
    Ok, thanks for reading it, but after much frustration i've managed to solve it myself. I used
    if ( strncmp ( current->data , word , n ) == 0 )

    where n = 20.
    Actually, what you probably want is
    if ( strncmp ( current->data , word , strlen(word) ) == 0 )

    This will compare the two words by exactly the length of the second word and makes the if more generic.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 03:36 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 06:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 01:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21