General Talk on Pointers and 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 SYNTAX int 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!

More Related Content

PPT
General Talk on Pointers
PDF
Microcontroladores: Programación en C para microcontroladores con AVR Butterf...
PPTX
Pointers
PDF
About size_t and ptrdiff_t
PPTX
Python programming language
PPTX
C Programming Language Step by Step Part 3
PDF
Python programming msc(cs)
PPT
Control structures pyhton
General Talk on Pointers
Microcontroladores: Programación en C para microcontroladores con AVR Butterf...
Pointers
About size_t and ptrdiff_t
Python programming language
C Programming Language Step by Step Part 3
Python programming msc(cs)
Control structures pyhton

What's hot (14)

PPT
Lecture 6
PDF
Chapter16 pointer
PDF
Pointers
PPTX
Presentation 2nd
PDF
14 Jo P Feb 08
PDF
Common mistakes in C programming
PPT
Datatypes
PDF
[ITP - Lecture 05] Datatypes
PPT
PDF
09 pseudo code
PPTX
Recursion with python
PPT
Csharp4 strings and_regular_expressions
PDF
Python If Else | If Else Statement In Python | Edureka
PDF
Acm aleppo cpc training ninth session
Lecture 6
Chapter16 pointer
Pointers
Presentation 2nd
14 Jo P Feb 08
Common mistakes in C programming
Datatypes
[ITP - Lecture 05] Datatypes
09 pseudo code
Recursion with python
Csharp4 strings and_regular_expressions
Python If Else | If Else Statement In Python | Edureka
Acm aleppo cpc training ninth session
Ad

Viewers also liked (9)

PPT
General Talk on Pointers
PPT
PPT
Family Resource Center Upper Nyack 2008-2009
PPT
More Pointers and Arrays
PPT
Classes and Inheritance
PPTX
Tartalommarketing - hogyan írjunk briefet a hatékony projekthez?
PDF
SLOW: Life in a Tuscan Town
PPTX
Tartalommarketing meetup - A Nagy Demisztifikáló Előadás 20150619
PDF
Stay Up To Date on the Latest Happenings in the Boardroom: Recommended Summer...
General Talk on Pointers
Family Resource Center Upper Nyack 2008-2009
More Pointers and Arrays
Classes and Inheritance
Tartalommarketing - hogyan írjunk briefet a hatékony projekthez?
SLOW: Life in a Tuscan Town
Tartalommarketing meetup - A Nagy Demisztifikáló Előadás 20150619
Stay Up To Date on the Latest Happenings in the Boardroom: Recommended Summer...
Ad

Similar to General Talk on Pointers (20)

PPT
Pointers
PDF
Pointers by: Professor Lili Saghafi
PPT
Pointers definition syntax structure use
PPT
Chapter09-10 Pointers and operations .PPT
PPT
Chapter09-10.PPT
PDF
Pointers & References in C++
PDF
VIT351 Software Development VI Unit3
PPTX
Intro To C++ - Class #17: Pointers!, Objects Talking To Each Other
PPT
Pointer
PDF
Chapter 5 (Part I) - Pointers.pdf
PPTX
POINTERS IN C
PDF
C for Java programmers (part 2)
PDF
Pointer in C++_Somesh_Kumar_Dewangan_SSTC
PDF
Chapter 2 - Introduction to Pointer Variables - Student.pdf
PPT
SPL_PS2 (1).ppt
PDF
C++ computer language chapter 4 pointers.pdf
PPTX
PPTX
Computer Programming Lecture numer 05 -- pointers,variablesb
PPTX
Pointers in c v5 12102017 1
PPTX
Pointers in c language
Pointers
Pointers by: Professor Lili Saghafi
Pointers definition syntax structure use
Chapter09-10 Pointers and operations .PPT
Chapter09-10.PPT
Pointers & References in C++
VIT351 Software Development VI Unit3
Intro To C++ - Class #17: Pointers!, Objects Talking To Each Other
Pointer
Chapter 5 (Part I) - Pointers.pdf
POINTERS IN C
C for Java programmers (part 2)
Pointer in C++_Somesh_Kumar_Dewangan_SSTC
Chapter 2 - Introduction to Pointer Variables - Student.pdf
SPL_PS2 (1).ppt
C++ computer language chapter 4 pointers.pdf
Computer Programming Lecture numer 05 -- pointers,variablesb
Pointers in c v5 12102017 1
Pointers in c language

Recently uploaded (20)

PDF
Five Habits of High-Impact Board Members
PDF
Hybrid model detection and classification of lung cancer
PPTX
Final SEM Unit 1 for mit wpu at pune .pptx
PDF
Getting started with AI Agents and Multi-Agent Systems
PDF
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
PDF
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
PPTX
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
PPTX
Benefits of Physical activity for teenagers.pptx
PDF
Taming the Chaos: How to Turn Unstructured Data into Decisions
PDF
Zenith AI: Advanced Artificial Intelligence
PDF
1 - Historical Antecedents, Social Consideration.pdf
PPTX
observCloud-Native Containerability and monitoring.pptx
PPT
Module 1.ppt Iot fundamentals and Architecture
PDF
DP Operators-handbook-extract for the Mautical Institute
PPTX
O2C Customer Invoices to Receipt V15A.pptx
PDF
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
PDF
WOOl fibre morphology and structure.pdf for textiles
PDF
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
PDF
Getting Started with Data Integration: FME Form 101
PDF
A Late Bloomer's Guide to GenAI: Ethics, Bias, and Effective Prompting - Boha...
Five Habits of High-Impact Board Members
Hybrid model detection and classification of lung cancer
Final SEM Unit 1 for mit wpu at pune .pptx
Getting started with AI Agents and Multi-Agent Systems
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
Benefits of Physical activity for teenagers.pptx
Taming the Chaos: How to Turn Unstructured Data into Decisions
Zenith AI: Advanced Artificial Intelligence
1 - Historical Antecedents, Social Consideration.pdf
observCloud-Native Containerability and monitoring.pptx
Module 1.ppt Iot fundamentals and Architecture
DP Operators-handbook-extract for the Mautical Institute
O2C Customer Invoices to Receipt V15A.pptx
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
WOOl fibre morphology and structure.pdf for textiles
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
Getting Started with Data Integration: FME Form 101
A Late Bloomer's Guide to GenAI: Ethics, Bias, and Effective Prompting - Boha...

General Talk on Pointers

  • 1. General Talk on Pointers and memory
  • 2. 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!
  • 3. 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.
  • 4. 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 
  • 5. 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.
  • 6. 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?
  • 7.   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.
  • 8.   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.
  • 9.   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.
  • 10.   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.
  • 11. 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 SYNTAX int myOtherInt = * myIntPtr; "*variable name" is read as "the contents of variable name"
  • 12. 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
  • 13. 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
  • 14. 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.
  • 15. 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?
  • 16. 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 }
  • 17. 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 }
  • 18. 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!