cruntime-error

How can i solve this c issue?


i put this code on code forces :

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

/*  1- take the input.
    2- create array of size of TestCases using malloc
    3- fill the array
    4- call comparsing function that return one int "the number of peopple ahead of tumer"
*/

int  compare(int * array, int TestCases);

int main () {
    //1-take the input
    int TestCases;
    scanf("%d", &TestCases);
    //2- create array
    int ** array = (int **) malloc( TestCases * sizeof(int *));
    // int array[10000][4];
    for (int i = 0; i < TestCases; i++){
        array[i] = (int *) malloc( 4 * sizeof(int));
    }
    
    int *output= (int *) malloc( 4 * sizeof(int));
    // 3- fill the array
    for (int i = 0; i < TestCases; i++){
        for( int j = 0 ; j < 4;  j++){
            scanf("%d", &array[i][j]);
        }
            //4- call function
     output[i] =compare ( array[i], TestCases);
     
    }
    
    for (int i = 0 ; i < TestCases;  i++){
            printf("%d\n", output[i]);
    }
    

    
    
    
    
    // free
    for (int i = 0; i < TestCases; i++){
        free(array[i]);
    }
    free(array);

    return 0;
}

int  compare(int * array,  int TestCases){

    
    int counter = 0;
    
        if(array[0] < array[1])
            counter++;
        if (array[0] < array[2])
            counter++;
        if (array[0] < array[3])
            counter++;
        

    // for (int i = 0; i < 4; i++){
        // printf("array[%d] = %d\n", i, array[i]);
    // }
    // printf("%d this counter\n", counter);
    // output[call_times] = counter; printf("%d\n",output[call_times]);

    return counter;
}

how ever i got run time error :
Test: #2, time: 15 ms., memory: 440 KB, exit code: -1073741819 (STATUS_ACCESS_VIOLATION), checker exit code: 0, verdict: RUNTIME_ERROR

Input

10000
9002 6635 7717 6452
8935 3959 8247 9158
2020 2162 355 7400
4050 672 6796 9641
7099 1943 481 9819
2470 4802 2245 8859
9378 7026 455 3976
6155 3644 4582 3696
1669 4727 2575 8523
268 1629 8811 3517
6868 9025 993 492
925 5731 8722 3969
6871 2561 6673 8114
7515 4300 2669 9796
7348 1105 8269 9712
922 4303 3719 9820
1601 2143 9908 7783
5854 2607 3368 1538
9206 5714 1353 6385
7264 3012 651 7657
9566 560 4491 4724
3879 6009 5790 3254
2751 769 6499 8160
810 2171 2236 7245
7293 8501 922...

Checker Log

Exit code is -1073741819

i tried to remove malloc and put array on the stack :

#include <stdio.h>
#include <stdlib.h>
 
/*  1- take the input.
    2- create array of size of TestCases using malloc
    3- fill the array
    4- call comparsing function that return one int "the number of peopple ahead of tumer"
*/
 
int  compare(int * array, int TestCases);
 
int main () {
    //1-take the input
    int TestCases;
    scanf("%d", &TestCases);
    //2- create array
    // int ** array = (int **) malloc( TestCases * sizeof(int *));
    int array[10000][4];
    // for (int i = 0; i < TestCases; i++){
        // array[i] = (int *) malloc( 4 * sizeof(int));
    // }
    
    int  output[10000];
    // 3- fill the array
    for (int i = 0; i < TestCases; i++){
        for( int j = 0 ; j < 4;  j++){
            scanf("%d", &array[i][j]);
        }
            //4- call function
     output[i] =compare ( array[i], TestCases);
     
    }
    
    for (int i = 0 ; i < TestCases;  i++){
            printf("%d\n", output[i]);
    }
    
 
    
    
    
    
    
 
    return 0;
}
 
int  compare(int * array,  int TestCases){
 
    
    int counter = 0;
    
        if(array[0] < array[1])
            counter++;
        if (array[0] < array[2])
            counter++;
        if (array[0] < array[3])
            counter++;
        
 
    
    return counter;
}

like this it worked i don't know why ?? i also subsect that is the second approach is bad bcz it will take alots of memory when it wont use it while first it will take only what it needs so my questions is:
1- why that run time error happens?
2- wish approach is good for performance the first one or the second?


Solution

  • int *output= (int *) malloc( 4 * sizeof(int)); allocates space for 4 int, but output[i] =compare ( array[i], TestCases); inside for (int i = 0; i < TestCases; i++){ uses TestCases elements of the allocated space, and TestCases is more than 4.