Array gone after a breaking from a switch-case?

Viewed 44

First time writing here and new to C. So I'm writing a program to calculate CPI and MIPS with a menu function with four options, one to input data, one to print that data in a table, one to calculate using the data, and one to quit the program. I'm using a switch-case loop to achieve this. Still, when the user goes to input data, the program is supposed to initialize and input the data into an array which it successfully does but when the array input loop finishes and the case breaks and goes back to the menu the array is gone from memory. Below is what I've written so far, the 2nd and 3rd cases are not completed yet so disregard them unless it's relevant. Any help would be much appreciated.

int main(int argc, const char * argv[]) {
    //variables
    int numOfClass = 0, machineMhz = 0, currentClass = 0, currentCPI = 0, currentCount = 0, quitFunction = 0, menuSelection;
    //MENU;
    while (quitFunction == 0) {
        printf("Performance Assessment:\n-----------------------\n");
        printf("1) Enter parameters\n");
        printf("2) Print table of parameters\n");
        printf("3) Print table of performance\n");
        printf("4) Quit\n\n\n");
        printf("Enter Selection: ");
        scanf("%d", &menuSelection);
        
        switch (menuSelection) {
            case 1:{
                printf("\nYou've selected \"Enter parameters\"\n");
                
                //getting number of classes
                printf("\nEnter the number of instruction classes: " );
                scanf("%d", &numOfClass);
                //initialize data arrays
                int cpiArray[numOfClass]; //array holding CPI data
                int instructionCountArray[numOfClass]; //array holding instruction count data
                //getting frequency of machine in MHz
                printf("\nEnter the frequency of the machine: ");
                scanf("%d", &machineMhz);
                
                //loop to get CPI and Instruction count
                for (int i = 0; i < numOfClass; i++) {
                    printf("Enter CPI of Class %d: ", i+1);
                    scanf("%d", &cpiArray[i]);
                    printf("Enter instruction count of Class %d (millions): ", i+1);
                    scanf("%d", &instructionCountArray[i]);
                    printf("\n");
                }
                printf("\n\n");
                break;
            }
            case 2: {
                currentClass = 1; //class counter that increments every loop
                printf("\nYou've selected \"Print table of parameters\"\n");
                printf("----------------------\n");
                printf("| Class | CPI | Count |\n");
                do {
                    printf("| %5d | %3d | %4d  |\n", currentClass, currentCPI, currentCount);
                    ++currentClass;
                }
                while (currentClass == numOfClass);
                
                break;
            }
            case 3:{
                printf("\nYou've selected \"Print table of performance\"\n");
                printf("function not built...sending you back to main menu...\n\n");
                break;
            }
            case 4:{
                printf("\nYou've selected \"Quit\"\n");
                printf("Goodbye!\n\n");
                quitFunction = 1;
                break;
            }
        }
    }
    
    return 0;
}

1 Answers

As people previously stated, the main problem is that your array is getting out of scope (it exists only inside case 1.

The question now is how can you solve this problem if:

  1. You don't know initially the numOfClass and
  2. I assume that you don't know how many times the user going to enter data (select option 1 two times for example).

Since there are no arrays with variable size built-in in C as there are in C++ (vectors), one possible solution is to use an int * (integer pointer) as an array and allocate/reallocate the needed amount of memory based on numOfClass and store the overall size manually.

So for example, your code would look something like:

int numOfClass = 0, machineMhz = 0, currentClass = 0, currentCPI = 0, currentCount = 0, quitFunction = 0, menuSelection;
// initialize the array. This equals to int cpiArray[1];
int *cpiArray = malloc(sizeof(int));
// Store the total size of the array
int cpiArraySize = 1;

while (quitFunction == 0) {
    // Prints here

   switch (menuSelection) {
        case 1:{
            //getting number of classes
            printf("\nEnter the number of instruction classes: " );
            scanf("%d", &numOfClass);
            // reallocate to needed size
            cpiArray =  realloc(cpiArray, (cpiArraySize * sizeof(int) + (numOfClass * sizeof(int))));
            // update the total size
            cpiArraySize += numOfClass;
            // rest of your code
            // you can access the array elements as usually with cpiArray[i].

Also don't forget to free the memory before you exit: free(cpiArray);

Note. I didn't test my changes on your code

Related