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?

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.

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

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++;
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.