**已关闭。**此问题不符合Stack Overflow guidelines。它目前不接受回答。
这个问题似乎不是关于在help center定义的范围内编程。
三年前就关门了。
Improve this question
如何修复此错误?它是由初始化数组pgAccess、pgArray和pgRequest到变量大小引起的,这些变量都是整个程序中使用的全局变量。我知道在一些方法中可能有一些未声明的变量,但我想专注于这些数组的问题。我在用虚拟内存算法。
int flag = 0, var, currFrame = 0, numFault, currRequest, request;
int numPages, numFrames, numRequest, fc =0, go =0, c =0;
int pgAccess[numRequest], pgArray[numPages], pgRequest[numRequest];
void runningMessage(int, int, int);
void fifo();
void lru();
void opt();
void runningMessage(int pg, int frame, int msg){
if(msg == 1)
{
printf("Page %d already in Frame %d\n",pg, frame);
}
if(msg == 2)
{
printf("Page %d loaded into Frame %d\n",pg, frame);
}
if(msg == 3)
{
printf("Page %d unloaded from Fram %d\n",pg, frame);
}
}
int main(int argc, char *argv[])
{
FILE *fp;
fp = fopen(argv[1], "r");
int x =0;
fscanf(fp, "%d %d %d", &numPages, &numFrames, &numRequest);
while(!feof(fp))
{
fscanf(fp, "%d", &pgAccess[x]);
x++;
}
fclose(fp);
printf("Algorithm: %s\n\n", argv[2]);
if(argv[2] == "FIFO")
{
fifo();
}
if(argv[2] == "LRU")
{
lru();
}
if(argv[3] == "OPT")
{
opt();
}
}
void fifo(){
int i, x;
for(i = 0; i < numFrames; i++)
{
pgArray[i] = -1;
}
while(x < numRequest)
{
flag =0;
var = pgAccess[x];
for(i = 0; i < numFrames; i++)
{
if(var == pgArray[i])
{
flag = 1;
x++;
runningMessage(pgArray[i], i, 1);
break;
}
}
if(flag == 0)
{
if(currFrame < numFrames)
{
pgArray[currFrame] = pgAccess[x];
runningMessage(pgAccess[x], currFrame, 2);
currFrame++;
x++;
numFault++;
}
else if( y < numFrames)
{
runningMessage(pgArray[y], y, 3);
pgArray[y] = pgAccess[x];
runningMessage(pgAccess[x], y, 2);
x++;
y++;
numFault++;
else
{
y =0;
}
}
}
}
printf(" %d Page Faults\n\n", numFault);
}
void lru(){
int i, x, y, a, b, j;
y = numFrames;
for(i = 0; i < y; i++)
{
pgRequest[i] = 0;
pgArray[i] = -1;
}
for(i = 0; i < numRequest; i++)
{
flag = 0;
a = pgAccess[i];
for(j = 0; j < y; j++)
{
if(a == pgArray[j])
{
flag = 1;
pgRequest[j] = i;
runningMessage(pgArray[j], j, 1);
break;
}
}
if((flag == 0) && (b < y))
{
fc++;
pgArray[b] = a;
pgRequest[b] = i;
runningMessage(pgArray[b], b, 2);
b++;
}
else if((flag == 0) && (b == y))
{
fc++;
minRequest = pgRequest[0];
for(currRequest = 0; currRequest < y; currRequest++)
{
if(pgRequest[currRequest] < minRequest)
{
minRequest = pgRequest[currRequest];
request = currRequest;
}
}
pgArray[request] = a;
pgRequest[request] = i;
runningMessage(pgAccess[request], request, 3);
runningMessage(pgArray[request], request, 2);
request =0;
}
}
printf(" %d Page Faults\n\n", fc);
}
void opt(){
int i, x, y, a;
for(i = 0; i < numFrames; i++)
{
pgRequest[i] = 0;
pgArray[i] = -1;
}
for(i = 0; i < numRequest; i++)
{
flag = 0;
a = pgAccess[i];
for(y = 0; y < numFrames, y++)
{
if(a == pgArray[y])
{
flag = 1;
runningMessage(pgArray[y], y, 1);
break;
}
}
if((flag == 0) && (x < numFrames))
{
fc++;
pgArray[x] = a;
runningMessage(pgArray[x], x, 2);
x++;
}
else if((flag == 0) && (x == numFrames))
{
fc++;
for(go = 0; go < numFrames; go++)
{
pgRequest[go] = 0;
}
for(currRequest = 0; currRequest < numFrames; currRequest++)
{
c = 0;
for(currFrame = i + 1; currFrame < numRequest; currFrame++);
{
if(pgArray[currRequest] == pgAccess[currFrame])
{
if(pgRequest[currRequest] == 0)
{
pgRequest[currRequest] = currFrame;
c = 1;
}
}
}
if(c != 1)
{
pgRequest[currRequest] = numRequest + 1;
}
}
request = 0;
maxRequest = pgRequest[0];
for(go = 0; go < numFrames; go++)
{
if(pgRequest[go] > maxRequest)
{
maxRequest = pgRequest[go];
runningMessage(pgArray[go], go, 3);
request = go;
}
}
pgArray[request] = a;
runningMessage(pgArray[request], request, 2);
}
}
printf(" %d Page Faults\n\n", fc);
}
2条答案
按热度按时间bgibtngc1#
在这里,您没有初始化变量
numRequest
和numPages
,而是将其传递到数组的初始化中。这可能会导致代码中的错误。因为numRequest
和numPages
可能会有一些随机的垃圾值,因为它们没有初始化。因此,要么在设置
numRequest
和numPages
之后声明数组,要么像this答案中提到的那样使用指针而不是数组。kiz8lqtg2#
你必须使用
malloc()
。只是要确保你用正确的方式。将数组解压缩为指针,
当分配时,
请注意,这不是您的程序中的唯一问题。