944,147 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 3601
  • C RSS
Nov 13th, 2006
0

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

Expand Post »
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
Click image for larger version

Name:	error2.JPG
Views:	76
Size:	45.9 KB
ID:	2537  
Attached Files
File Type: cpp PROG04.CPP (3.9 KB, 49 views)
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
siddhiinfomedia is offline Offline
1 posts
since Nov 2006
Nov 14th, 2006
0

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

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.
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Nov 14th, 2006
0

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

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Calling C function from MATLAB
Next Thread in C Forum Timeline: Compiling Question





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC