Lidor
Lidor

Reputation: 11

free() crashes the code

#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];
            }
        }
    }
}

the free at the end crashes my code, showing this error: https://i.sstatic.net/nqxBG.png the same usage of the function free() on another code worked good, but not here. I have tried running through it in step by step mode, it crashes at the first free. Dr. memory shows this: https://i.sstatic.net/rSZJr.png another link: https:// i.stack. imgur.com/ZX2Ne.png (paste it without the spaces in the middle, Can't post more than 2 links) what can I do?

Upvotes: 0

Views: 74

Answers (1)

unwind
unwind

Reputation: 400129

This:

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

under-allocates. Adding a single byte to the size of an array of int makes little sense. You probably meant:

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

Don't cast the return value of malloc(), and use sizeof on the left-hand side.

Upvotes: 4

Related Questions