1

I've allocated a two-dimensional pointer array. I need to deallocate this pointer array w/o using array notation e.g., indices or offset values.

main.c

#include "practice.h"

int main() {
   int **A, **B, **C; // declare arrays.
   initNumSpace(A, B, C); // allocate space of arrays.
   freeNumSpace(A, B, C); // dealloc space of arrays.
}

practice.h

#ifndef PRACTICE
#define PRACTICE

/**
 * Function to alloc space of params.
 * @param A is a 2D pointer array.
 * @param B is a 2D pointer array.
 * @param C is a 2D pointer array.
*/
void initNumSpace();

/**
 * Function to free allocated space of params.
 * @param A is a 2D pointer array.
 * @param B is a 2D pointer array.
 * @param C is a 2D pointer array.
*/
void freeNumSpace();

#endif

practice.c

#include <stdlib.h>
#include "practice.h"

// allocate space of arrays.
void initNumSpace(int **A, int **B, int **C) {
    A = malloc(sizeof(int*) * 10);
    B = malloc(sizeof(int*) * 10);
    C = malloc(sizeof(int*) * 10);

    int** a = A;
    int** b = B;
    int** c = C;

    for (int i = 0; i < 10; i++) {
       *a = malloc(sizeof(int) * 10);
       *b = malloc(sizeof(int) * 10);
       *c = malloc(sizeof(int) * 10);

       a++;
       b++;
       c++;
   }
}

// de-alloc space
void freeNumSpace(int **A, int **B, int **C) {
   int** a = A;
   int** b = B;
   int** c = C;

   int* p = *a;
   int* q = *b;
   int* r = *c; 

   for (int i = 0; i < 10; i++) {
       p = *a;
       q = *b;
       r = *c;
           free(p);
           free(q);
           free(r);
       a++;
       b++;
       c++;
    }

    free(A);
    free(B);
    free(C);
}

There is an error output in Valgrind, specifying misuse of allocated or de-allocated space. I would like to know if the program can better de-allocate the space without misuse of memory.

TheShanachie
  • 45
  • 10
  • 2
    `int** A, B, C;` => `int ** A, ** B, ** C;` – Avi Berger Sep 21 '22 at 15:55
  • 1
    Your code is somewhat obfuscated (and thus not reproducible) by not giving us the definition of `A`, `B`, and `C`. You also haven't provided a complete example that includes both allocating and deallocating memory within the same code. – h0r53 Sep 21 '22 at 15:55
  • 2
    The valgrind diagnostic you report is more likely to be about the program's *use* of the allocated space than about its allocations and deallocations themselves (to the extent that those are separable). If you compile in debug mode and analyze a run of the resulting program with Valgrind then the output will give you a lot more detail about exactly where the issue is. – John Bollinger Sep 21 '22 at 15:56
  • 2
    Currently we don't know what source lines the Valgrind message refers to. Please [edit] your question and copy&paste your complete program (as a [mre]) and the full error message from Valgrind. `int** A, B, C;` defines a pointer `int **A` and two integer values `int B` and `int C`. Depending on your platform, an `int` might be too small to hold a pointer value, so the assignment `B = malloc(...)` may truncate the value, then `int** b = B;` would result in an invalid pointer value. You should get compiler warnings about wrong types. – Bodo Sep 21 '22 at 16:05
  • You shouldn't need `p`, `q` and `r` in the posted code. You could replace `free(p);` with `free(*a);` etc. – Ian Abbott Sep 21 '22 at 16:18
  • rather this `int** A, B, C;` --> `int **A; int B; int C;` – CGi03 Sep 21 '22 at 16:33
  • @CGi03 do you mean `int **A; int **B; int **C;`? – Willis Hershey Sep 21 '22 at 17:20
  • TheShanachie, Tip: even with a square matrix, use code like `#define M 10 #define N M` and the `M, N` in code rather than `10` to add clarity. – chux - Reinstate Monica Sep 21 '22 at 18:03
  • 2
    "I've allocated a two-dimensional pointer array." Perhaps you have, but the code you show doesn't even compile, so it's impossible to tell what you did in your real code. – n. m. could be an AI Aug 24 '23 at 03:56
  • @TheShanachie, Posted code does not compile. Try compiling what is posted. Post a [mcve]. "I'm having trouble deallocating memory within my program. " --> post _trouble_ details. – chux - Reinstate Monica Aug 24 '23 at 04:41
  • I've redefined the question. This is longer, but hopefully it can make the desired answer more clear. P.S. This is very old, but I'm trying to better understand how to ask questions through this forum. – TheShanachie Aug 24 '23 at 19:03

1 Answers1

3

Save time. Enable all warnings

B = malloc(sizeof(int*) * 10);

Warning: assignment to 'int' from 'void *' makes integer from pointer without a cast [-Wint-conversion]

Fix with:

int** A, B, C; --> int **A, **B, **C;

Allocate to the referenced object, not type

Less error prone. Easier to review and maintain.

// A = malloc(sizeof(int*) * 10);
A = malloc(sizeof *A * 10);
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256