18+-+Copying+Arrays - 9/1/10 Dynamic Arrays Creating So...

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

View Full Document Right Arrow Icon
9/1/10 1 Copying Arrays EECS 280 Programming and Introductory Data Structures 1 Dynamic Arrays Creating 2 So far, the things we've been creating dynamically have had sizes that were known to the compiler. For example, the amount of memory required to hold an int (or an IntSet ) is easy to compute. However, one can also create objects whose sizes are not known to the compiler, by creating dynamic arrays . The syntax for doing so is very closely related to the syntax for creating single elements. 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 Freeing an array works slightly different than freeing a single object: delete[] ia; If you allocate an array-of-T , you absolutely must use the delete[] operator, and not the "plain" delete operator. Conceptually, the array allocator and the singleton allocator are completely different: Mixing them leads to undefined behavior. 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 When the new operator sees it is allocating an array, it stores the size of the array along with the array. It does this by carving out space for the array, plus a bit extra: The space before the array records the number of elements in the array, in this case, 5: And a pointer to the beginning of the array is returned: array array 5 array 5 ia Dynamic Arrays Freeing 5 Now, if you just delete ia ; the delete operator thinks it is only returning enough space for a single integer to the heap. 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 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. 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
9/1/10 2 Dynamic Arrays Building a new IntSet 7 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. 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.

Page1 / 6

18+-+Copying+Arrays - 9/1/10 Dynamic Arrays Creating So...

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