C Pointers - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

C Pointers

Description:

provide a method of referencing the memory location of variables. analogy in real world: ... Log on to Yahoo! Instant Messenger (I will send you an INVITE to join a ... – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 21
Provided by: jasont6
Category:

less

Transcript and Presenter's Notes

Title: C Pointers


1
C Pointers
  • Read Chapter 2
  • (P. 63 - 75)

2
COP3530 C Pointers
  • Pointers
  • provide a method of referencing the memory
    location of variables
  • analogy in real world
  • ATM card has a number on it that references your
    bank account
  • ordinary variable declaration
  • int value
  • compiler allocates memory
  • compiler also associates name with the memory
    address
  • compiler puts the associated value in slots (if
    necessary)
  • thus, (address operator) can be used to
    retrieve the address (slot number) of the
    variable

3
Pointers (cont.)
  • Declaring Pointers
  • format
  • type pointer
  • - this creates a variable named pointer that can
    be used to contain the address of another var of
    the defined type
  • example
  • int MyPointer
  • creates a pointer called MyPointer that can be
    used to point to another int
  • the pointer is bound to the type that it is
    defined for
  • can also use typedef to create a pointer type
  • typedef int IntPointer
  • IntPointer MyPointer
  • (creates a pointer of the IntPointer type called
    MyPointer)

4
Pointers (cont.)
  • Assigning values (addresses) to Pointers
  • format
  • int x 4
  • MyPointer x
  • indirection/dereferencing
  • again, MyPointer is a pointer that points to a
    memory address of some variable, in this case x.
  • MyPointer is basically an alias for the variable
    it points to
  • if I wish to know what value is located at an
    address pointed to by a pointer, I put an
    before the pointer
  • is called the indirection operator and using it
    like this is called dereferencing the pointer

5
Pointers (cont.)
  • ex
  • typedef int intPointer
  • void main()
  • int Value1 10
  • int Value2 20
  • intPointer Pointer1
  • Pointer1 Value1
  • cout ltlt "Value1 " ltlt Value1 ltlt endl
  • cout ltlt "Pointer1 " ltlt Pointer1 ltlt endl
  • Pointer1 Value2
  • cout ltlt "Value2 " ltlt Pointer1
  • What is output?

6
Pointers (cont.)
  • In the above example, if I wished to change the
    value of Value2, I could do so in two ways
  • (old way) Value2 30
  • Or, I could use the pointer to change the value
  • Pointer1 30
  • will reset the value that Pointer1 points to to
    30.

7
Pointers (cont.)
  • Pointers and Classes
  • Can use pointers to point to class objects
  • Clock ClockPtr
  • ClockPtr ClockPointer
  • ClockPointer Clock1
  • then you can easily pass objects to functions
    (will discuss later)
  • To reference a member function of an object
    pointer
  • (ClockPointer).theHour()
  • - or use the class pointer selector operator
  • ClockPointer -gttheHour() - will execute
    theHour() function of the Clock1 object (pointed
    to by ClockPointer)

8
Pointers (cont.)
  • Assign Pointer to a Pointer
  • int x 4, y 5
  • IntPointer Pointer1 x
  • intPointer Pointer2 y
  • Pointer1 Pointer2
  • both pointers now point to address of variable y
  • thus the following statement
  • cout ltlt Pointer1 ltlt " " ltlt Pointer2 ltlt endl
  • Will produce the following output
  • 5 5
  • (since derferencing both will give the value
    contained in y)
  • this is often difficult to debug
  • (the aliasing problem)

9
Why Use Pointers? / Pointers with function
arguments
  • original versions of C did not have operator
    with functions
  • if needed to pass variables by reference, you had
    to pass the address of the variable
  • void Doubleit(int n)
  • n 2
  • call DoubleIt(x)
  • Can be written
  • void Doubleit(intPointer Iptr)
  • Iptr 2
  • call DoubleIt(x)

10
Pointers and Arrays
  • when assigning arrays to pointers, the pointer
    will point to the starting point of the array
  • char MyArray10
  • typedef char CharPointer
  • CharPointer myPtr
  • myPtr MyArray //will point to slot 0 of
    MyArray

11
Pointers (cont.)
  • Other operations
  • comparisons
  • can use regular relational ops to see if two
    pointers point to same memory space
  • if (Pointer1 Pointer2) //true if
    point to same address
  • if (Pointer1 ! Pointer2) // true if
    they point to different
  • arithmetic
  • can add to/subtract from pointers
  • intPointer Pointer1
  • Pointer1
  • will add the size of that pointer to the pointer
    (move to next slot in memory)

12
More Pointer Operations
  • int myInts10
  • IntPointer myPointer
  • myPointer MyInts //points to slot 0
  • for (int i 0 i lt 10 i)
  • myPointer 0
  • myPointer
  • will initialize entire array to 0's

13
Dynamic Memory and Pointers
  • in all previous examples, memory is allocated for
    variables when program is compiled
  • programmer is forced to know the size of memory
    needed when the program is written
  • not always efficient
  • dynamic allocation
  • allowing the program to determine the size of
    memory needed at run-time
  • two steps
  • 1) acquire the memory when needed (allocate)
  • 2) discard the memory when done (deallocate)

