0

How can I fix this error? It is caused by initializing my arrays pgAccess, pgArray, and pgRequest to variable sizes that are all global variables used throughout the program. I know there may be some undeclared variables in some of the methods but I would like to focus on the problem with these arrays. I'm working with virtual memory algorithms here.

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);

}

OsayiZoom
  • 21
  • 4
  • 1
    First, you must use strcmp instead of if(argv[2] == "FIFO") – Luv May 01 '20 at 03:48
  • You're right I will change that – OsayiZoom May 01 '20 at 03:51
  • 1
    `How can I fix this error?` What error? Please see [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). – dxiv May 01 '20 at 03:53
  • `pgAcces[numRequests]` The variable `numRequests` is undefined here. Use `malloc`. – 001 May 01 '20 at 03:53
  • @JohnnyMopp it was defined on the previous line – M.M May 01 '20 at 03:57
  • I did that and it seemed to produce more errors – OsayiZoom May 01 '20 at 04:04
  • 2
    It's hard to answer your question because there's so much code. It would really help us out if you could trim out anything that's not *directly* relevant to your question and create a [mre]. It's fine if the stripped down program isn't particularly useful as long as it still demonstrates the problem. You can, for example, hard code user input, or replace function calls with canned results. This isn't just for our benefit; you might very well figure out the answer yourself while creating the MRE! – John Kugelman May 01 '20 at 04:15
  • @JohnnyMopp line 2 defines six `int` variables . Three of them have explicit initializers, but all of them are initialized to `0` (since static storage duration variables with no initializer behave as if they had a `= {0}` initializer). It seems you have some misconception about the meaning of "define". (NB - there is a technical point about tentative definitions that could be made here but it does not affect the conclusion) – M.M May 01 '20 at 13:32
  • It is the wrong close reason. It ought to be closed as a duplicate. There are [a lot of duplicates](https://stackoverflow.com/questions/linked/13645936). – Peter Mortensen Jul 29 '23 at 11:44
  • Possible duplicate: *[Variably modified array at file scope in C](https://stackoverflow.com/questions/13645936/variably-modified-array-at-file-scope-in-c)* (see also [the linked questions](https://stackoverflow.com/questions/linked/13645936)). – Peter Mortensen Jul 29 '23 at 11:45

2 Answers2

0

You still have to use malloc(). Just make sure that you used it the correct way.

Declare your arrays as pointers,

int *pgAccess, *pgArray, *pgRequest;

when allocating,

pgAccess=malloc(sizeof(int)*numRequest);

Notice that this is not the only issue in your program.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user12986714
  • 741
  • 2
  • 8
  • 19
0

Here, you have not initialized the variable numRequest and numPages, and you are passing it inside the initialization of an array. This might be causing the error in your code. Because numRequest and numPages might have some random garbage values as they are not initialized.

So either you declare the array after setting those numRequest and numPages or take the pointer instead of array as mentioned in this answer.

het jagani
  • 47
  • 1
  • 4