gpt4 book ai didi

c - 返回c中未结束的方法

转载 作者:行者123 更新时间:2023-11-30 14:23:06 25 4
gpt4 key购买 nike

编辑 - 我发现了算术错误,但仍然有返回错误

由于某种原因,我的程序出现了两个错误。第一个错误是我的每个methods()末尾的“return”并没有结束该方法并将我带回main。我的第二个问题在第 23 行,其中 pfNum = mainSize/pageSize;给我一个“SIGFPE,算术异常”,不知道为什么会发生这两种情况,有人可以帮助我吗?

谢谢

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

/* Define page table as dynamic structure containing virtual page and page frame
and initialize variable as pointer to structure */
struct table{
int vp;
int pf;
}*pageTable = NULL;
/* Declare global var's */
int mainSize,pageSize,policy,pfNum;
/**********************************************************************/
void option1(){
/* Declare local var's */
int k;
/* Prompt for main memory size, page size, and replacement policy */
printf("Enter main memory size(words): ");
scanf("%d",&mainSize);
printf("Enter page size(words/page): ");
scanf("%d",&pageSize);
printf("Enter replacement policy(0=LRU, 1=FIFO): ");
scanf("%d",&policy);
pfNum = mainSize/pageSize;
/* Allocate and initialize page table based on number of entries */
pageTable = malloc(pfNum *sizeof(pageTable));
for(k=0;k<pfNum;k++){
pageTable[k].vp=-1;
pageTable[k].pf=k;
}
return;
}
/**********************************************************************/
void option2(){
/* Declare local var's */
int va,page,offset,i=0,temp;
/* Prompt for virtual address */
printf("Enter virtual memory address to access: ");
scanf("%d",&va);
/* Translate virtual mem addr to virtual page and offset*/
page = va/pageSize;
offset = va%pageSize;
/* Check for end of table, unallocated entry, or matched entry in table
and update table appropriately; while none of three cases, keep looping */

while(i<pfNum && pageTable[i].vp!=1 && pageTable[i].vp!=page)
i++;
if(i<=pfNum){
int j;
temp = pageTable[0].pf;
for(j=1;j<pfNum;j++)
pageTable[j-1]=pageTable[j];
pageTable[j].vp=page;
pageTable[j].pf=temp;
printf("Page Fault!");
}
else if(pageTable[i].vp==-1){
pageTable[i].vp = page;
printf("Page fault!");
}
else if(pageTable[i].vp==page){
temp = pageTable[i].pf;
int l,address;
for(l=i+1;l<pfNum-1;l++)
pageTable[l-1]=pageTable[l];
pageTable[l].vp = page;
pageTable[l].pf = temp;
address = (temp*pageSize)+offset;
printf("Virtual address %d maps to physical address %d",va,address);
}
return;
}
/**********************************************************************/
void option3(){
/* Declare local var's */
int u;
for(u=0;u<pfNum;u++ && pageTable[u].vp!=-1)
printf("VP %d --> PF %d",pageTable[u].vp,pageTable[u].pf);
/* Print out each valid virtual page and page frame pair in table */

return;
}


/**********************************************************************/
int main(){
/* Declare local var's */
int choice;
/* Until user quits, print menu of options, prompt for user input, and select appropriate option */
printf("/n");
printf("Virtual memory to Main memory mapping:\n");
printf("--------------------------------------\n");
printf("1) Set parameters\n");
printf("2) Map virtual address\n");
printf("3) Print page table\n");
printf("4) Quit\n");
printf("\n");
printf("Enter Selection: ");
scanf("%d",&choice);
printf("\n");
while(choice!=4){
if(choice==1)
option1();
else if(choice==2){
option2();
}
else if(choice==3)
option3();
}
printf("Goodbye. Have a nice day.");
return 1;
}

最佳答案

“SIGFPE,算术异常”异常很可能是由零除引起的。

关于c - 返回c中未结束的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13205033/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com