Segmentation Fault when run in Linux but perfect compilation and execution in Windows

Reply

Join Date: Nov 2006
Posts: 1
Reputation: siddhiinfomedia is an unknown quantity at this point 
Solved Threads: 0
siddhiinfomedia siddhiinfomedia is offline Offline
Newbie Poster

Segmentation Fault when run in Linux but perfect compilation and execution in Windows

 
0
  #1
Nov 13th, 2006
Hello Friends...

I've written a code for Simulating Paging algorithm using LRU...
The code is in the attached file.

Now on windows... it is executing perfectly, giving the output i desire...
but on Linux REGAL gnu compiler, it is giving me the error...
Error file is attached... as error2.jpg

SEGMENTATION FAULT

Can someone help me with this?
How to solve this problem?
Attached Thumbnails
error2.JPG  
Attached Files
File Type: cpp PROG04.CPP (3.9 KB, 13 views)
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Segmentation Fault when run in Linux but perfect compilation and execution in Windows

 
0
  #2
Nov 14th, 2006
Comment out section by section and locate which part gives that error. After that post it here. Since we don't have the input files, we can't reproduce the error here. The file is also too long for us to wade through all the code.
バルサミコ酢やっぱいらへんで
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,378
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Segmentation Fault when run in Linux but perfect compilation and execution in Windows

 
0
  #3
Nov 14th, 2006
compile your program for debug then when the core file is created use debugger on it to see where the problem is.

Suggestion: your program makes no input validations. How does it know that argv[1] contains an integer between 0 and 20? what happens if I enter 500 as argv[1] ?

  1. int cstr[20]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; // stores c-values from distance string
  2. int fstr[20]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; // stores f-values from c-str giving probability of page faults
you can simplify the above like below. It is not necessary to specify the initialization of each element of the array because the compiler will set all unspecified initializer to 0 for you.
  1. int cstr[20]={0}; // stores c-values from distance string
  2. int fstr[20]={0}; // stores f-values from c-str giving probability of page faults

>> while (!feof(in))
we have said many many times on these boards that you should not rely on feof() because it does not always catch eof-of-file at the right time. Instead you should code it like this:
  1. while( fgets(ea, 2, in) )
  2. {
  3. // blabla
  4. }

Where is pointer ea allocated? I see where it is declared and left unitialized, but I don't see where you allocated any memory for it. That may explain the seg fault you are getting.

  1. for(i=0;i<dsn;i++)
  2. {
  3. printf("%d ",diststr[i]);
  4. switch(diststr[i])
  5. {
why did you make this do difficult? you don't need that switch statement
  1. for(i=0;i<dsn;i++)
  2. {
  3. printf("%d ",diststr[i]);
  4. if( diststr[i] < 20)
  5. cstr[diststr[i]]++;
  6. else
  7. cstr99++;
  8. }
Last edited by Ancient Dragon; Nov 14th, 2006 at 8:58 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC