I can't figure out why this isn't working (it's part of a larger amount of code but I tested it separately and I still get an error):

int nrows = 5;
int ncolumns = 4;
int i;
int **array = malloc(nrows * sizeof(int *));
for(i = 0; i < nrows; i++)
      array[i] = malloc(ncolumns * sizeof(int));

for(i = 0; i < nrows; i++)

The error I get is: malloc: *** error for object 0x1001000a0: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
22222Abort trap

I'm new to C and in particular memory allocation so would really appreciate any help.

It seem like you put free(array); in the loop instead of outside of the loop.

Be a part of the DaniWeb community

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