Upload
emartinezromero
View
363
Download
1
Embed Size (px)
DESCRIPTION
Citation preview
General Talk on Pointersand memory
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!
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.
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
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.
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?
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.
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.
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.
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.
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"
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
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
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.
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?
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}
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}
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?More next lecture. Difficult topics ahead!