14
Dynamic Memory (cont.)
  • Acquiring memory with new operation - new
  • used to request additional memory when program is
    running
  • format
  • new VariableType
  • ex
  • int intPtr
  • intPtr new int
  • requests a size in memory big enough to hold an
    int
  • if able, intPtr will be assigned the adress of
    the slot
  • if unable, the null address (0) is assigned
  • If the null adress is not assigned intPtr is said
    to be an anonymous variable (has no name assigned
    to it)
  • Can access the variable by using dereferencing
  • cin gtgt intPtr intPtr

15
Dynamic Memory (cont.)
  • To use new with an array
  • new VariableTypeArraySize
  • Ex
  • int TotalItems
  • cout ltlt Enter of values to store
  • cin gtgt TotalItems
  • intPtr MyArray
  • MyArray new intTotalItems
  • for (int i 0 i lt TotalItems i)
  • cout ltlt Enter Value
  • cin gtgt MyArrayi

16
Dynamic Memory (cont.)
  • The Delete operator
  • Used to remove data from the heap when no longer
    needed
  • Heap also called runtime stack
  • Delete
  • delete Pointer
  • delete ArrayPointer
  • Ex
  • int iptr char cptr
  • iptr new int cptr Memory
  • delete iptr Memory new
    char100
  • delete
    Memory

17
Dynamic Memory- Deconstructors
  • class Person
  • public
  • Person()
  • Person(char N, int A)
  • void Display()
  • private
  • char Name
  • int Age

18
Dynamic Memory Deconstructors
  • PersonPerson()
  • Name 0
  • Age 0
  • PersonPerson(char N, int A)
  • int Len
  • Len StrLen(N)
  • Name new charLen 1
  • Strcpy(Name, N)
  • Age A

19
Dynamic Memory Deconstructors
  • Program allocates memory at runtime for each
    object declared
  • Person P1(Jim Jones, 30)
  • Person P2(Steve Snapper, 25)
  • Make sure space is deallocated using
    deconstructor
  • Person()
  • PersonPerson()
  • delete Name
  • Automatically removes object from heap when
    lifetime of that object is exhausted

20
QUESTIONS?
  • Prepare for TEST 1
  • Chapter 1 of Book (Notes)
  • Review of Classes (Ch. 4 in Book and Notes)
  • ADTs (Lists) (Ch. 6 in Book and Notes)
  • Pointers (Ch. 2 in Book and Notes)
  • ONLINE HELP SESSION
  • Tues. July 4 700 a.m. 800 a.m. (tentatively)
  • Log on to Yahoo! Instant Messenger (I will send
    you an INVITE to join a conference when Im
    online)
Write a Comment
User Comments (0)
About PowerShow.com