12DynamicArraysSmall - items you want in the array s Second...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
1 CMSC 106 Lecture Set #12 – Dynamically Allocated Arrays Set Started: Wednesday, November 30, 2011 Pointers and Arrays s Name of an Array = Address of 0 th element s Can use pointers to indicate other elements of the array s Can use pointer arithmetic to traverse an array s When an array is passed as the argument to a function – only the address indicated is passed Statically allocated array s char name[] = “Jandelyn”; s int sizes[5] = {4,6,2,4,8}; s double scores[10]; s Name of array is a constant pointer scores++; name+=2; *(name+2) = ‘z’;
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Filling statically allocated array s Size of array must be known at COMPILE time s Always stays the same size (can use only a portion of the allocated space if you wish) s Must make sure you stay “in bounds” Dynamically allocating space s calloc s First argument must indicate the number of
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: items you want in the array s Second argument must indicate the size of one of those array items s Allocates a single sequential and contiguous block of memory of the size needed s Returns NULL if the request can’t be fulfilled s Returns the Address of that space if it can be Rules s Space is allocated from a place called the heap s Once allocated, the size is fixed (can’t change size and remain at that location in memory) s The space is sequential and contiguous so can still use array index or pointer method (exactly the same as a statically allocated array) s The name of the Array is NOT CONSTANT – but be careful not to lose the location of the beginning of the array s Once allocated and you are done using it, the space must be freed...
View Full Document

This note was uploaded on 01/12/2012 for the course CMSC 106 taught by Professor Staff during the Fall '07 term at Maryland.

Page1 / 2

12DynamicArraysSmall - items you want in the array s Second...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online