ACCESS VIOLATION function, and dynamic arrays

42 Views Asked by At

i have a problem with the next code, the violaion happens in stdio.h i dont think i have allocated wrongly the arrays, but that also may be the reason, i have no clue on what to do, and im stuck at this problem since a while ago,

int openNread(int cpu_v, int time_v, int** CPU, int** _time) {
    HANDLE console = CodeDef();
    SetConsoleCursorPosition(console, (COORD) { 0, 4 });

    FILE* cpu_f = NULL;
    FILE* time_f = NULL;

    fopen_s(&cpu_f, "CPU.txt", "r");
    fopen_s(&time_f, "time.txt", "r");

    if (cpu_f == NULL || time_f == NULL) {
        printf("\n\nInput error, please try again");
        Sleep(1000);
    }
    else {
        *CPU = (int*)malloc(cpu_v * sizeof(int));
        if (*CPU == NULL) {
            printf("Memory allocation error for CPU array");
            return 1; 
        }

        *_time = (int*)malloc(time_v * sizeof(int));
        if (*_time == NULL) {
            printf("Memory allocation error for _time array");
            free(*CPU); 
            return 1; 
        }

        for (int i = 0; i < cpu_v; i++) {
            fscanf_s(cpu_f, "%d", &(*CPU)[i]);
        }

        for (int i = 0; i < time_v; i++) {
            fscanf_s(time_f, "%d", &(*_time)[i]);
        }

        fclose(cpu_f);
        fclose(time_f);
    }

    return 0;
}

the next part where i use this funtion is in the main.c file inside a switch, i only use this arrays again a bit later to generate a table, but

    while (!exit_flag) {
        system("cls");
        choose_pos = menu(main, console, 4);
        
        switch (choose_pos) {
            case 0:
                back_flag = 0;
                while (!back_flag) {
                    system("cls");
                    choose_pos = menu(menu1, console, 3);
                    switch (choose_pos) {
                    case 0:
                      //code for switch case 0
                        break;
                    case 1:
                        cpu_v = VerificationC();
                        time_v = VerificationT();                 
                        openNread(cpu_v, time_v, &CPU, &_time);
                        system("pause");
                        break;
                    case 2:
                        back_flag = 1;
                        break;
                    }
                }

i have no clue of what im doing wrong, since the VS compiler does not mark any warning so far

i tried getting rid of the dynamic arrays, but i cant do that, because i need to change the size of the array to a unset number later on (very big ones too), so i would like to be able to change my code.

also i will send the other two functions that may create a problem so i can give more context into my problem.

int GenerateC() {
    FILE* CPUf = NULL;

    HANDLE console = CodeDef();
    srand(time(NULL));

    SetConsoleCursorPosition(console, (COORD) { 0, 6 });

    int cpu_n;
    cpu_n = rand() % 1000;

    fopen_s(&CPUf, "CPU.txt", "w");

    if (CPUf != NULL) {
        for (int i = 0; i < cpu_n; i++) {
            fprintf(CPUf, "%d\n",   rand() % 1001);
        }
        fclose(CPUf);
    }
    return cpu_n;
}
int GenerateT() {
    FILE* timef = NULL;

    HANDLE console = CodeDef();
    srand(time(NULL));
    
    SetConsoleCursorPosition(console, (COORD){ 0, 6 });

    int time_n;
    time_n = rand() % 1000;

    fopen_s(&timef,"time.txt", "w");

    if (timef != NULL) { 

    for (int i = 0; i < time_n; i++) {
        fprintf(timef, "%d\n", 1 + (rand() % 254));
    }
     fclose(timef); 
    }
    return time_n;
}

those two are the ones that open the file and generate random numbers a random amount, then there it goes the Verification that checks if there is alreay CPU.txt and time.txt available to read.

int VerificationC() {
    int cpu_v = 1;
    FILE* CPUf = NULL;
    fopen_s(&CPUf, "CPU.txt", "r");
    char c;

    if (CPUf == NULL) {
        return cpu_v;
    }
    else {
        for (c = getc(CPUf); c != EOF; c = getc(CPUf)) if (c == '\n') cpu_v++;
        fclose(CPUf);
    }   

    return cpu_v;
}
int VerificationT() {
    int time_v = 1;
    FILE* timef = NULL;
    fopen_s(&timef, "CPU.txt", "r");
    char c;

    if (timef == NULL) {
        return time_v;
    }
    else {
        for (c = getc(timef); c != EOF; c = getc(timef)) if (c == '\n') time_v++;
        fclose(timef);
    }

    return time_v;
}
0

There are 0 best solutions below