i did not use free function, why the submission result kept telling me "free(): invalid pointer"


  • 0
    S
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdbool.h>
    
    int** threeSum(int* nums, int numsSize, int* returnSize);
    void sort(int * p, int size);
    bool issame(int* a, int* b);
    
    int main(int argc, const char * argv[]) {
        int S[10] = {-1, 0, 1, 2, -1, -1, 1, -2, 2, -4};
        int returnsize;
        int** pintarray;
        pintarray = threeSum(S, 10, &returnsize);
        int i;
        int j;
        for (i = 0; i<returnsize; i++) {
            for (j = 0; j<3; j++) {
                printf("%d ", *(*(pintarray+i)+j));
            }
            printf("\n");
        }
        printf("%d\n", returnsize);
        return 0;
    }
    
    int** threeSum(int* nums, int numsSize, int* returnSize) {
        int** pIntArray;
        int i;
        int j;
        int k;
        int l;
        
        int count = 0;
        int *pInt;
        int *oldpInt = NULL;
        
        sort(nums, numsSize);
        for (i = 0; i < numsSize; i++) {
            for (j = i+1; j < numsSize; j++) {
                for (k = j+1; k < numsSize; k++) {
                    int sum = nums[i] + nums[j] + nums[k];
                    if(0 == sum){
                        pInt = malloc(sizeof(int) * 3);
                        pInt[0] = nums[i];
                        pInt[1] = nums[j];
                        pInt[2] = nums[k];
                        
                        for (l = 0; l < count; l++) {
                            oldpInt = *(pIntArray+l);
                            if (issame(oldpInt, pInt)) {
                                break;
                            }
                        }
                        if (l == count) {
                            count++;
                            if (1 == count) {
                                //分配一个指针的空间指向pInt指向的区域
                                pIntArray = malloc(sizeof(int*) * count);
                                *pIntArray = pInt;
                            }else{
                                pIntArray = realloc(pIntArray, sizeof(int*)*count);
                                *(pIntArray + count - 1) = pInt;
                            }
                        }
                    }
                }
            }
        }
        *returnSize = count;
        return pIntArray;
    }
    
    bool issame(int* a, int* b){
        if (!a || !b) {
            return false;
        }
        int i;
        for (i = 0; i < 3; i++) {
            if (a[i] != b[i]) {
                return false;
            }
        }
        return true;
    }
    
    void sort(int * p, int size){
        int i;
        int j;
        for (i = 0; i<size; i++) {
            int minindex = i;
            for (j = i+1; j<size; j++) {
                if (p[j] < p[minindex]) {
                    minindex = j;
                }
            }
            int temp = p[minindex];
            p[minindex] = p[i];
            p[i] = temp;
        }
    }
    
    Runtime Error Message:
    free(): invalid pointer: 0x0000000000402661 ***
    Last executed input:
    []
    

Log in to reply
 

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