How to free dynamically allocated array?

M

Markus

Hi,

I have read the FAQ 6.16 on how to dynamically allocate a
two-dimensional array.

I do it like this:

double** A = (double **)malloc(m * sizeof(double *));
for(i = 0; i < m; i++)
A = (double *)malloc(n * sizeof(double));

But how do I free it? I currently have only

free(A);

but I guess it is not enough.

The FAQ is not clear about this.

TIA

markus
 
K

Krishanu Debnath

Markus said:
Hi,

I have read the FAQ 6.16 on how to dynamically allocate a
two-dimensional array.

I do it like this:

double** A = (double **)malloc(m * sizeof(double *));
for(i = 0; i < m; i++)
A = (double *)malloc(n * sizeof(double));

But how do I free it? I currently have only

free(A);

but I guess it is not enough.

The FAQ is not clear about this.


for(i = 0; i < m; i++)
free(A);
free(A);

Krishanu
 
F

Flash Gordon

This is not the preferred form and is error prone. Try
double** A = malloc(m * sizeof *A);

Reasons for this include:
Getting rid of the cast means that all compilers (including C89
compilers are *required* to produce a diagnostic if you forget to
include stdlib.h

By only specifying the type once you only have to get it right once
rather than in the three places you specified type. Think of the risk
of miscounting the *s if you did a 5D array!

It is less to type.

It is less to read

The FAQ at http://www.eskimo.com/~scs/C-faq/q6.16.html is out of date in
this.

Also you need to check if malloc succeeded before you use the space you
hope was allocated. After all, where will the code below write if malloc
failed?
for(i = 0; i < m; i++)
A = (double *)malloc(n * sizeof(double));


A = malloc(n * sizeof *A);

See above.

Correct. Each successful call to malloc needs a corresponding free.

OK, you read the FAQ which is good. You told us that you had read this
section which is even better. It's nice to see evidence that some people
do the right thing.
for(i = 0; i < m; i++)
free(A);
free(A);


This is correct.
 
D

Default User

Markus said:
Hi,

I have read the FAQ 6.16 on how to dynamically allocate a
two-dimensional array.

I do it like this:

double** A = (double **)malloc(m * sizeof(double *));
for(i = 0; i < m; i++)
A = (double *)malloc(n * sizeof(double));

But how do I free it? I currently have only

free(A);

but I guess it is not enough.

The FAQ is not clear about this.


The answer to 7.23 basically covers your question, although that's
about a dynamic structure.

http://www.eskimo.com/~scs/C-faq/q7.23.html


The advice there, one free() for every malloc() is the right one.



Brian
 
M

Markus

Flash Gordon said:
This is not the preferred form and is error prone. Try
double** A = malloc(m * sizeof *A);

Reasons for this include:
Getting rid of the cast means that all compilers (including C89
compilers are *required* to produce a diagnostic if you forget to
include stdlib.h

By only specifying the type once you only have to get it right once
rather than in the three places you specified type. Think of the risk
of miscounting the *s if you did a 5D array!

It is less to type.

It is less to read

The FAQ at http://www.eskimo.com/~scs/C-faq/q6.16.html is out of date in
this.

Also you need to check if malloc succeeded before you use the space you
hope was allocated. After all, where will the code below write if malloc
failed?

Thx a lot for your kind advice. Is this proper way to test for malloc
failing?

if(A == NULL)
printf("\nmalloc failed!");

You see I am not good with pointer arithmetic...

markus
 
S

Simon Biber

Markus said:
Thx a lot for your kind advice. Is this proper way to test for malloc
failing?

if(A == NULL)
printf("\nmalloc failed!");

You see I am not good with pointer arithmetic...

Yes, but the \n should go at the end of the line, not the beginning. I
would make A be lowercase, as I reserve all-capitals for macro names.
It's also usual to direct error messages to stderr instead of stdout.

double** a = malloc(m * sizeof *a);

if(a == NULL)
{
fprintf(stderr, "malloc failed!\n");
exit(EXIT_FAILURE);
}

This will exit the program if malloc fails, which may or may not be what
you want your program to do in that case.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top