free()崩溃代码

时间:2017-04-26 10:13:47

标签: c malloc

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

void printingArr(int** arr, int rows);
void sortingEachOneOfThem(int** pArr, int rows);
void sortingTheWholeArray(int** pArr, int rows);
void bubbleSort(int* arr);
void freeArray(int **a, int m);

int main(void)
{
    int** pArr = 0;
    int numOfRows = 0;
    int sizes = 0;
    printf("Enter number of rows: ");
    scanf("%d", &numOfRows);
    pArr = (int**) malloc(sizeof(int*) * numOfRows);
    if (pArr == NULL)
    {
        printf("Unsuccessful malloc!\n");
        return 1;
    }

    for (int i = 0; i < numOfRows; i++)
    {
        printf("Enter array length for row %d: ",i);
        scanf("%d", &sizes);
        pArr[i] = (int*) malloc(sizeof(int) * sizes + 1);
        if (pArr[i] == NULL)
        {
            printf("Unsuccessful malloc!\n");
            return 1;
        }   
        pArr[i][0] = sizes;
        for (int k = 1; k < sizes + 1; k++)
        {
            printf("Enter value for array: ");
            scanf("%d", &pArr[i][k]);
        }
    }
    printingArr(pArr, numOfRows);
    sortingEachOneOfThem(pArr, numOfRows);
    printingArr(pArr, numOfRows);
    sortingTheWholeArray(pArr, numOfRows);
    printingArr(pArr, numOfRows);


    for (int i = 0; i < numOfRows; i++)
    {
        if (pArr[i] != NULL)
        {
            free(*(pArr + i));
        }
    }
    //free(pArr);

    system("pause");
    return 0;
}
/*
this amazing, wonderfull piece of program prints the array given
input: int** arr, int rows
output: none
*/
void printingArr(int** arr, int rows)
{
    int i = 0;
    int k = 0;
    for (i = 0; i < rows; i++)
    {
        for (k = 0; k <= arr[i][0]; k++)
        {
            printf("%d ", arr[i][k]);
        }
        printf("\n");
    }
    printf("\n");
}
/*
This beautiful function sorts the whole array, but its length of rows like a pyramid
input: int** arr, int rows
output: none
*/
void sortingTheWholeArray(int** pArr, int rows)
{
    int* temp = 0;
    int i = 0, k = 0;
    for (i = 0; i < rows - 1; i++)
    {
        for (k = 0; k < rows - 1; k++)
        {
            if (pArr[k][0] > pArr[k + 1][0])
            {
                temp = pArr[k];
                pArr[k] = pArr[k + 1];
                pArr[k + 1] = temp;
            }
        }
    }
}
/*
This little small function sorts every row of the array of arrays given to it
input: int** arr, int rows
output: none
*/

void sortingEachOneOfThem(int** pArr, int rows)
{
    int i = 0;
    for (i = 0; i < rows; i++)
    {
        bubbleSort(pArr[i]);
    }
}

/*
This little piece of a code is a bubble sort, sorts the array given to it :)
input: int* arr, int rows
output: none
*/

void bubbleSort(int* arr)
{
    int i = 1, k = 0;
    for (i = 1; i < arr[0] - 1; i++)
    {
        for (k = 1; k <= arr[0] - i; k++)
        {
            if (arr[k] > arr[k + 1])
            {
                arr[k] += arr[k + 1];
                arr[k + 1] = arr[k] - arr[k + 1];
                arr[k] -= arr[k + 1];
            }
        }
    }
}

最后的免费崩溃我的代码,显示此错误: https://i.stack.imgur.com/nqxBG.png 函数free()在另一个代码上的相同用法效果很好,但不在这里。我已尝试逐步运行它,它在第一次免费崩溃。记忆博士显示:https://i.stack.imgur.com/rSZJr.png 另一个链接:https:// i.stack。 imgur.com/ZX2Ne.png(粘贴它,中间没有空格,不能发布2个以上的链接) 我该怎么办?

1 个答案:

答案 0 :(得分:4)

此:

pArr[i] = (int*) malloc(sizeof(int) * sizes + 1);

下分配。将单个字节添加到int数组的大小毫无意义。你可能意味着:

pArr[i] = malloc((sizes + 1) * sizeof *pArri[i]);

请勿转换malloc()的返回值,并在左侧使用sizeof