getting Segmentation Fault (core dumped) error but Program runs fine.

i am executing following program

int main()                                                              
{      char str[1][624];                                                       
FILE * fp;                                                              
int i=0;                                                                
int j=0;                                                                
int flag=0;                                                             
char c;
fp=fopen("tmp","r");                                                    
do                                                                        
{                                                                             
c=getc(fp);                                                             
  if (flag==1)                                                                    
  {                                                                        
	if (c==')')                                                                   		
	{                                                                       		
	flag=0;                                                                 
	str [j]='\0';                                                        
	printf("string=%s\n", str[j]);                                          	
	i=0;                                                                    	
	j++;                                                                            
	if (j==2)                                                                                                               
		break;
	else 
		continue;                                                                   
    }                                                                   	
  else                                                                           	
	{                                                                       
  str[j]=c;                                                             	
  i++;                                                                     
  continue;                                                                
	}                                                                 
  }
 else if (c=='(')                                                           
 {                                                                                
flag=1;                                                                  
continue;                                                        
 }                                                                  
}while(c!=EOF);                                                        
fclose(fp);                                                              
free(str);                                                               
free(c);                                                                 
free(i);                                                                 
free(j);                                                                 
free(flag);
return 0;
}

after execution i am able to get desired print but after return 0
i am getting an error as "Segmentation Fault (core dumped)"

please suggest, its urgent.

I would suggest looking up the man page for "free". Free is only called on memory allocated using commands such as "malloc", "calloc" or "realloc". Your use of it in this program is corrupting the process memory and making it coredump.

There are exactly 7 locations in your code, which could get the segmentation faults:

1)
Two locations at the code below:

	str [j]='\0';                                                        
	printf("string=%s\n", str[j]);                                          	

Reason: Your decleration for str

      char str[1][624];

Gives you only 1 column of 624 character locations; should be accessible in the code only at the zeroth row location only, like str[0][i].

Soln: Either make sure that the value of J is not more than 0 and check for

if(1 == J) break;

//instead of the below;

if(j ==2)
          break;

or declare str like the following

char str[3][624];

This way you'd not get seg-11 at either of the 2 locations, described above.

2)
Five locations at the following code segments:


free(str);                                                               
free(c);                                                                 
free(i);                                                                 
free(j);                                                                 
free(flag);

Reason:
You should use free() only on pointers, you explitly declared and defined in your program. That too, only when these pointers are pointing to a valid memory locations on the heap area of your program. Primarily once for every explicit call to malloc(), realloc(), calloc() only, however, sometimes you get a memory allocated through library function calls or some systems call (like stat()) for eg, but here too, such functions require you to pass a validly defined pointers to them.

You should NEVER call free() on any other things. Here you have called free() over a number of datatypes (which contain some numeric values which gets type casted implicitly to pointers to a memory locations which is definitely out of range of your process memory area. Hence getting segmentation faults.

Soln:
Remove the call to free() you do not require them in your program.

Cheers :slight_smile:

8....
You call:

c=getc(fp);

When you dont even know if the:

fp=fopen("tmp","r"); 

Was successful (fp might be NULL)

Absolutely!!!