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

Info icon This preview shows pages 1–7. 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
Image of page 1

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

View Full Document Right 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
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
Image of page 3

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

View Full Document Right 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
Image of page 4
1) Default constructor 2) Copy constructor 3) Destructor 4) Assignment operator Class Signature Class Signature
Image of page 5

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

View Full Document Right 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
Image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern