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


 
Thread Tools Search this Thread
Top Forums Programming getting Segmentation Fault (core dumped) error but Program runs fine.
# 1  
Old 10-22-2010
getting Segmentation Fault (core dumped) error but Program runs fine.

i am executing following program

Code:
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][i]='\0';                                                        
	printf("string=%s\n", str[j]);                                          	
	i=0;                                                                    	
	j++;                                                                            
	if (j==2)                                                                                                               
		break;
	else 
		continue;                                                                   
    }                                                                   	
  else                                                                           	
	{                                                                       
  str[j][i]=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.
# 2  
Old 10-22-2010
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.
# 3  
Old 10-22-2010
There are exactly 7 locations in your code, which could get the segmentation faults:

1)
Two locations at the code below:

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

Reason: Your decleration for str
Code:
      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 [I]str[0].

Soln: Either make sure that the value of J is not more than 0 and check for
Code:
if(1 == J) break;

//instead of the below;

if(j ==2)
          break;

or declare str like the following
Code:
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:
Code:
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 Smilie

Last edited by Praveen_218; 10-22-2010 at 08:42 AM.. Reason: Typo spelling errors :)
# 4  
Old 10-22-2010
8....
You call:
Code:
c=getc(fp);

When you dont even know if the:
Code:
fp=fopen("tmp","r");

Was successful (fp might be NULL)
# 5  
Old 10-22-2010
Quote:
Originally Posted by citaylor
8....
You call:
Code:
c=getc(fp);

When you dont even know if the:
Code:
fp=fopen("tmp","r");

Was successful (fp might be NULL)
Absolutely!!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Find a string across line break (because of "segmentation fault core dumped")

Hi, thanks to a precedent post, and thanks to the reply of derekludwig of the forum, I have convert my first awk command as : test.txt is : AAAAAGHIJKLAjKMEFJKLjklABCDJkLEFGHIJKL awk -f findstring.awk test.txt > textreturn.txtfindstring.awk is : BEGIN{ SLENGTH = 3 } { ... (3 Replies)
Discussion started by: thewizarde6
3 Replies

2. UNIX and Linux Applications

Tilda Segmentation fault (core dumped)

Can anyone tell me why I keep getting a Segmentation fault when I try to run tilda? $ tilda Segmentation fault (core dumped) It seemed to run after I deleted my tilda directory like this thread said to do. Unfortunately it wouldn't let me set my keybinding with anything I tried. ... (0 Replies)
Discussion started by: cokedude
0 Replies

3. UNIX for Advanced & Expert Users

Segmentation Fault/ core dumped in metadb

When I was trying to mirror in my v880 server after OS up gradation from 8 to 10 metadb -afc 3 /dev/dsk/c1t1d0s7 I got an error metadb: Segmentation Fault Segmentation Fault (core dumped) Then I logged a case to Oracle/sun team they suggest "Please could you try metadb -ac 3... (0 Replies)
Discussion started by: taherahmed
0 Replies

4. Programming

Memory Fault (core dumped) in ttpy program

I´m writing this program in QNX , I`m kinda new to UNIX and programing in general, and when I try to run it it gives me the Memory Fault error. Can anyone help? #include <stdio.h> #include <fcntl.h> void main(void) {int a,ter; char buf; printf("a="); scanf("%d",a); ter=open... (6 Replies)
Discussion started by: GiganteAsesino
6 Replies

5. Shell Programming and Scripting

Segmentation Fault(Core Dump) Error

Hi all, I have a folder with some 28 files. I have a script file that will iteratively take one file at a time from the folder and provide an output for the input file. Till the 7th file, there was no problem but from the 8th file onwards, i got this Segmentation Fault(Core Dump) error. A file... (2 Replies)
Discussion started by: mick_000
2 Replies

6. Red Hat

Segmentation fault (core dumped)

Hi All, I am getting "Segmentation fault (core dumped)" error in the runtime. I am new this please can you tell me why is that i am getting this error and I am not sure of my compilation : gcc -c avc_test.c gcc -c md5.c gcc avc_test.o md5.o -shared -Llibcoreavc_sdk.so -o proj ... (1 Reply)
Discussion started by: fido.genial
1 Replies

7. Solaris

segmentation fault core dumped

i am getting segmentation fault (core dumped) i tried following things but couldn't understand what is wrong with my prog and where the problem is.... i have only adb debugger available on solaris.... so plz help bash-3.00$ pstack core core 'core' of 765: ./mod_generalised_tapinread... (4 Replies)
Discussion started by: junaid.nehvi
4 Replies

8. Solaris

Segmentation Fault (core dumped)

i am getting Segmentation Fault (core dumped) on solaris, but when i run the same program with same input on linux it runs successfully. How can i trace the fault in program on solaris. (6 Replies)
Discussion started by: junaid.nehvi
6 Replies

9. Programming

Segmentation Fault (core dumped)

what r the situations to receive an error msg like the one below Segmentation Fault (core dumped) (2 Replies)
Discussion started by: bankpro
2 Replies

10. UNIX for Dummies Questions & Answers

Segmentation fault (core dumped)

Hello To All! Now anfd then I receive a message on my console: Segmentation fault (core dumped) What does it mean? Or more precisely what are the implications? :confused: (1 Reply)
Discussion started by: Ivo
1 Replies
Login or Register to Ask a Question