16+-+Copying+Arrays - 11/16/2009 Dynamic Arrays Creating...

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

View Full Document Right Arrow Icon
11/16/2009 1 Copying Arrays EECS 280 Programming and Introductory Data Structures 1 Dynamic Arrays Creating 2 c So far, the things we've been creating dynamically have had sizes that were known to the compiler. c For example, the amount of memory required to hold an int (or an IntSet ) is easy to compute. c However, one can also create objects whose sizes are not known to the compiler, by creating dynamic arrays . c The syntax for doing so is very closely related to the syntax for creating single elements. c Allocation works exactly as you'd expect: int *ia = new int[5]; creates an array of five integers in the heap, and stores a pointer to the first element of that array in ia . Dynamic Arrays Freeing 3 c Freeing an array works slightly different than freeing a single object: delete[] ia; c If you allocate an array-of-T , you absolutely must use the delete[] operator, and not the "plain" delete operator. c Conceptually, the array allocator and the singleton allocator are completely different: c Mixing them leads to undefined behavior. c This is because the language runtime system has to keep track of how large arrays are – since the compiler doesn't know, it has to be dynamic information. Dynamic Arrays Freeing 4 c When the new operator sees it is allocating an array, it stores the size of the array along with the array. c It does this by carving out space for the array, plus a bit extra: c The space before the array records the number of elements in the array, in this case, 5: c And a pointer to the beginning of the array is returned: array array 5 array 5 ia Dynamic Arrays Freeing 5 c Now, if you just delete ia ; the delete operator thinks it is only returning enough space for a single integer to the heap. c The delete[] operator knows to look "just before" the pointer, to see how many elements to return to the heap. array 5 ia array 5 ia Dynamic Arrays Building a new IntSet 6 c With this information, we can build a version of IntSet that allows the client to specify how large the capacity of the set should be. c The data elements will change slightly: class IntSet { int *elts; // pointer to dynamic array int sizeElts; // capacity of array int numElts; // current occupancy public: ... };
Background image of page 1

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

View Full DocumentRight Arrow Icon
11/16/2009 2 Dynamic Arrays Building a new IntSet 7 c With this information, we can build a version of IntSet that allows the client to specify how large the capacity of the set should be. c The data elements will change slightly: class IntSet { int *elts; // pointer to dynamic array int sizeElts; // capacity of array int numElts; // current occupancy public: ... }; Rather than hold an array explicitly, we have a pointer that will (eventually) point to a dynamically-created array. Dynamic Arrays
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/26/2010 for the course EECS 280 taught by Professor Noble during the Fall '08 term at University of Michigan.

Page1 / 6

16+-+Copying+Arrays - 11/16/2009 Dynamic Arrays Creating...

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

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