Arrays
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
525
On Slideshare
495
From Embeds
30
Number of Embeds
2

Actions

Shares
Downloads
2
Comments
0
Likes
0

Embeds 30

http://rcc-elena.blogspot.com 16
http://rcc-martinez-141.blogspot.com 14

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

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