Presentation is loading. Please wait.

Presentation is loading. Please wait.

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);

Similar presentations


Presentation on theme: "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);"— Presentation transcript:

1 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

2 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

3 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? The insert method can use the new operator to allocate a new buffer and delete operator to delete the old buffer. Potential Problem: new-ing and delete-ing every time we insert or remove an array element is very inefficient. Key Benefit C++ Vector

4 Size Capacity To avoid frequent newing and deleting to increase array size Vector class allocates more memory than it currently needs. That is Vector’s 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

5 1)Default constructor 2)Copy constructor 3)Destructor 4)Assignment operator Class Signature

6 template class Vector { public: // Default constructor Vector() { Size = Capacity = 0; Buffer = NULL; } Vector(int size, int initialCapacity = 0) { Size = size; Capacity = initialCapacity > size ? initialCapacity : size; Buffer = new T[Capacity]; } private: T* Buffer; int Size, Capacity; Vector Template Declaration

7 // Copy constructor Vector(const Vector& aVector) { Size = Capacity = 0; Buffer = NULL; // Do copy via assignment operator *this = aVector; } // Destructor ~Vector() { if ( Buffer ) { delete Buffer; Buffer = NULL; Size = Capacity = 0; } Copy Constructor & Destructor

8 int GetSize() const { return Size; } bool IsValid() const { return Buffer != NULL; } const T* GetBuffer() const { return Buffer; } Vector const Methods

9 void InsertAt(int index, const T value) { if ( index > Size || index < 0 ) throw "Vector::InserAt() index out of range"; // Exceeding current capacity? BoostCapacity(1); // Make room for the new intacter MoveBy(index, 1); Size++; // Set the value Buffer[index] = value; }Vector::InsertAt()

10 // Index operator int& operator [](int index) const { if ( !IsValid() ) throw "Vector::Buffer is not initialized"; if ( index > Size || index < 0 ) throw "Vector[] index out of range"; return Buffer[index]; } Index Operator

11 Vector & operator = (const Vector & aVector) { int newSize = aVector.GetSize(); // Make sure the new data fits if ( newSize > Capacity ) BoostCapacity(newSize - Size); // Append new text at the end Copy(aVector.GetBuffer(), Buffer, newSize); Size = aVector.GetSize(); return* this; } Assignment Operator (Deep Copy)

12 1)Take Vector class from Angel (given by Vector.h and Vector.cpp files) and convert it into a generic vector type; 2)Retrofit your EmailChecker to use Vector class to store domains, e.g. your EmailSubdomain Subdomains[MAX_SUBDOMAINS]; Should be replaced with Vector Subdomains; This means when you parse Subdomains you will be adding elements to Vector class. So you may define Vector::Add() method to insert elements at position that is equal to Size, e.g. Add(T newElement) InsertAt(Size, newElement); Homework #3


Download ppt "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);"

Similar presentations


Ads by Google