C code gets Runtime Error in some cases


  • 0
    Y

    The code gets RE when inputting

    1 18
    1
    >> realloc(): invalid old size: 0x0000000000d5f090
    

    or

    1 18
    2
    >> malloc.c:2842: mremap_chunk: Assertion `((size + offset) & (_rtld_global_ro._dl_pagesize - 1)) == 0' failed.
    

    but correct when

    1 18
    3
    >> 3 3 3 3 3 3
    

    The error occurs when reallocating the memory of *columnSizes:

    *columnSizes = (int*) realloc(*columnSizes, (*returnSize+1)*sizeof(int));
    

    *columnSizes is initialized with 0 size memory:

    *columnSizes = (int*) malloc(0*sizeof(int));
    

    Here is my code (including the main function):

    void dfs(int k, int* oneAns, int colSize, int*** ans, int** columnSizes, int* returnSize, const int* candidates, const int candidatesSize, const int target) {
        if (!target) {
            //columnSize
            *columnSizes = (int*) realloc(*columnSizes, (*returnSize+1)*sizeof(int));
            (*columnSizes)[*returnSize] = colSize;
            //combinationSum
            *ans = (int**) realloc(*ans, (*returnSize+1)*sizeof(int*));
            (*ans)[*returnSize] = (int*) malloc(colSize*sizeof(int));
            memcpy((*ans)[*returnSize], oneAns, colSize*sizeof(int));
            //returnSize
            (*returnSize)++;
            return;
        }
    
        int i;
        for (i = k; i < candidatesSize; i++) {
            if (candidates[i] > target) break;
            oneAns[colSize] = candidates[i];
            dfs(i, oneAns, colSize+1, ans, columnSizes, returnSize, candidates, candidatesSize, target-candidates[i]);
        }
    }
    
    int cmp(const void* a, const void* b) {
        return (*(int*)a - *(int*)b);
    }
    
    int** combinationSum(int* candidates, int candidatesSize, int target, int** columnSizes, int* returnSize) {
        int** ans    = (int**) malloc(0*sizeof(int*));
        int*  oneAns = (int*) malloc(candidatesSize*sizeof(int));
        *columnSizes = (int*) malloc(0*sizeof(int));
        *returnSize  = 0;
    
        qsort(candidates, candidatesSize, sizeof(int), cmp);
        dfs(0, oneAns, 0, &ans, columnSizes, returnSize, candidates, candidatesSize, target);
        free(oneAns);
        return ans;
    }
    
    
    int main() {
        int* returnSize; int** columnSizes = (int**) malloc(sizeof(int*)); int* candidates;
        int candidatesSize, target;
        scanf("%d%d", &candidatesSize, &target);
        candidates = (int*) malloc(candidatesSize*sizeof(int));
        int i; for (i = 0; i < candidatesSize; i++)
            scanf("%d", &candidates[i]);
    
        int** ans = combinationSum(candidates, candidatesSize, target, columnSizes, returnSize);
        int j; for (i = 0; i < *returnSize; i++) {
            for (j = 0; j < (*columnSizes)[i]; j++)
                printf("%d ", ans[i][j]);
            printf("\n");
            free(ans[i]);
        }
        free(*columnSizes);
    
        return 0;
    }
    

    Do anyone know what causes the error? Thank you!


  • 0
    T

    hey, i met the same problem, for some cases it works right, but gets RE(not WA) in other cases, i think something wrong in the oj :(


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.