Arrays like vectors, but with less features
Allocate an array from the heap <ul><li>double * doubleArray = new double[4]; </li></ul><ul><li>remember the new function?...
Allocate an Array on the Heap <ul><li>when you do your allocation </li></ul><ul><li>double * doubleArray = new double[4]; ...
Allocate an Array on the Heap <ul><li>When you dereference </li></ul><ul><li>doubleArray[whatever] 0, 1, 2, or 3  </li></u...
New returns a pointer <ul><li>double * doubleArray = new double[4]; </li></ul><ul><li>the new function returns a pointer t...
How are arrays different than vectors? <ul><li>Arrays </li></ul><ul><li>no machinery </li></ul><ul><li>no overhead for mac...
You can't mix different pointers <ul><li>char * myCharArray = new char[256]; </li></ul><ul><li>int * myIntArray = new int[...
Dereference an Array <ul><li>You can dereference an array through the * syntax  </li></ul><ul><li>-or-  </li></ul><ul><li>...
Array Allocation: What's in there? <ul><li>When you allocate an array with new what is in there?  </li></ul><ul><li>When y...
Initialization of  Memory Allocated from the Heap <ul><li>Important!!!!!******* </li></ul><ul><li>Built-in types that have...
Upcoming SlideShare
Loading in...5
×

Arrays

253

Published on

Published in: Technology, Sports
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
253
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Arrays

  1. 1. Arrays like vectors, but with less features
  2. 2. Allocate an array from the heap <ul><li>double * doubleArray = new double[4]; </li></ul><ul><li>remember the new function? </li></ul><ul><li>This will allocate an array  </li></ul><ul><li>(like a vector, but less functions you can use.) </li></ul><ul><li>The array will be of four doubles, that you can reference with the [] syntax as before: </li></ul><ul><li>doubleArray[0] </li></ul><ul><li>doubleArray[1] </li></ul><ul><li>doubleArray[2] </li></ul><ul><li>doubleArray[3] </li></ul><ul><li>Can you reference doubleArray[4]?  </li></ul><ul><li>What is in all these elements? </li></ul>
  3. 3. Allocate an Array on the Heap <ul><li>when you do your allocation </li></ul><ul><li>double * doubleArray = new double[4]; </li></ul><ul><li>1) You invoke new, which gets memory from the heap </li></ul><ul><li>2) You get the constructor for double </li></ul><ul><li>3) You get four entries, all in a row </li></ul><ul><li>4) You get back a pointer to the first one in the variable doubleArray </li></ul>
  4. 4. Allocate an Array on the Heap <ul><li>When you dereference </li></ul><ul><li>doubleArray[whatever] 0, 1, 2, or 3  </li></ul><ul><li>(4 is not in bounds,  </li></ul><ul><li>neither are any numbers greater than 4 or less than 0) </li></ul><ul><li>The compiler does this little calculation: </li></ul><ul><li>(it doesn't care how many you asked for) </li></ul><ul><li>start from the pointer in doubleArray. Look at the sizeof the type of thing. Multiply the index by the sizeof and add it to the pointer. </li></ul><ul><li>doubleArray[0] is at the start of the array </li></ul><ul><li>doubleArray[1] is one double size unit away </li></ul><ul><li>doubleArray[2] is at doubleArray + two times sizeof(double) </li></ul><ul><li>doubleArray[3] is at doubleArray + three times sizeof(double) </li></ul><ul><li>doubleArray[4] is out of bounds. There might be other stuff there </li></ul>
  5. 5. New returns a pointer <ul><li>double * doubleArray = new double[4]; </li></ul><ul><li>the new function returns a pointer to whatever it just allocated. </li></ul><ul><li>Pointers always point to a type of thing. </li></ul><ul><li>So, easy question: </li></ul><ul><li>Why is this wrong: </li></ul><ul><li>char * myCharPtr = new double[4]; </li></ul><ul><li>Why is this wrong: </li></ul><ul><li>double myDouble = new double[4]; </li></ul>
  6. 6. How are arrays different than vectors? <ul><li>Arrays </li></ul><ul><li>no machinery </li></ul><ul><li>no overhead for machinery </li></ul><ul><li>no features </li></ul><ul><li>has no idea how big it is, you can't ask it.  </li></ul><ul><li>All you get back is a pointer </li></ul><ul><li>lets you step all over out-of-bounds stuff. </li></ul><ul><li>Vectors </li></ul><ul><li>a class, lots of functions included you pay for that machinery with overhead </li></ul><ul><li>lots of useful features </li></ul><ul><li>can tell you how big it is, among other things </li></ul><ul><li>you get back an object with functions </li></ul>
  7. 7. You can't mix different pointers <ul><li>char * myCharArray = new char[256]; </li></ul><ul><li>int * myIntArray = new int[64]; </li></ul><ul><li>The amount of memory allocated may be the same. </li></ul><ul><li>But if you walk down these arrays using [ ] as in </li></ul><ul><li>myIntArray[20] and myCharArray[20], the offset is totally different because the sizeof is totally different. </li></ul><ul><li>sizeof(int) is usually 4, sizeof(char) is usually 1 </li></ul><ul><li>Say myCharArray is xFFF000 and myIntArray is xFFF100. </li></ul><ul><li>At what address is myIntArray[20],  </li></ul><ul><li>and where is myCharArray[20]? </li></ul>
  8. 8. Dereference an Array <ul><li>You can dereference an array through the * syntax  </li></ul><ul><li>-or-  </li></ul><ul><li>the [ index ]  syntax,  </li></ul><ul><li>As we have been doing on the previous pages! </li></ul><ul><li>Remember we are counting from 0! </li></ul><ul><li>Question to class: What do you get when you do </li></ul><ul><li>double * myDoubleArray = new double[4]; </li></ul><ul><li>sizeof(myDoubleArray) </li></ul>
  9. 9. Array Allocation: What's in there? <ul><li>When you allocate an array with new what is in there?  </li></ul><ul><li>When you allocate an uninitialized pointer what is in there? </li></ul><ul><li>Thoughts from the class. </li></ul><ul><li>If you do this: </li></ul><ul><li>int * myIntPointer; </li></ul><ul><li>// not pointing anywhere yet </li></ul><ul><li>and then you do this: </li></ul><ul><li>*myIntPointer = 999; </li></ul><ul><li>What do you think will happen? </li></ul>
  10. 10. Initialization of  Memory Allocated from the Heap <ul><li>Important!!!!!******* </li></ul><ul><li>Built-in types that have an initialization, such as ints, chars,  doubles  </li></ul><ul><li>DO NOT GET INITIALIZED  </li></ul><ul><li>when you allocate them with new!!!!! </li></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×