Im getting double free error's from this? Why?
2d array standard implementation. any clues?

  int **m = malloc(5 * sizeof(int));

  int i,j;
  for(i = 0; i < 5; i++)
        m[i] = malloc(5 * sizeof(int));


        for(i = 0; i< 5; i++)
                free(m[i]);

        free(m);

in line 1, it should be

malloc(5*sizeof(int*));

you have to allocate memory for a int pointer, not integer. try this. ;)

nice thanks!

please mark this thread as solved, it will be helpful for everyone ;)

A good trick for avoiding this type of mistake is taking the size of the destination pointer rather than hardcoding the type:

int **m = malloc(5 * sizeof *m);
...
m[i] = malloc(5 * sizeof *m[i]);

This works because sizeof doesn't actually perform the dereference, it just evaluates to the size of the result as if had it been performed.

commented: awesome trick i must say ;) +2
commented: cool :) +3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.