Pointers and arrays in memory.
Memory is just a long string of bytes addressed by locations. Variables are represented by their addresses. Programs can also deal with addresses via pointers. Pointers contain the address of the data they point to. Arrays are blocks of contiguous memory that can be accessed using indexes that calculate the address from the starting point. Functions use stack frames to manage local variables that are destroyed when the function exits.
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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. Constructor vs. no Constructor
string * somePtr = new string("yaketyyak");
double * someOtherPtr = new double[4];
Is the string initialized?
Are the doubles initialized?
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. Allocating whole arrays at a time
Question for the class:
What are the differences between
char * line = new char[256];
char line2[256];
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. 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. 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