general talk on pointers

47
General Talk on Pointers and memory

Upload: emartinezromero

Post on 22-Nov-2014

664 views

Category:

Technology


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: General Talk on Pointers

General Talk on Pointersand memory

Page 2: General Talk on Pointers

Ch. 17.3

section 17.3 

This is difficult reading. 

Try to read it with pencil and paper, to one side of your laptop or computer at RCC!

Page 3: General Talk on Pointers

memory in your computer

When you buy a computer, you know that you need to buy a certain amount of RAM, and that it's measured in (big) integers.

When you buy a hard drive, whether internal, external or flash,you also measure the size in (big) integers.

Your flash drive may be 1G. What exactly is that 1 G?

It's one gigabyte: a billion bytes.

Think of your computer's memory as a long chain of bytes.

Page 4: General Talk on Pointers

memory in your computer   

Thinking of that long stream of bytes:

Imagine that the bytes are numbered 

from 0 through 1G on that flash drive, 

or in RAM from 0 through 24 GB 

Page 5: General Talk on Pointers

Declare a variable

int myInt = 999;

Somewhere in memory, there is an int sized piece set aside

1) with the handle "myInt"2) with the contents 999

The computer has no idea of the variable name myInt.myInt is just a fiction that the compiler provides for you.

The computer represents that place as address #FFF780(in hexadecimal).

Memory is numbered. The addresses are the numbers.

Page 6: General Talk on Pointers

Addresses to Places in memory

You can refer to that variable as myInt, the handle that you assigned to it.

Or you can refer to that variable in a more abstract way, via its address in memory,

NEW SYNTAX:

   int * myIntPtr = &myInt;

what? What? WHAT?

Page 7: General Talk on Pointers

 int * myIntPtr = &myInt;

Let's dissect the pieces.

&myInt We have already seen similar in the stream example.& means "address of "

All variables have an address in memory.The & operator takes the address of a variable.

&myInt means, the address to where the myInt label refers.

Page 8: General Talk on Pointers

 int * myIntPtr = &myInt;

More pieces:

int * myIntPtr

This is a variable declaration, similar to the int declaration.

But it does not declare an int, it declares a pointer to an int.

myIntPtr is a variable that does not itself contain an int. It contains an address, where an int resides.

Page 9: General Talk on Pointers

 int * myIntPtr = &myInt;

Analogy:

You live in your house or apartment.You have moved from elsewhere, and have address forwarding. The post office has a catalog of where all forwarded mail should go.

You get a piece of mail, via your old post office.The post man sees your name, and looks up a reference to your new address.1) You are the number 17.2) Your house is the int myInt.3) The reference at the old post office is the pointer, myIntPtr.

Page 10: General Talk on Pointers

 int * myIntPtr = &myInt;

say that myInt is at address #FFF880 (or whevs)

myInt = 17;int * myIntPtr = &myInt;

Now the value 17 is in myInt, and the value FF880 is in myIntPtr.

Page 11: General Talk on Pointers

Dereferencing a Pointer

Doing what to a pointer now?Say that you are not aware of the myInt variable.Say that your code is only aware of the myIntPtr variable.

Remember:myInt = 17; int * myIntPtr = &myInt;

how do you get at that 17 via myIntPtr?

NEW SYNTAXint myOtherInt = *myIntPtr;"*variable name" is read as "the contents of variable name"

Page 12: General Talk on Pointers

Derefencing Examples

Dereference on the Left

int * ptr1;char * ptr2;

*ptr1 = 888;The contents of ptr1 gets 888

*ptr2 = 'c';The contents of ptr2 gets 'c'

Dereference on the Right

int * ptr1;char * ptr2;int anInt;char aChar;

anInt = *ptr1;anInt gets the contents of ptr1

aChar = *ptr2;aChar gets the contents of ptr2

Page 13: General Talk on Pointers

Pointers to a thing are not the same as the thing

So you can't mix and match.You will have errors around this issue. 

int * myIntPtr = &myInt;

int anotherInt = myIntPtr; // not legal

char * myCharPtr = &myChar;char anotherChar = myCharPtr;// again, not legal

Page 14: General Talk on Pointers

sizeof    

This little funtion (or operator) tells you how much space a given thing takes up in memory.

You can find out interesting details about the architecture of your machine using sizeof.

Page 15: General Talk on Pointers

sizeof

Guesses (and thoughts) from the students please:

How big is an int? sizeof(int)How big is a char? sizeof(char)How big is a pointer to an int? sizeof(int*)How big is a pointer to a char? sizeof(char*)How big is a literal? sizeof('a')another literal? sizeof(999)another literal? sizeof("blablblalba")How big is a boolean?How big is a double?How big is an empty vector?How big is a vector with entries?

Page 16: General Talk on Pointers

