cpointersmemory-managementfreezerealloc

C Double Pointer Reallocation program hangs upon being ran twice in a row


Whenever I try to run this code I've written, it works fine the first time. The second time, the execution hangs up at the end ( it executes everything in the function, but never exits the function ). All the files are in the same folder. These are the files:

main.c

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "matrixTests.h"

int main() {
    
    int8_t a = 0;
    int8_t *p = &a;
    printf("\n%d ", *p);
    printf("\n0x%X ", p);
    printf("\n0x%X ", &a);
    printf("\n0x%X ", &p);
    printf("\n-----------------\n");
    //printSeven();
    //multiDimensionalTest();
    array2Type row_sizes[] = {1,2,3,2,1,7};
    matrixCustomRowLengthsResize(6, row_sizes);
    printf("\n-----------------\n");    

    return 0;
}

matrixTests.h

#ifndef matrixTests
#define matrixTests
#include <stdint.h>

#define array1Type uint16_t 
#define array2Type uint16_t 

void multiDimensionalTest();
void multiDimensionalDemo();
void matrixCustomRowLengths(array2Type nr_rows, array2Type row_lenghts[]);
void matrixCustomRowLengthsResize(array2Type nr_rows, array2Type row_lenghts[]);
void printSeven();


#endif

matrixTests.c (I've included only relevant functions)

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "matrixTests.h"

void matrixCustomRowLengths(array2Type nr_rows, array2Type row_lenghts[])
{
    array2Type i = 0;
    array2Type j = 0;
    array2Type arr_size_1 = nr_rows;
    array2Type **arr = (array2Type**)malloc(arr_size_1 * sizeof(array2Type));
    for(i=0;i<arr_size_1;i++)
    {
        printf("Array %d: ", i);
        *(arr+i) = (array2Type*)malloc(row_lenghts[i] * sizeof(array2Type));
        for(j = 0; j < row_lenghts[i]; j++)
        {
            *(*(arr+i) + j) = (array2Type)(i + j);
            printf("Column %d: val = %hu | ", j, *(*(arr+i) + j));
        }
        printf("\n");
    }

    for( i = arr_size_1-1; i>0; i--)
    {
        free(*(arr+i));
    }
    free(arr);

}

void matrixCustomRowLengthsResize(array2Type nr_rows, array2Type row_lenghts[])
{
        array2Type i = 0;
    array2Type j = 0;
    array2Type arr_size_1 = nr_rows;
    array2Type **arr = (array2Type**)malloc(arr_size_1 * sizeof(array2Type));
    for(i=0;i<arr_size_1;i++)
    {
        printf("Array %d: ", i);
        *(arr+i) = (array2Type*)malloc(row_lenghts[i] * sizeof(array2Type));
        for(j = 0; j < row_lenghts[i]; j++)
        {
            *(*(arr+i) + j) = (array2Type)(i + j);
            printf("Column %d: val = %hu | ", j, *(*(arr+i) + j));
        }
        printf("\n");
    }
    
    printf("\nResize Example\n");
    uint16_t index_resize = nr_rows / 2;
    uint16_t resize_size = 10;
    i = index_resize;
    row_lenghts[i] = (array2Type)resize_size;
    *(arr+i) = (array2Type*)realloc(*(arr+i), row_lenghts[i] * sizeof(array2Type));
    for(i=0;i<arr_size_1;i++)
    {
        printf("Array %d: ", i);
        for(j = 0; j < row_lenghts[i]; j++)
        {
            *(*(arr+i) + j) = (array2Type)(i + j);
            printf("Column %d: val = %hu | ", j, *(*(arr+i) + j));
        }
        printf("\n");
    }

    for( i = arr_size_1-1; i>0; i--)
    {
        free(*(arr+i));
    }
    free(arr);
}

I run my code with this script:

runThis.bat

gcc -o test test.c matrixTests.c
.\test

The problem only appears when running the matrixCustomRowLengthsResize function.

I tried running it, and expected it to work every time - but it hangs on the second execution.

EDIT: fixed a typo in main.c


Solution

  • It seems the problem has been solved. The problem was this line:

    array2Type **arr = (array2Type**)malloc(arr_size_1 * sizeof(array2Type));
    

    It should've been:

    array2Type **arr = malloc(arr_size_1 * sizeof(array2Type*));
    

    This would cause a problem later in the program when I would reallocate memory to a given row, since I would cast the malloc to (array2Type*) type, but that memory block was the size of a (array2Type) type instead.

    *(arr+i) = (array2Type*)realloc(*(arr+i), row_lenghts[i] * sizeof(array2Type)); //here is where the discrepancy between (array2Type*) and (array2Type) would cause a problem.
    

    For some reason it would result in incorrect behaviour only on a second run. This problem however actually threw a segmentation fault error in an online C compiler, but not on my machine.

    The malloc casts were not a problem, but I understand that they are unnecessary.