0

I wrote this program that basically allocate a matrix of chars using pointer. Here's the code:

char **bitmap;

void create_bitmap() {
  int i;
  int columns = (int) ceil(m / 8) + 1;
  bitmap = (char **) malloc(sizeof(char) * n);

  for (i = 0; i < n; i++)
    bitmap[i] = (char *) calloc(columns, sizeof(char));
}

void free_bitmap() {
  int i;

  for (i = 0; i < n; i++) {
    free(bitmap[i]);
  }

  free(bitmap);
}

The heap block error comes out when at the last instruction of the free_bitmap() procedure. I just can't figure out what triggers the error. Thanks in advance.

Francesco
  • 78
  • 6
  • 2
    `malloc(sizeof(char) * n);` => `malloc(sizeof(char *) * n);` – Gerhardh Feb 01 '23 at 11:25
  • Aside: [Do I cast the result of malloc?](https://stackoverflow.com/a/605858/2505965) – Oka Feb 01 '23 at 11:28
  • Your code is incomplete. Please [edit] your question and show a [mre]. See also https://airbus-seclab.github.io/c-compiler-security/ – Bodo Feb 01 '23 at 11:28

1 Answers1

1

The error was simple and I guess this is the final proof that I am not focused this morning.

In the procedure create_bitmap(), the malloc istruction would have been written:

bitmap = (char **) malloc(sizeof(char *) * n);

Now, this istruction will allocate n char* pointers, instead of only a char. Pardon.

Francesco
  • 78
  • 6