8-Vector - C Arrays C arrays are limited they are...

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

View Full Document Right Arrow Icon
C arrays are limited: - they are represented by pointers (which may or may not be valid); - Indexes not checked (which means you can overrun your array); - Arrays cannot grow or shrink to accommodate more or less data. C Arrays C Arrays
Background image of page 1

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

View Full DocumentRight Arrow Icon
In C++ you can define a Vector class that would work just as array does but would not suffer from its shortcomings. How? You make the class that: - manages its own memory dynamically (via new); - grows as necessary; - checks the pointer value and index bounds; - overloads [ ] index operator to provide element access. C++ Vector = Array with Benefits C++ Vector = Array with Benefits
Background image of page 2
C arrays are static and cannot grow… But you can define C++ Vector class to grow as necessary such that you could insert new elements into array! How? Kind of like in DynamicString::InsertAt() method using new to allocate new array and delete to delete old one, but not quite in the same way. Why? Because newing and deleting every time you need to add a single element is grossly inefficient. Key Benefit C++ Vector Key Benefit C++ Vector
Background image of page 3

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

View Full DocumentRight Arrow Icon
Size Capacity To avoid frequent newing and deleting to increase array size Vector class allocates more memory than it currently needs. That is Vector Capacity . In the same time Size designates the number of used array elements (Size <= Capacity). When a new element needs to be inserted and Size < Capacity no memory re-allocation is required. However, when Size = Capacity re-allocation is inevitable and Capacity is boosted by some value > 1. Solution: Capacity and Size Solution: Capacity and Size
Background image of page 4
1) Default constructor 2) Copy constructor 3) Destructor 4) Assignment operator Class Signature Class Signature
Background image of page 5

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

View Full DocumentRight Arrow Icon
class Vector { private: int* Buffer; int Size, Capacity; public: // Default constructor Vector() { Size = Capacity = 0; Buffer = NULL; } Vector(int size, int initialCapacity = 0) { Size = size; Capacity = initialCapacity > size ? initialCapacity : size; Buffer = new int[Capacity]; } Vector Declaration Vector Declaration
Background image of page 6
// Copy constructor { Size = Capacity = 0; Buffer = NULL;
Background image of page 7

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

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/18/2010 for the course CSE 122 taught by Professor Yoxheimer,ericch during the Spring '07 term at Penn State.

Page1 / 25

8-Vector - C Arrays C arrays are limited they are...

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

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