0

I have problem with freeing the memory after executing the program. Sees someone, where is the problem? I think that I free everything, what i have allocated before.Thanks. Here is my code:

  #include <stdio.h>
#include <malloc.h>
#include "ca.h"

#define wrong_input "Chybny vstup.\n"

int *bubbleSort(int *arrayWithValues, int *helpArr, int size);

void free2DArray(int **array, int numOfRows);

int main() {
    int zeroOnLine;
    int zeroCounter = 0;
    short numOfColors;
    long long numOfAutomaton;
    scanf("%hd", &numOfColors);
    switch (numOfColors) {
        case 2:
            numOfAutomaton = 256;
            break;
        case 3:
            numOfAutomaton = 7625597484987;
            break;
        default:
            printf(wrong_input);
            return 100;
    }

    long long rule;
    scanf("%lld", &rule);

    if (rule < 0 || rule >= numOfAutomaton) {
        printf(wrong_input);
        return 101;
    }
    int numOfSteps;
    scanf("%d", &numOfSteps);
    if (numOfSteps < 1) {
        printf(wrong_input);
        return 102;
    }
    int inputLength;
    scanf("%d", &inputLength);
    if (inputLength <= 0) {
        printf(wrong_input);
        return 103;
    }
    int i;
    int **result = (int **) calloc((numOfSteps + 1), sizeof(int *));
    for (int row = 0; row < numOfSteps + 1; row++) {
        result[row] = (int *) calloc(inputLength, sizeof(int));//LINE 55-Problem is there:)
    }
    for (i = 0; i < inputLength; i++) {
        scanf("%d", &result[0][i]);

    if (result[0][i] > (numOfColors - 1) || result[0][i] < 0) {
        printf(wrong_input);
        return 104;
    }
}
ca_cfg_t *ca = ca_init(numOfColors, rule);


int *zeroArr = calloc(numOfSteps + 1, sizeof(int));
zeroOnLine = 0;
for (int j = 0; j < inputLength; j++) {
    if (result[0][j] == 0) {
        printf(".");
        zeroOnLine++;
    } else {
        printf("%d", result[0][j]);
    }
}
zeroArr[0] = zeroOnLine;
zeroCounter += zeroOnLine;
printf("\n");
for (int row = 1; row < numOfSteps + 1; row++) {
    zeroOnLine = 0;
    result[row] = ca_step(ca, result[row - 1], inputLength);
    for (int column = 0; column < inputLength; column++) {
        if (result[row][column] == 0) {
            printf(".");
            zeroOnLine++;
            } else {
                printf("%d", result[row][column]);
            }
        }
        zeroArr[row] = zeroOnLine;
        zeroCounter += zeroOnLine;
        printf("\n");
    }
    printf("Celkovy pocet nul je %d\n", zeroCounter);
    int *helpArr = calloc(numOfSteps + 1, sizeof(int));
    for (int i = 0; i < numOfSteps + 1; ++i) {
        helpArr[i] = i;
    }
    helpArr = bubbleSort(zeroArr, helpArr, (numOfSteps + 1));
    int sortedRow;
    for (int row = 0; row < numOfSteps + 1; row++) {
        sortedRow = helpArr[row];
        for (int column = 0; column < inputLength; column++) {
            if (result[sortedRow][column] == 0) {
                printf(".");
            } else {
                printf("%d", result[sortedRow][column]);
            }
        }
        printf("\n");
    }
    free(helpArr);
    free(zeroArr);
    free2DArray(result, numOfSteps + 1);
    ca_done(ca);
    return 0;
}

void free2DArray(int **array, int numOfRows) {
    for (int row = 0; row < numOfRows; row++)
        free(array[row]);
    free(array);
}

int *bubbleSort(int *arrayWithValues, int *helpArr, int size) {
    for (int i = 0; i < size - 1; i++) {
        for (int j = 0; j < size - i - 1; j++) {
            if (arrayWithValues[helpArr[j + 1]] > arrayWithValues[helpArr[j]]) {
                int tmp = helpArr[j + 1];
                helpArr[j + 1] = helpArr[j];
                helpArr[j] = tmp;
            }
        }
    }
    return helpArr;
}

and there is valgrind log below:

    ==1949== Memcheck, a memory error detector
==1949== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==1949== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==1949== Command: ./s_main_o
==1949== 
==1949== 
==1949== HEAP SUMMARY:
==1949==     in use at exit: 100 bytes in 5 blocks
==1949==   total heap usage: 15 allocs, 10 frees, 332 bytes allocated
==1949== 
==1949== 100 bytes in 5 blocks are definitely lost in loss record 1 of 1
==1949==    at 0x4C272B8: calloc (vg_replace_malloc.c:566)
==1949==    by 0x400848: main (main.c:55)
==1949== 
==1949== LEAK SUMMARY:
==1949==    definitely lost: 100 bytes in 5 blocks
==1949==    indirectly lost: 0 bytes in 0 blocks
==1949==      possibly lost: 0 bytes in 0 blocks
==1949==    still reachable: 0 bytes in 0 blocks
==1949==         suppressed: 0 bytes in 0 blocks
==1949== 
==1949== For counts of detected and suppressed errors, rerun with: -v
==1949== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 4 from 4)

The program counts Cellular automatons. Input is for example: 2 30 5 0 0 1 0 0

preneond
  • 497
  • 2
  • 5
  • 21
  • 1
    First of all you should be including `` and not ``. Secondly you don't have to [cast the result of `malloc`](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). Thirdly, please edit your question to include the input you give to the program. – Some programmer dude May 17 '16 at 08:58
  • By the way, if you look at the Valgrind output, you will see that the memory is allocated from the file `main.c` on line 55. Which line is that? – Some programmer dude May 17 '16 at 08:59
  • 1
    @JoachimPileborg, ``? Do you mean ``? – David Ranieri May 17 '16 at 09:10
  • @JoachimPileborg Could you clarify what you mean by “you should be including ”? – Pascal Cuoq May 17 '16 at 09:11
  • You don't free memory when the `main()` returns with `return 104;` – Rohan May 17 '16 at 09:19
  • Also, no inputs/putputs are shown. We don't know what you entered, so it's more difficult to detect where the 100 came from:( If you hide debug info from us, you will get down and/or close voted. – Martin James May 17 '16 at 09:22
  • what does `ca_step` do? `result[row]` gets reassigned, so maybe the original pointer is lost – Karsten Koop May 17 '16 at 09:34
  • 1
    @AlterMann Oh, brainfart, yes of *course* I mean ``! My excuse is that I was just doing some C++ programming and had just included ``... :) – Some programmer dude May 17 '16 at 09:38

0 Answers0