2D arrays and malloc

  • Thread starter Erik de Castro Lopo
  • Start date
E

Erik de Castro Lopo

Hi all,

I found the following malloc/free example on a mailing list and immediately
thought it was wrong. So, I whipped up the following test program, compiled
it on Linux with gcc and used "-W and -Wall -ansi -pedantic". Gcc did not
complain. I then ran the executable under valgrind (a Linux memory checker
much like Purify) and it reported no errors.

I always remember 2D arrays requiring an outer malloc followed by a loop
which mallocs the second dimension and something similar for free.

So, is the following code legal? Can anybody point me to the relevant section
of the C standard.

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

#define M 8
#define N 12

int
main (void)
{ double (*a)[M] ;
unsigned j, k ;

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

for (j = 0 ; j < M ; j++)
for (k = 0 ; k < N ; k++)
a [j][k] = j * k + j;

free(a);

return 0 ;
}

--
+-----------------------------------------------------------+
Erik de Castro Lopo (e-mail address removed) (Yes it's valid)
+-----------------------------------------------------------+
"Lumping configuration data, security data, kernel tuning parameters,
etc. into one monstrous fragile binary data structure is really dumb."
- David F. Skoll
 
P

pete

Erik said:
Hi all,

I found the following malloc/free example on a mailing list and immediately
thought it was wrong. So, I whipped up the following test program, compiled
it on Linux with gcc and used "-W and -Wall -ansi -pedantic". Gcc did not
complain. I then ran the executable under valgrind (a Linux memory checker
much like Purify) and it reported no errors.

I always remember 2D arrays requiring an outer malloc followed by a loop
which mallocs the second dimension and something similar for free.

So, is the following code legal?
Yes.

Can anybody point me to the relevant section
of the C standard.

'a' is a pointer to an array.
#include <stdio.h>
#include <stdlib.h>

#define M 8
#define N 12

int
main (void)
{ double (*a)[M] ;
unsigned j, k ;

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

/* This is the way we usually write something like that, here: */
a = malloc(N * sizeof *a);

/*
** I find it simpler to post a rudimentary success check,
** rather than to explain why it's missing from the code:
*/
if (a == NULL) {
fputs("malloc problem\n", stderr);
exit(EXIT_FAILURE);
}
for (j = 0 ; j < M ; j++)
for (k = 0 ; k < N ; k++)
a [j][k] = j * k + j;

free(a);

return 0 ;
}

I think it would have been more natural
to associate k with M and j with N,
but in terms of size and alignment, everything is OK anyway.
 
D

Dave Thompson

On Fri, 17 Dec 2004 23:10:18 +1100, Erik de Castro Lopo
I always remember 2D arrays requiring an outer malloc followed by a loop
which mallocs the second dimension and something similar for free.

So, is the following code legal? Can anybody point me to the relevant section
of the C standard.
{ double (*a)[M] ;
a = malloc (sizeof (double) * N * M);
<snip>

Yes. You don't need the standard; see FAQ 6.16 et seq at usual places
and http://www.eskimo.com/~scs/C-faq/top.html .

The pointer to (allocated) pointers to (allocated) elements isn't
actually a 2D array at all, but a 2-level structure that can be
subscripted (used) as if it were a 2D array; that was/is the only way
to make both dimensions variable in C89, and still the only way for a
static (including global) variable in C99.

Even so it doesn't actually require a _loop_ of malloc's (and free's),
although that is a direct way and easy to remember; you can malloc
once for the row-pointers and once for all rows (unless that's so
large it exceeds a limit on your system) -- and if the elements are
characters (which cannot require nontrivial alignment) you can even
tack them onto the row pointers into a single block. Again see 6.16.

- David.Thompson1 at worldnet.att.net
 

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,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top