1

Please bear with me as this is probably a very simple question but I am very new to C. I am trying to malloc a specific array and then free it. However, the line:

    M = malloc(N*sizeof(double *));

...doesn't work. Can somebody please explain to me why this is not working and what the solution would be? Many thanks in advance.

Nelly Dee
  • 23
  • 4
  • Yesterday I added: [C: Freeing 2D array](http://stackoverflow.com/questions/23328230/c-freeing-2d-array-heap-corruption-detected/23328992#23328992) Read. – Grijesh Chauhan Apr 28 '14 at 13:59
  • 2
    Also, [don't cast malloc](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – 001 Apr 28 '14 at 14:02
  • @JohnnyMopp It is not working without casting. I have edited the code above to reflect what I believe it should be without casting. However, the line `M = malloc(N*sizeof(double *))` returns an error – Nelly Dee Apr 28 '14 at 14:54
  • Then you are probably compiling as C++. In C++ you need the cast. – 001 Apr 28 '14 at 15:02
  • @JohnnyMopp It compiles fine, it just has a squiggly red line beneath that = sign. I don't understand why but not to worry. Thanks for the info on casting - I didn't know before today. – Nelly Dee Apr 28 '14 at 15:05

1 Answers1

2

You should free all positions, not just the first. See this example I have wrote.

As mentioned from @Grijesh, the allocation is also wrong. The example covers allocation too. Moreover, I suggest you not to cast the return of malloc (more).

You have to think the 2D array, as a 1D array, where every cell of it is a pointer to a 1D array. A picture might help: http://gsamaras.files.wordpress.com/2014/04/array2d-n.png Here, 1D array that holds the pointers is to the left and every cell of it, points to another 1D array. How many 1D arrays to the left? As many cells as you have in the left array.

Btw, Nelly I think this is not a silly question, it's something that gets beginners into trouble. ;)

EDIT: About your new code, you had to have the same definition and declaration for matrix_free, as well as, call it as you should. What's definition, etc. ?? Answer.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

#define Nmax 9000

double **makeMatrix(int N);
void free_matrix(double **M,int N);

int main(void) {
    int N;

    for (N = 2; N < Nmax; N *= 2) {
        double **L = makeMatrix(N);
        printf("yes \n");
        free_matrix(L, N);
        printf("woo \n");
    }
    return 0;
}

double **makeMatrix(int N) {
    int i, j;
    double **M;

    M = malloc(N * sizeof(double *));
    for (i = 0; i < N; i++)
        M[i] = malloc(N * sizeof(double));

    for (i = 1; i < N; i++) {
        for (j = 1; j < N; j++) {
            M[i][j] = (i) * (j) * M_PI / N;
        }
    }

    return (M);
}

void free_matrix(double **M, int N) {
    int i;
    for (i = 1; i <= N; i++) {
        free(M[i]);
    }
    free(M);
}

And then I receive the youwho output. :) But, it will stop at a certain point, because NMAX is too big! Not only NMAX is too big, but N grows really fast ( N *=). Have you done the math in a piece of paper? Too big numbers. For example, if I do N +=, then, I can go until NMAX = 9000. Debug tip: How do I know in which loop it reaches? I printed out the counter of the loop, like this

printf("woo %d\n",N);

Of course, if you feel sure for yourself, then I suggest you learning the debugger.

Community
  • 1
  • 1
gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • True. Btw, the example covers the allocation too. I answered to Nelly's answer. Better questions shall receive better answers. – gsamaras Apr 28 '14 at 14:03
  • @G.Samaras Thanks for your lovely reply. I am still a bit confused. I allocated this way but it is not working: 'double **M;' 'M = malloc(N*sizeof(double *));' 'for(i = 0 ; i < N ; i++)' 'M[i] = malloc( N*sizeof(double) );' – Nelly Dee Apr 28 '14 at 14:34
  • @GrijeshChauhan Thanks for your links. Could you please have a look at my comment above? Thank you. – Nelly Dee Apr 28 '14 at 14:40
  • What exactly goes wrong? Maybe you could edit your original code with the new code.:) – gsamaras Apr 28 '14 at 14:41
  • @NellyDee Your allocation steps in comment looks correct, I have doubt on nested-loop just after allocation-loops in which you initialize matrix, notice you don't use `M[0][col]` and `M[row][col]` as you start from `i = 1` `for(i=1;i – Grijesh Chauhan Apr 28 '14 at 14:45
  • @G.Samaras I have edited the original code. It doesn't allow M to equal malloc. I get error: a value of type "*void*" cannot be assigned to an entity of type "double *" :( – Nelly Dee Apr 28 '14 at 14:46
  • I agree with Grijesh. @Nelly Dee indent your code please! – gsamaras Apr 28 '14 at 14:49
  • @GrijeshChauhan I see that. However, the program is registering an issue at the`M = malloc(N*sizeof(double *));` line. I don't understand why – Nelly Dee Apr 28 '14 at 14:49
  • @G.Samaras You should add about free to because, check free loop `for(i=1;i<=N;i++{} ` – Grijesh Chauhan Apr 28 '14 at 14:50
  • @NellyDee Additionally free_matrix's declaration and definition syntax are not same. – Grijesh Chauhan Apr 28 '14 at 14:53
  • @GrijeshChauhan, not sure were you are referring. :/ – gsamaras Apr 28 '14 at 14:53
  • @G.Samaras Check Code there are many mistakes, Also see loop in main -- `Nmax 9000` How much memory he is allocating!!! – Grijesh Chauhan Apr 28 '14 at 14:54
  • @NellyDee, free_matrix definition is not the same as the declaration! They must be the same! Moreover, when you call it in main, you should provide all the arguments the function needs! – gsamaras Apr 28 '14 at 14:55
  • @GrijeshChauhan I need this to work for large matrices. It should work up to 8192. After that the memory allocation is too large. I don't know where my mistake lies now – Nelly Dee Apr 28 '14 at 14:56
  • @G.Samaras Please could you kindly explain why `M = malloc(N*sizeof(double *))` is giving me error: a value of type "* void" cannot be assigned to an entity of type "double *" – Nelly Dee Apr 28 '14 at 14:58
  • See my edit. I have removed sin (you need -lm at the compiler flags to enable it). – gsamaras Apr 28 '14 at 15:02
  • I need the sine! Haha. Thanks for all your help though. I really appreciate it. :) – Nelly Dee Apr 28 '14 at 15:10