Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

segmentaion fault

Status
Not open for further replies.

prgm

Programmer
Apr 28, 2005
6
US
HELLO

I AM TRYING TO RUN THIS PGM. I WAS ABLE TO COMPLILE IT BUT WHEN I GIVE THE SAME PAGE NO. TO B REPLACED AGAIN IT SAYS


#include<stdio.h>
#include<string.h>
#include<malloc.h>
#include<stdlib.h>

/*I this program I am using Queue as Data Structure with front and rear Pointer LILO (Last In L\
ast Out)*/

struct page
{
char nm[20];//page contains
int pageno; //page Id
int count; //no of times page access in the RAM
struct page *next;
};

struct page *front=NULL,*rear=NULL;
static int pagefaultcounter=0; //initializing the pagefaultcounter

struct hdd
{
char nm[20];
int pageno;
};

void initialize_RAM() //Initializing the RAM
{
struct page *newnode=(struct page *)malloc(sizeof(struct page)); //Creating Space In the Heap
strcpy(newnode->nm,"NULL");
newnode->pageno=0;
newnode->count=0;
newnode->next=NULL;
if(front==NULL)
{
front=rear=newnode;
}
else
rear->next=newnode;
rear=newnode;
}
void insert_in_RAM(struct hdd* pointer) //Insert in the ram
{
int i,page,j,k=2,l=1;
struct page *temp=front;
struct page *temp2;
printf("\nEnter page no between 1to10: ");
scanf("%d",&page); //asking the page number from the user

//finding the page in the RAM
for(temp=front;temp!=NULL;temp=temp->next)
{

//If page already in the RAM
if(page==temp->pageno)
{

//swaping the user page in temporary veriable
strcpy(temp2->nm,temp->nm);

temp2->pageno=temp->pageno;
temp2->count=temp->count;
for(;l<=k;l++)
{
//swaping the page blocks from the RAM to its previous block in the RAM
strcpy(temp->nm,temp->next->nm);
temp->pageno=temp->next->pageno;
temp->count=temp->next->count;
temp=temp->next;
}
//inserting the user page at last in the RAM (Queue Data structure)
strcpy(rear->nm,temp2->nm);
rear->pageno=temp2->pageno;
rear->count=temp2->count;
rear->count++;
printf("\n%s",front->nm);
return;
}
k--;
}

//If Page is not in the RAM And It is in the Harddisk
printf("in the disk");
for(i=0;i<=9;i++) //finding the pageno from the harddisk
{
if(page==pointer->pageno) //if pageno find in the harddisk
{
//finding the empty place in RAM

for(temp=front;temp!=NULL;temp=temp->next)
{
if(temp->pageno==0) //Zero Indicates Empty block in the RAM
{
strcpy(temp->nm,pointer->nm);
temp->pageno=pointer->pageno;
temp->count++;
printf("\n%s",temp->nm);
pagefaultcounter++; //Increasing counter of pagefault
return;
}
}


temp=front;

//if ram is full means there is no empty block for extra page now we have //to swap t\
he page from the harddisk to the RAX
for(j=0;j<=1;j++)
{
//swaping the page blocks from the RAM to Previous blocks in the RAM
strcpy(temp->nm,temp->next->nm);
temp->pageno=temp->next->pageno;
temp->count=temp->next->count;
temp=temp->next;

}

//inserting the user page in the end of RAM (Queue Data structure)
strcpy(rear->nm,pointer->nm);
rear->pageno=pointer->pageno;
rear->count=1;
printf("\n%s",pointer->nm);
pagefaultcounter++; //Increasing counter of pagefault
return;
}

pointer++;
}

// If Page not found in the RAM
if(i==10)
{
printf("\nPage Not Found: Segmentation Fault");
}
}

void show() //Showing the RAM contains
{
struct page* temp=NULL;
printf("\n Contain Pageno Pagecount");
for(temp=front;temp!=NULL;temp=temp->next)
{
printf("\n %s %d %d",temp->nm,temp->pageno,temp->count);
}

printf("\n\n No of Times Page Fault accured: %d",pagefaultcounter);

}

int main()
{
int i;
//initalaizing the Harddisk (First Inserting the data in the Harddisk for fist time)
struct hdd hkp[10]={{"aaa",1},{"bbb",2},{"ccc",3},{"ddd",4},{"eee",5},{"fff",6},{"ggg",7},{"h\
hh",8},{"iii",9},{"jjj",10}};
struct hdd* pointer;
for(i=0;i<=2;i++)
initialize_RAM(); //creating the Ram size
while(1)
{
system("cls");
printf("\n 1. Insert");
printf("\n 2. Show");
printf("\n 3. Exit");
printf("\n Enter Your Choice: ");
scanf("%d",&i);
switch(i)
{
case 1:
system("cls");
pointer=hkp;
insert_in_RAM(pointer);
printf("\nPress Enter....");
fflush(stdin);
int getch();
break;
case 2:
system("cls");
show();
int getch();
break;
case 3:
exit(0);
}
}
}



it will b really great if u cld. help me

thankyou very much

 
1. Use CODE tag for your snippets (see Process TGML link on the form).
2. How about VC++ compiler message local variable 'temp2' used without having been initialized in insert_in_RAM()? You make a string copy via garbage pointer value...
3. Don't use continuation sign \ in one-line comments.

Correct the code and try again...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top