110 likes | 124 Views
Delve into the convenience and maintenance issues of bicycles and programming arrays versus vectors, highlighting the benefits of the latter. Explore the ease of resizing, error handling, and memory management for vectors compared to arrays.
E N D
What was originally bought as a form of convenience, soon became an inconvenience due to all the maintenance it required. Each week, there was always something wrong. From punctured tires, to broken spokes, to rusty chains needing to be replaced... It wasn’t until I came to Texas A&M that I chose to buy one, to get around campus quicker. …And it certainly did that. But not without problems. As a teenager, I didn’t ride bicycles often.
It intrigued me because the bike came with several features, including airless tires and a chainless frame, that essentially made maintenance a non-issue for the owner. Basically, it offered everything my original bike had to offer, but without the added strain of maintaining it. Hence why when I heard of Maroon Bikes, I was intrigued.
It’s this same reasoning that makes Vectors popular over arrays. Arrays have many uses, but they come with maintenance issues as well.
For instance, suppose I am using an array to store a Student’s grades. Now suppose I need to add a new grade for the most recent assignment. As a result, I must first allocate a new array with the size that I need. This array can’t be made larger to accommodate that grade. Next, I must copy all the original values to this new array. Only then can I add the new value. Once I’ve done all that, the last step is deleting the old array. 94 97 79 84 92 71 100 95 94 97 79 84 92 71 100 95 [0] [1] [2] [3] [4] [5] [6] [7] 98 [0] [1] [2] [3] [4] [5] [6] [7] [8]
Just as arrays cannot be made larger, they also cannot be made smaller. A new one must be allocated. For example, suppose the fifth grade was a test grade, and the student exercised her privilege to drop it. A similar process occurs when we have to remove an element as well. [0] [1] [2] [3] [4] [5] [6] [7] With vectors, however, we need only call push_back()or erase(). All other steps are then handled behind the scenes. In general, we must go through this process whenever we need to change the size of the array. 94 94 97 97 79 79 84 84 92 71 71 100 100 95 95 98 98 [0] [1] [2] [3] [4] [5] [6] [7] [8]
If you don’t keep a close eye on them, they won’t mind crossing the street. Another problem with arrays is they’re an awful lot like infants.
for( inti = 0; i < 5; ++i ) { ++p; } for( inti = 0; i < 15; ++i ) { ++p; } This is allowed by the compiler, even though it took the pointer out of range. …And we can go even further. To illustrate what I mean, suppose we allocate an array… Recall that the pointer always points to the first element initially. But we can move it… 0 0 0 0 0 int* p = new int[ 5 ]; p
0 0 0 0 0 Even out-of-bounds indicesare allowed. Negative ones as well. p[ 1000 ] = 100; p[ -1000 ] = 100; p p
Vectors do not share this problem. They throw an exception whenever we enter an index that is out-of-bounds. They also provide a size() function to help us remember what we have available.
The last maintenance issue is that arrays must be returned when we are done with them. This is unlike regular objects, who automatically return their memory once they go out of scope. Once they go out of scope, Vectors call their destructor, ~Vector(), which returns all of its memory. Array memory must be returned manually, or it won’t be available for other uses. This is done using the delete[] operator. Vectors, fortunately, do this automatically.
So in summary, Vectors offer the same usefulness that arrays offer, but with several additional features. Three of them being: • All re-allocation and de-allocation being handled behind the scenes • Protection from going outside or range • Returning the memory when we’re done with it.