Upcoming SlideShare
×

# General Talk on Pointers

315 views

Published on

Published in: Technology, Education
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
Your message goes here
• Be the first to comment

• Be the first to like this

Views
Total views
315
On SlideShare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
4
0
Likes
0
Embeds 0
No embeds

No notes for slide

### General Talk on Pointers

1. 1. General Talk on Pointers and memory
2. 2. Ch. 17.3 <ul><li>section 17.3  </li></ul><ul><li>This is difficult reading.  </li></ul><ul><li>Try to read it with pencil and paper,  to one side of your laptop or computer at RCC! </li></ul>
3. 3. memory in your computer <ul><li>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. </li></ul><ul><li>When you buy a hard drive, whether internal, external or flash, </li></ul><ul><li>you also measure the size in (big) integers. </li></ul><ul><li>Your flash drive may be 1G. What exactly is that 1 G? </li></ul><ul><li>It's one gigabyte: a billion bytes. </li></ul><ul><li>Think of your computer's memory as a long chain of bytes. </li></ul>
4. 4. memory in your computer    <ul><li>Thinking of that long stream of bytes: </li></ul><ul><li>Imagine that the bytes are numbered  </li></ul><ul><li>from 0 through 1G on that flash drive,  </li></ul><ul><li>or in RAM from 0 through 24 GB  </li></ul>
5. 5. Declare a variable <ul><li>int myInt = 999; </li></ul><ul><li>Somewhere in memory, there is an int sized piece set aside </li></ul><ul><li>1) with the handle &quot;myInt&quot; </li></ul><ul><li>2) with the contents 999 </li></ul><ul><li>The computer has no idea of the variable name myInt. </li></ul><ul><li>myInt is just a fiction that the compiler provides for you. </li></ul><ul><li>The computer represents that place as address #FFF780 </li></ul><ul><li>(in hexadecimal). </li></ul><ul><li>Memory is numbered. The addresses are the numbers. </li></ul>
6. 6. Addresses to Places in memory <ul><li>You can refer to that variable as myInt, the handle that you assigned to it. </li></ul><ul><li>Or you can refer to that variable in a more abstract way,  </li></ul><ul><li>via its address in memory, </li></ul><ul><li>NEW SYNTAX: </li></ul><ul><li>   int * myIntPtr = & myInt; </li></ul><ul><li>what? What? WHAT? </li></ul>
7. 7.   int  *  myIntPtr =  & myInt; <ul><li>Let's dissect the pieces. </li></ul><ul><li>& myInt </li></ul><ul><li>  </li></ul><ul><li>We have already seen similar in the stream example. </li></ul><ul><li>& means &quot;address of &quot; </li></ul><ul><li>All variables have an address in memory. </li></ul><ul><li>The & operator takes the address of a variable. </li></ul><ul><li>&myInt means, the address to where the myInt label refers. </li></ul>
8. 8.   int  *  myIntPtr =  & myInt; <ul><li>More pieces: </li></ul><ul><li>int  *  myIntPtr </li></ul><ul><li>This is a variable declaration, similar to the int declaration. </li></ul><ul><li>But it does not declare an int, it declares a pointer to an int. </li></ul><ul><li>myIntPtr is a variable that does not itself contain an int. It contains an address, where an int resides. </li></ul>
9. 9.   int  *  myIntPtr =  & myInt; <ul><li>Analogy: </li></ul><ul><li>You live in your house or apartment. </li></ul><ul><li>You have moved from elsewhere, and have address forwarding.  </li></ul><ul><li>The post office has a catalog of where all forwarded mail should go. </li></ul><ul><li>You get a piece of mail, via your old post office. </li></ul><ul><li>The post man sees your name, and looks up a reference to your new address. </li></ul><ul><li>1) You are the number 17. </li></ul><ul><li>2) Your house is the int myInt. </li></ul><ul><li>3) The reference at the old post office is the pointer, myIntPtr. </li></ul>
10. 10.   int  *  myIntPtr =  & myInt; <ul><li>say that myInt is at address #FFF880 (or whevs) </li></ul><ul><li>myInt = 17; </li></ul><ul><li>int  *  myIntPtr =  & myInt; </li></ul><ul><li>Now the value 17 is in myInt,  </li></ul><ul><li>and the value FF880 is in myIntPtr. </li></ul>
11. 11. Dereferencing a Pointer <ul><li>Doing what to a pointer now? </li></ul><ul><li>Say that you are not aware of the myInt variable. </li></ul><ul><li>Say that your code is only aware of the myIntPtr variable. </li></ul><ul><li>Remember: </li></ul><ul><li>myInt = 17; </li></ul><ul><li>  int  *  myIntPtr =  & myInt; </li></ul><ul><li>how do you get at that 17 via myIntPtr? </li></ul><ul><li>NEW SYNTAX </li></ul><ul><li>int myOtherInt = * myIntPtr; </li></ul><ul><li>&quot;*variable name&quot; is read as &quot;the contents of variable name&quot; </li></ul>
12. 12. Derefencing Examples <ul><li>Dereference on the Left </li></ul><ul><li>int * ptr1; </li></ul><ul><li>char * ptr2; </li></ul><ul><li>*ptr1 = 888; </li></ul><ul><li>The contents of ptr1 gets 888 </li></ul><ul><li>*ptr2 = 'c'; </li></ul><ul><li>The contents of ptr2 gets 'c' </li></ul><ul><li>Dereference on the Right </li></ul><ul><li>int * ptr1; </li></ul><ul><li>char * ptr2; </li></ul><ul><li>int anInt; </li></ul><ul><li>char aChar; </li></ul><ul><li>anInt = *ptr1; </li></ul><ul><li>anInt gets the contents of ptr1 </li></ul><ul><li>aChar = *ptr2; </li></ul><ul><li>aChar gets the contents of ptr2 </li></ul>
13. 13. Pointers to a thing  are not the same   as the thing <ul><li>So you can't mix and match. </li></ul><ul><li>You will have errors around this issue.  </li></ul><ul><li>int  *  myIntPtr =  & myInt; </li></ul><ul><li>int anotherInt = myIntPtr;  </li></ul><ul><li>// not legal </li></ul><ul><li>char * myCharPtr = *myChar; </li></ul><ul><li>char anotherChar = myCharPtr; </li></ul><ul><li>// again, not legal </li></ul>
14. 14. sizeof     <ul><li>This little funtion (or operator) tells you how much space a given thing takes up in memory. </li></ul><ul><li>You can find out interesting details about the architecture of your machine using sizeof. </li></ul>
15. 15. sizeof <ul><li>Guesses (and thoughts) from the students please: </li></ul><ul><li>How big is an int? sizeof(int) </li></ul><ul><li>How big is a char? sizeof(char) </li></ul><ul><li>How big is a pointer to an int? sizeof(int*) </li></ul><ul><li>How big is a pointer to a char? sizeof(char*) </li></ul><ul><li>How big is a literal? sizeof('a') </li></ul><ul><li>another literal? sizeof(999) </li></ul><ul><li>another literal? sizeof(&quot;blablblalba&quot;) </li></ul><ul><li>How big is a boolean? </li></ul><ul><li>How big is a double? </li></ul><ul><li>How big is an empty vector? </li></ul><ul><li>How big is a vector with entries? </li></ul>
16. 16. The Null Pointer    <ul><li>How do you initialize a pointer? </li></ul><ul><li>You set it to null, which is 0; </li></ul><ul><li>int * myIntPtr = 0; </li></ul><ul><li>Sometimes people test a pointer before using it: </li></ul><ul><li>if (myIntPtr) </li></ul><ul><li>{ </li></ul><ul><li>   // do some code if the pointer contains a value other than 0 </li></ul><ul><li>} </li></ul><ul><li>else </li></ul><ul><li>{ </li></ul><ul><li>// do some other code for the case when myIntPtr is empty </li></ul><ul><li>} </li></ul>
17. 17. The Null Pointer <ul><li>It's also valid to test for the null pointer like this: </li></ul><ul><li>if (myIntPtr != 0) </li></ul><ul><li>{ </li></ul><ul><li>// there is a value in there, do something </li></ul><ul><li>} </li></ul><ul><li>else </li></ul><ul><li>{ </li></ul><ul><li>// there's not a value in there, do something else </li></ul><ul><li>} </li></ul>
18. 18. Testing for the Null Pointer <ul><li>It's not 100% fool-proof to test for the null pointer. </li></ul><ul><li>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. </li></ul><ul><li>This happens when variables are not initialized or when they are mistakenly overwritten. </li></ul><ul><li>2) The pointer could be pointing to an object that has been deleted, or garbage collected. </li></ul><ul><li>WHAT? ARE? YOU? TALKING? ABOUT? </li></ul><ul><li>More next lecture. Difficult topics ahead! </li></ul>