1 / 12

C Arrays

C Arrays. 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++ Vector = Array with Benefits.

zander
Télécharger la présentation

C Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C Arrays 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.

  2. C++ Vector = Array with Benefits 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.

  3. Key Benefit C++ Vector 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.

  4. Solution: Capacity and Size 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. Size Capacity

  5. Class Signature Default constructor Copy constructor Destructor Assignment operator

  6. Vector Template Declaration template<typename T> 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;

  7. Copy Constructor & Destructor // 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; } }

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

  9. Vector::InsertAt() 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; }

  10. Index Operator // 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]; }

  11. Assignment Operator (Deep Copy) Vector<T>& operator = (const Vector<T>& 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; }

  12. Homework #3 Take Vector class from Angel (given by Vector.h and Vector.cpp files) and convert it into a generic vector type; Retrofit your EmailChecker to use Vector class to store domains, e.g. your EmailSubdomain Subdomains[MAX_SUBDOMAINS]; Should be replaced with Vector<EmailSubdomain> 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);

More Related