954,190 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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

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?

Attachments error2.JPG 45.85KB PROG04.CPP (3.93KB)
siddhiinfomedia
Newbie Poster
1 post since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

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.

WolfPack
Postaholic
Moderator
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
 

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] ?

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
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.

int cstr[20]={0};		// stores c-values from distance string
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:

while(   fgets(ea, 2, in) )
{
   // blabla
}


Where is pointerea 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.

for(i=0;i<dsn;i++)
	{
		printf("%d ",diststr[i]);
		switch(diststr[i])
		{

why did you make this do difficult? you don't need that switch statement

for(i=0;i<dsn;i++)
	{
		printf("%d ",diststr[i]);
                if( diststr[i] < 20)
                      cstr[diststr[i]]++;
                else
                       cstr99++;
        }
Ancient Dragon
Retired & Loving It
Team Colleague
30,046 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,342
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You