More Pointers and Arrays


Published on

Published in: Technology
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

More Pointers and Arrays

  1. 1. Pointers and Arrays until you want to vomit
  2. 2. Memory Is just a long long string of bytes When you create a variable and give it a name: that's just an artifact of the compiler talking to YOU when it talks to the computer, the only thing it talks about are addresses.
  3. 3. Be an Address Dealer Your program can also deal in addresses, if it is appropriate The whole language of Java thinks it's not. Other languages, (C, C++) think it is. That means, there are good reasons on both sides.
  4. 4. Syntax Review: Abstract Example (type *) myPtr; This declares an empty variable, of type pointer to type. When populated, it will contain an address: myPtr = &SomeTypeOfThing; myPtr will contain, FFF000, for example. at FFF000, there will be an object some "type".
  5. 5. Syntax Review: Doubles and Chars double myDouble, anotherDouble; myDouble = 9.9; (double *) myDoublePtr; myDoublePtr = &myDouble; read the contents of myDouble via the pointer: anotherDouble = *myDoublePtr; anotherDouble = 4.4; write the contents of myDouble via the pointer: *myDoublePtr = anotherDouble;
  6. 6. Syntax Review: You can't do that (double *) myDoublePtr; double myDouble; so far, so good ... myDoublePtr = 8.8; //NO myDouble = myDoublePtr; // also no myDouble = 22; // so far, so good *myDoublePtr = 22; // ?? Could be trouble!!!! Why? (Hard ?)
  7. 7. Pointers & Addresses: Why bother? Because of the way the operating system has decided to divvy up memory. Four types of memory: code static related to functions (The Stack) related to free memory (The Heap)
  8. 8. Compiler: Calculates how much space you need. How much space does the code itself occupy? Code How about the variables outside of your main, or outside of any function at all? Global How big is the stack frame for each of your functions? And that's all the compiler cares about. All the rest, the news and the push_backs etc: All the rest happens at RUN TIME
  9. 9. Functions, yet Again Remember that your functions, when the are running, are represented by a stack frame. The active stack frames pile up, one on top of the other, in reverse order of invocation. The latest is the greatest! When it's done, it POPS off the stack. It takes its variables with it.
  10. 10. Function Parameters When you pass parameters to a function, those are local to the function. They are copies (By Default) of the originals in the calling functions. That's why changes aren't permanent. When the stack frame POPS all those changes go away with the stack frame.
  11. 11. Call By Reference: Passing around Notes When you call by reference (not the default, you need to declare the function with the & parameters) Then you are passing an address of a parameter, not a copy of the thing itself. It's like passing notes in grade school "pssst go look in the closet" Where the answer to the test will be on a piece of paper. etc.
  12. 12. Compiler Space Small, Real Space Big Imagine a program that is short, and performs some simple calculation on lots and lots and lots of records: Big Huge Investment Bank BHB, inc. wants to keep track of all the ticker symbols in the US plus all the ones in Euro countries, plus Japan, plus Canada, plus Australia etc etc It will just update stock prices as they come in. If they stocks are in memory, they are easy to access quickly but the calculation itself is simple: the heap.
  13. 13. New: Just like in your current HW new will work on any valid type new will return a pointer to that type: catch it in a point to type variable new will get space on the heap big enough to store 1 or more of your preferred type. If that type has a constructor, then new will execute the constructor for you.
  14. 14. Constructor vs. no Constructor string * somePtr = new string("yaketyyak"); double * someOtherPtr = new double[4]; Is the string initialized? Are the doubles initialized?
  15. 15. Constructor vs. no Constructor string * somePtr = new string("yaketyyak"); double * someOtherPtr = new double[4]; Is the string initialized? Are the doubles initialized? How about this familiar one: char * inputLine = new char[256];
  16. 16. Allocating whole arrays at a time Question for the class: What are the differences between char * line = new char[256]; char line2[256];
  17. 17. Allocating Whole Arrays at a Time char line2[256]; this will come off the stack or out of static (global) char * line = new char[256]; it's on the heap - not allocated at compile time
  18. 18. Arrays The whole array is referred to by a common name like line on the previous slide The memory is allocated in one long block the lowest address is the first element the highest address is the last element it's divided up into multiples of whatever the sizeof(type) is
  19. 19. Counting from 0 The lowest address is the start of the array it's chopped up into chunks the sizeof the type therefore: the address of an element at some index equals is calculated by this formula: address = start address + ( index multiplied by sizeof(type)) That's why we always count from 0 CUZ: it always comes back to an address in memory
  20. 20. Two Dimensional Arrays
  21. 21. Credit where credit is due Toothpaste for Dinner
  22. 22. Syntax For a multi dimensional array char matrix[3][3]; like on the back ground of this slide!
  23. 23. [0][0] [0][1] [0][2] [1][0] [1][1] [1][2] [2][0] [2][1] [2][2]
  24. 24. a 2 X 3 matrix A B C D E F
  25. 25. matrix[0][0] contains A A B C D E F
  26. 26. matrix[1][1] contains E A B C D E F
  27. 27. What is the matrix short hand for the cell containing D? A B C D E F
  28. 28. Array Syntax one more piece of syntax: int digits[4] = { 34, 543, 654, 4354};