Fscanf segmentation fault issue (I think)

This is a discussion on Fscanf segmentation fault issue (I think) within the C Programming forums, part of the General Programming Boards category; I have my little program that has to read a number of words from different files, insert them into some ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    3

    Fscanf segmentation fault issue (I think)

    I have my little program that has to read a number of words from different files, insert them into some skip-lists and do some other stuff afterwards.

    I was given 3 tests to check the program's functionality.

    For the first two tests, it works splendidly. However, the last one has a rough estimation of a few hundred thousand words in it and the fscanf() function that I use to read the files with makes the program crash after reading about 50.000 words.

    Here's where the problem should be located (If the problem IS with the fscanf() function).

    Note: English isn't my native language, so some of the variable names may seem strange to you

    Code:
    while(fscanf(definitii,"%s",aux) && !feof(definitii))
        {
    			if(aux[strlen(aux)-1]==':')
    			{
    				if(n>0)
    					nr_elem[n-1]=nr_cuv;
    				L=realloc(L,(n+1)*sizeof(List*)); 
    				L[n]=NULL;
    				tabel_def=(char**)realloc(tabel_def,(n+1)*sizeof(char*));
          	                        tabel_def[n]=(char*)malloc(strlen(aux));
    				for(j=0;j<strlen(aux);j++)
    					aux[j]=tolower(aux[j]);
    				aux[strlen(aux)-1]='\0';
         	 	                strncpy(tabel_def[n],aux,strlen(aux)+1);
    				nr_cuv=0;
    				n++;
    				
    			}
    			else
    			{
    				
    				if(strlen(aux)>=3 && isValidString(aux))
    				{
    					nr_cuv++;
    				  for(j=0;j<strlen(aux);j++)
                                      aux[j]=tolower(aux[j]);
    	  			  insert(&(L[n-1]),strtok(aux,"(),.;- "));
    				}
    	                }	
    			free(aux);
    			aux=malloc(100);
        }
    Any help or constructive criticism is greatly appreciated.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I would bet good money that fscanf isn't the cause of your problem. You are most likely trying to scan into a block of memory you shouldn't be scanning into. You probably just ran off the end of your array. The chances of you finding an obscure bug in fscanf is as close to zero as you can imagine.
    L=realloc(L,(n+1)*sizeof(List*));
    You should never use realloc this way. You should assign the return value to a temporary pointer, because if realloc fails here, you've just lost everything you had.
    Code:
    T = realloc( L, (n+1) * sizeof *L );
    if( T )
        L = T;
    You should also just control your loop with the return value of fscanf instead of bothering with feof. There's a FAQ on that if you care to read it.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    3
    First of all, thank you for your quick response. I really appreciate your advise and yeah... I didn't know about that FAQ. However, I'm still having the exact same problem.

    I forgot to mention that I also ran the program through valgrind and this is the ouput when it reaches said Segmentation fault:

    Code:
    ==3601== Invalid read of size 2                                              
    ==3601==    at 0x408AADD: __isoc99_fscanf (isoc99_fscanf.c:31)               
    ==3601==    by 0x804964E: main (in /home/student/SD/Tema 2/take 2/a.out)     
    ==3601==  Address 0x368 is not stack'd, malloc'd or (recently) free'd        
    ==3601==                                                                     
    ==3601==                                                                     
    ==3601== Process terminating with default action of signal 11 (SIGSEGV)      
    ==3601==  Access not within mapped region at address 0x368                   
    ==3601==    at 0x408AADD: __isoc99_fscanf (isoc99_fscanf.c:31)
    ==3601==    by 0x804964E: main (in /home/student/SD/Tema 2/take 2/a.out)
    ==3601==  If you believe this happened as a result of a stack
    ==3601==  overflow in your program's main thread (unlikely but
    ==3601==  possible), you can try to increase the size of the
    ==3601==  main thread stack using the --main-stacksize= flag.
    ==3601==  The main thread stack size used in this run was 8388608.
    Hope it helps...

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by zouru View Post
    First of all, thank you for your quick response. I really appreciate your advise and yeah... I didn't know about that FAQ. However, I'm still having the exact same problem.

    I forgot to mention that I also ran the program through valgrind and this is the ouput when it reaches said Segmentation fault:

    Code:
    ==3601== Invalid read of size 2                                              
    ==3601==    at 0x408AADD: __isoc99_fscanf (isoc99_fscanf.c:31)               
    ==3601==    by 0x804964E: main (in /home/student/SD/Tema 2/take 2/a.out)     
    ==3601==  Address 0x368 is not stack'd, malloc'd or (recently) free'd        
    ==3601==                                                                     
    ==3601==                                                                     
    ==3601== Process terminating with default action of signal 11 (SIGSEGV)      
    ==3601==  Access not within mapped region at address 0x368                   
    ==3601==    at 0x408AADD: __isoc99_fscanf (isoc99_fscanf.c:31)
    ==3601==    by 0x804964E: main (in /home/student/SD/Tema 2/take 2/a.out)
    ==3601==  If you believe this happened as a result of a stack
    ==3601==  overflow in your program's main thread (unlikely but
    ==3601==  possible), you can try to increase the size of the
    ==3601==  main thread stack using the --main-stacksize= flag.
    ==3601==  The main thread stack size used in this run was 8388608.
    Hope it helps...
    This is telling you that your aux variable is not properly initialized before it enters the loop.

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    3
    Well thing is it enters that loop over 50.000 times and then it gives me the error. Also to note that I free my aux variable and allocate another block of memory at the end of each loop... Seemed to work well for tests that contained fewer words.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by zouru View Post
    Also to note that I free my aux variable and allocate another block of memory at the end of each loop...
    Why? I mean really, why is it dynamic at all? You are allocating the same amount of space each time through, so why are you freeing it at all? Why aren't you just using a static buffer of 100 characters? Just memset it if you need to clear it out. Oh, and you never actually check to see if malloc fails or not. So that's probably what's happening. After ther 50,000th time you free 100 bytes and try to get some more, something goes wrong. You never actually check to see if anything goes wrong, so you're oddly surprised when it does.

    Again I have to ask, why aren't you just using a static array? More importantly, why don't you actually check the return values of all the functions you are using here?


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault issue
    By Stenland in forum C Programming
    Replies: 12
    Last Post: 09-19-2009, 04:56 AM
  2. fscanf causes a SEGMENTATION FAULT
    By yougene in forum C Programming
    Replies: 15
    Last Post: 12-28-2008, 11:11 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 02:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 12:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM

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