[LCP]Re: 2D arrays:dynamic allocation and freeing

vivek ep vivek_ep at yahoo.com
Sun Feb 6 15:38:02 UTC 2005


 hi,


>The way to dynamically allocate a 2D array is :
>int a(*)[5];

should be 'int (*a)[5];'
try 'cdecl' command in linux to verify declarations. 

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

if you want to have a 5*8 array, allocation should be
 a=(int (*)[5])malloc(sizeof (int)*40);

now "a" will be point to an 8 element array, whose
elements are 5 elment array of 'int'.

Thus when you are trying acess a[1][2], u will get 3rd
'int' in 2nd element of array pointed by a.
In another way a[1][2] can also be accessed by 
*((int *)a+7).



>2.
>Is my way of freeing ok

In this case free(a) will do.

vivek  



__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam
protection around 
http://mail.yahoo.com 
--0-2021490201-1107277796=:43222
Content-Type: text/html; charset=us-ascii

<DIV>
<DIV>hi all,<BR><BR>The way to dynamically allocate a
2D array is 
:<BR><BR><BR>int a(*)[5];<BR><BR>a=(int 
(*)[5])malloc(sizeof(int)*5);<BR><BR><BR>This will
create array of pointer to arrays.<BR><BR>We can
access 
elements as a 2D array<BR>a[1][1] with access
particular 
element.<BR><BR><BR><BR>Question:<BR>1.<BR>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.<BR><BR>2.<BR>Is my way of freeing 
ok<BR><BR>for(i=0;i&lt;5;i++)<BR>free(a[i]);<BR><BR>Because
it will free the pointers , what about the 
elements associated with those pointers.
<DIV>hi all,<BR><BR>The way to dynamically allocate a
2D array is 
:<BR><BR><BR>int a(*)[5];<BR><BR>a=(int 
(*)[5])malloc(sizeof(int)*5);<BR><BR><BR>This will
create array of pointer to arrays.<BR><BR>We can
access 
elements as a 2D array<BR>a[1][1] with access
particular 
element.<BR><BR><BR><BR>Question:<BR>1.<BR>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.<BR><BR>2.<BR>Is my way of freeing 
ok<BR><BR>for(i=0;i&lt;5;i++)<BR>free(a[i]);<BR><BR>Because
it will free the pointers , what about the 
elements associated with those pointers.</DIV><!-- /
message --></DIV><!-- / 
message 
--></DIV><p>__________________________________________________<br>Do
You Yahoo!?<br>Tired of spam?  Yahoo! Mail has the
best spam 
protection around <br>http://mail.yahoo.com 
--0-2021490201-1107277796=:43222--



--__--__--

_______________________________________________
linuxCprogramming mailing list
linuxCprogramming at lists.linux.org.au
http://lists.linux.org.au/listinfo/linuxcprogramming


End of linuxCprogramming Digest

Delete Reply Forward Spam Move...
Previous | Next | Back to Messages Save Message Text
Check Mail Compose Compose Text Msg 	
	Search Mail Search the Web
Move Options

    * [New Folder]

Forward Options

    * As Inline Text
    * As Attachment

Reply Options

    * Reply To Sender
    * Reply To Everyone

Mail Shortcuts

    * Check Mail Ctrl++C
    * Compose Ctrl++P

    * Folders Ctrl++F
    * Advanced Search Ctrl++S

    * Options
    * Help Ctrl++H

Address Book Shortcuts

    * Add Contact
    * Add Category
    * Add List

    * View Contacts
    * View Lists

    * Quickbuilder
    * Import Contacts
    * Synchronise

    * Addresses Options
    * Addresses Help

Calendar Shortcuts

    * Add Event
    * Add Task
    * Add Birthday

    * Day
    * Week
    * Month
    * Year

    * Event List
    * Reminders
    * Tasks

    * Sharing
    * Synchronise

    * Calendar Options
    * Calendar Help

Notepad Shortcuts

    * Add Note
    * Add Folder

    * View Notes

    * Notepad Options
    * Notepad Help

Advanced Search

    * Advanced Search


Copyright © 1994-2005 Yahoo! Inc. All rights reserved.
Terms of Service - Guidelines

NOTICE: We collect personal information on this site.

To learn more about how we use your information, see
our Privacy Policy



	
	
		
___________________________________________________________ 
ALL-NEW Yahoo! Messenger - all new features - even more fun! http://uk.messenger.yahoo.com




More information about the linuxCprogramming mailing list