General Talk on Pointers
Upcoming SlideShare
Loading in...5
×
 

General Talk on Pointers

on

  • 226 views

 

Statistics

Views

Total Views
226
Views on SlideShare
226
Embed Views
0

Actions

Likes
0
Downloads
4
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

General Talk on Pointers General Talk on Pointers Presentation Transcript

  • 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?