The Null Pointer   

How do you initialize a pointer?You set it to null, which is 0;int * myIntPtr = 0;

Sometimes people test a pointer before using it:if (myIntPtr){   // do some code if the pointer contains a value other than 0}else{// do some other code for the case when myIntPtr is empty}

Page 17: General Talk on Pointers

The Null Pointer

It's also valid to test for the null pointer like this:

if (myIntPtr != 0){// there is a value in there, do something}else{// there's not a value in there, do something else}

Page 18: General Talk on Pointers

Testing for the Null Pointer

It's not 100% fool-proof to test for the null pointer.

1) There could be junk in the variable. When you test it, it's not 0, but it's not a valid pointer value either.

This happens when variables are not initialized or when they are mistakenly overwritten.

2) The pointer could be pointing to an object that has been deleted, or garbage collected.

WHAT? ARE? YOU? TALKING? ABOUT? The Heap.More next lecture. Difficult topics ahead!

Page 19: General Talk on Pointers

What happens when you declare a variable?

When you declare some variables, whether in or outside of main,the compiler knows what space you will need,at compile time.

int oneInt, twoInt, threeInt;int main(){char oneChar, twoChar, threeChar;...}How much space do we need for variables? Please calculate.

Page 20: General Talk on Pointers

Industrial Applications

Say you write a program to manage bank records.Do you know ahead of time how many records you will need?

What can you do to get around this problem? 1) Declare an arbitrary upper limit. If the bank needs more records than that, well, tough nougies.

2) Create an insanely large number of records to start. If you don't use that space for records, use it up anyway "just in case".

Easy question: Can you point out the problem with these solutions?

Page 21: General Talk on Pointers

Solution: Dynamic allocation of Memory

Dynamic in this context means:marked by usually continuous and productive activity or change Allocation in this context means:the act of distributing by allotting or apportioning; distribution according to a plan; 

Meaning what? That the amount of space your program uses is not determined at compile time, but at run time.

Please define again the difference between compile time and run time.

Page 22: General Talk on Pointers

Dynamic Allocation of Memory

The system provides you with mechanisms to conditionally ask for more memory as you need it and use it.

You have already used some of these mechanisms.

What do you think happens when you type:

anyVector.push_back(anyValue)?

Page 23: General Talk on Pointers

What happens when you create a new object?

You have seen problems where we create new objects. You yourself programmed them in the library-book assignment.

Can you think of a case of dynamic allocation of memory from that project?

Page 24: General Talk on Pointers

A little bit of ArchitectureMemory is just a long list of bytes, addressable by pointers.But most operating systems divide up that memory according to some orderly scheme. Here's one such scheme.

1) Some memory corresponds to the code that you wrote, the instructions. These are the functions and main.

2) Some is for the variables you have declared outside of main.

3) Some is for the variables you have declared in the functions, including main.

4)The rest of the memory is free for other uses

Page 25: General Talk on Pointers

A little bit of Architecture

The four pieces are called: 

code

static

the stack

the heap

Page 26: General Talk on Pointers

How to allocate the code portion?

You do this when you write your code.

But remember it's translated into machine language.

Your files are called source files. Your comments won't make it into the code!

The code portion is fixed at the outset.

Page 27: General Talk on Pointers

How to allocate the static portion?

Again, you do this when you write your code.  These are all the variables that you define outside of main, and the variables that you define in your .h files.

These are called static, as a group, because they will not change in size, although they may change in contents.

These are called the global variables, because they are visible to all the functions in a file. 

With some exceptions. Remember how we declared them in different places in a file and affected visibility. 

Page 28: General Talk on Pointers

How to allocate the stack portion?

When you write your code, you tell the compiler what variables a particular function (including main) wants to use.

When your function is invoked (run), that's when space for those variables is set aside, on the stack.

Page 29: General Talk on Pointers

The Stack: some more detail

Let's consider the example of the function that had a vector as a parameter, and that swapped the words in that vector. When we were calling the function with the vector by value, our changes to that vector were no longer in effect when we returned from the function.  Why did this happen? Because when you call by value, you get a copy of the parameters in the destination function, not the originals.

What do I mean by "in" the destination function? Where is that?

Page 30: General Talk on Pointers

Why is it called a stack?

It's like a stack of dishes. You should take a dish off the top of the stack,and place a dish on the top of the stack.

Your program starts in tmain, and it calls a function,process_file.

Process_file calls a function, process_line.

process_lineprocess_file_tmain

Page 31: General Talk on Pointers

Stack example: vectors

You have been using the vector class for weeks now.

Many have been using the push_back function. The opposite of the push_back function is the pop_back function.

What does the word back refer to in both cases? 

Knowing what push means, what do you thinkpop must mean?

Thoughts from the class.

Page 32: General Talk on Pointers

