General Talk on Pointers

15
General Talk on Pointers and memory

Transcript of General Talk on Pointers

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?