[LCP]2D arrays:dynamic allocation and freeing

Pankaj Gode godepankaj at yahoo.com
Wed Feb 2 02:58:02 UTC 2005


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.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.



__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.linux.org.au/pipermail/linuxcprogramming/attachments/20050202/711521ac/attachment.htm 


More information about the linuxCprogramming mailing list