# 2D arrays:dynamic allocation and freeing

• 02-03-2005
bravetanveer
2D arrays:dynamic allocation and freeing
hi all,

The way to dynamically allocate a 2D array is :

int a(*)[5];

a=(int (*)[5])malloc(sizeof(int)*5);

This will create array of pointer to arrays.

We can access elements as a 2D array
a[1][1] with access particular element.

Question:
1.
After allocating space for pointers, is it not the case that i need to allocate space for the array they are pointing to?, as we usually do for a pointer to point to array of elements.

2.
Is my way of freeing ok

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

Because it will free the pointers , what about the elements associated with those pointers.
• 02-03-2005
Salem
> int a(*)[5];
This is a pointer to an array (not a pointer to a pointer).

> a=(int (*)[5])malloc(sizeof(int)*5);
1. Never cast malloc (see the FAQ)
2. You don't allocate enough memory. The correct call is
a = malloc ( 5 * sizeof *a );

> Is my way of freeing ok
No, there is only one malloc, so there is only one free, and that would be
free( a );
• 02-05-2005
bravetanveer
where have i found int (*)[5];
Sorry for that message above:

This is valid syntax.
Actually, i was going through code for mp3, at that time i found this:

[/QUOTE][/PHP][/CODE]
{
....
....
....
};

[CODE][PHP][QUOTE]

And this alloates pointers to array of characters i.e. strings.
In this array lots of strings can be stored.

Am I wrong with the above statement?
If yes, do correct me.

regards
bravetanveer
• 02-05-2005
quzah
Why are you bothering with all this convoluted crap? Why don't you just use a pointer to a pointer, malloc N poitners, then malloc M bytes for each?
Code:

```unsigned char **twod; int x; twod = malloc( N * sizeof( unsigned char * ) ); for( x = 0; x < N; x++ )     twod[x] = malloc( M * sizeof( unsigned char ) ); ...do stuff with stuff... for( x = 0; x < N; x++ )     free( twod[x] ); free( twod );```

Quzah.
• 02-05-2005
Salem
> And this alloates pointers to array of characters i.e. strings.
Not quite - all those parentheses matter, and translating them into words requires a good deal of precision.

If you have
Code:

`char *q = malloc( 10 * sizeof *q );`
q is a pointer to an array of 10 characters
It is the dynamically allocated equivalent of char q[10]

If you have
Code:

`char (*p)[20] = malloc( 10 * sizeof *p );`
p is a pointer to an array of 10 array-of-20-characters.
It is the dynamically allocated equivalent of char p[10][20]

If you have
Code:

`char **p = malloc( 10 * sizeof *p );`
p is a pointer to an array of 10 pointers to char.
It is the dynamically allocated equivalent of char *p[10]

However, by itself it is not able to store strings, so you need (as per Quzah's post) to allocate some more memory
Code:

`for ( i = 0 ; i < 10 ; i++ ) p[i] = malloc( 20 * sizeof *p[i] );`
Now you have the dynamically allocated equivalent of char p[10][20]

Now both types of p can be used to store up to 10 strings, each up to 20 characters in length. So for example, this is valid for both of them
Code:

`strcpy( p[0], "hello world" );`
So why choose char (*p)[20] over char **p ?
Well the pointer to an array is much easier to allocate (just one malloc call), but the downside is that every string will have space for 20 characters whether you want them or not.
The pointer to a pointer on the other hand is more complicated to allocate, but you get the freedom to allocate memory according to the length of each string stored.

If this is actual C code, then it is broken.
1. You don't cast malloc in C
2. There is no sizeof in the malloc call, so all you've managed to achieve with this call is the equivalent of a pointer to unsigned char array[1][MAD_BUFFER_MDLEN];
Essentially, you've added a dimension for no reason at all.
• 02-05-2005
bravetanveer
Thanks
hi salem,

Thanks for that detailed explanation of pointer and arrays.

I got some real good knowledge.

regards
bravetanveer