One stack operation on a vector

You can imagine something like this happening to your vector.

Page 33: General Talk on Pointers

Functions on a StackNow apply that same concept to your functions.The computer keeps track of them all in a stack.

The bottom of the stack is an object that represents tmain. It has a stack "frame". The stack frame is like one entry in a vector. In the stack frame you have room for all the variables declared in tmain.

Say tmain calls process_file.  process_file gets a stack frame that sits on top of tmain's stack frame, just like in the stack of dishes, or in a vector. That frame has process_file's variables. Those are the only local variables visible to the stack frame. 

Local variables are those that are in the current stack frame.Only the variables in the current frame are visible! Other than global variables.

Page 34: General Talk on Pointers

Local Variables in the Stack Frame

Think of the functions all piled up in a stack, in the order in which they are called.

Only local variables are visible:They are the ones in the current stack frame. The "top".

If you put those two thoughts together, you can see why in last week's examples with functions and call by value, no permanent changes are made to the parameters:

Because the parameters are copied into the new stack frame!

Page 35: General Talk on Pointers

Stack of Dishes Analogy

Look down at a stack of dishes, pilled up high,each with some food on the plate.

You can only see the food on the top plate.The food on the other plates  is not accessible to you.

That top dish is like the current stack frame.The variables in a function are like the food on the plate.

You can only see and access the top dish!

Page 36: General Talk on Pointers

At the end of a Function

Each function has its own sandbox. Variables are copied into its sandbox. 

When the function is done, the stack is popped. Take away that plate and wash it.

All the info from that sandbox is gone. All that food is tossed.

The only thing that comes back is the return code, if you have one. 

The current stack frame is the old previous. That's the dish that was underneath, with the food on that plate. That function resumes where it left off.

Page 37: General Talk on Pointers

The function's stack frame is goneAll the variables from that ended frame go away. The previous stack frame, the one for the function that made the initial call,is the new current stack frame.

But remember, what do we do if we either

1) Don't want to copy everything into a new sandbox

-and/or-

2) Want some permanent change?

We use call by reference: POINTERS

Page 38: General Talk on Pointers

call by Reference: Pointers

If we use call by reference as we did in last week's examples , then a pointer gets copied to the local stack framewhen a function is called.

The function, operating on a pointer, makes changes to the contents at the pointer. That may be elsewhere in the stack or somewhere else altogether.

When the stack frame is popped, the sandbox with the copy of the pointer goes away.

Changes made to the contents at the pointer are still in effect, if it wasn't an address in that stack frame!

Page 39: General Talk on Pointers

Code, Static and StackSo far we have discussed three different ways that the computer will allocate memory to your program.Let's return to a big industrial case.

A bank has many many functions that operate on its many records to maintain all of its many transactions.

These many records cannot be:copied among functions (too much space, too slow)

changed over and over in each function, since call by argument changes are local (changes must be permanent)

declared in static ahead of time (can't predict how many)

Page 40: General Talk on Pointers

Where do those records come from?

What about your homework?

You read in a file, but you don't know ahead of time how many records there will be.

You push them onto a vector as you read them.

Last week we saw that the vector's machinery occupied 20 bytes, regardless of entries. 

Where are those entries coming from when you usepush_back?

Page 41: General Talk on Pointers

The HeapIsn't the answer obvious, since we haven't talked about it yet?

Page 42: General Talk on Pointers

The Heap

When you use the push back function,

when you use a constructor function in a class,

under the covers, without you explicitly asking for it,

you have invoked the new function, which takes memory from the heap.

New Syntax: "new"

eg:LocalError * myError = new LocalError("any message");

Page 43: General Talk on Pointers

New

Remember the LocalError class that we created weeks ago?We can create an object of the LocalError type by using new.New will return a pointer to an object of type LocalError.

LocalError * myError = new LocalError("any message");

This syntax is not better or worse than the old syntax, it just illustrates the idea that the memory is new, it was not allocated when the program started.

And that new memory comes off the heap.

Page 44: General Talk on Pointers

Pieces of the Syntax

LocalError * myError = new LocalError("blablbla");

(LocalError *) together, are the type of the variable. This variable is a pointer to type LocalError.

myError is the name of the variable.

new means create a new variable from the heap.

LocalError("blabla"); remember that the LocalError class had a constructor that took a string as its parameter.

That's why it's called a constructor. It is a function that builds a new whatever from the heap when it is called.

Page 45: General Talk on Pointers

It's on the Heap. So What?Since it's on the Heap and not on the stack, when stack frames change and stack frame variables go away,variables on the heap are still available for use.

How does your program get to access them? Through POINTERS!

Page 46: General Talk on Pointers

More Images: the Stack

 

Page 47: General Talk on Pointers

More Images: Heap and Stack