Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ics212-14-pointers

 ics212-14-pointers

Avatar for William Albritton

William Albritton

October 12, 2015
Tweet

More Decks by William Albritton

Other Decks in Programming

Transcript

  1. Memory Allocation  Declaring and initializing pointers  Address operator

    and dereferencing operator  Pointer Arithmetic
  2. Pointers  Pointers are variables that contain a memory address

    of another variable as its value  What are they used for?  Generic access to several similar variables  Call by reference function parameters  Dynamically allocated data structures  Creating copies of arrays and structures
  3. Declare and Initialize a Variable  Every local variable has

    a name, a value, and an address in memory on the runtime stack name value address number1 0xffbffa4c int number1 = 5; 5
  4. Declare and Initialize a Pointer  A pointer local variable

    also has a name, a value, and an address in memory on the runtime stack name value address number1 0xffbffa4c pointer1 0xffbffa48  NULL means that the pointer does not point to anything int *pointer1 = NULL; 5 NULL
  5. Declare and Initialize a Pointer  When we declare a

    pointer, the asterisk (*) indicates that we are creating a pointer, which is a variable that stores an address to another variable int *pointer1 = NULL;
  6. Declare and Initialize a Pointer  When we declare a

    pointer, the pointer’s type should match the variable’s type which it will point to  So the type for number1 and pointer1 is int int number1 = 5; int *pointer1 = NULL;
  7. Assign an Address to a Pointer  The address operator

    (&) returns the address of number1 (0xffbffa4c), which is stored in pointer1 name value address number1 0xffbffa4c pointer1 0xffbffa48  Usually the “pointing” is represented with an arrow pointer1 = &number1; //0xffbffa4c 5 0xffbffa4c
  8. Dereferencing a Pointer  The dereferencing operator (*) returns the

    value stored at the address that is stored in pointer1 name value address number1 0xffbffa4c pointer1 0xffbffa48  So the output is: 5 printf(“%i”, *pointer1); //5 5 0xffbffa4c
  9. Dereferencing a Pointer  The dereferencing operator (*) returns the

    value which pointer1 points to, so number2 has value 5 name value address number1 0xffbffa4c pointer1 0xffbffa48 number2 0xffbffa44 number2 = *pointer1; //5 5 0xffbffa4c 5
  10. Address Stored in the Pointer  We don’t use the

    dereferencing operator (*) if we want to access the address stored in the pointer name value address number1 0xffbffa4c pointer1 0xffbffa48  So the output is: 0xffbffa4c printf(“0x%p”, pointer1); //0xffbffa4c 5 0xffbffa4c
  11. Pointer’s Address on Runtime Stack  We do use the

    address operator (&) if we want to access the pointer’s address on the runtime stack name value address number1 0xffbffa4c pointer1 0xffbffa48  So the output is: 0xffbffa48 printf(“0x%p”, &pointer1);//0xffbffa48 5 0xffbffa4c
  12. Pointer’s Address on Runtime Stack  Similarly, we use the

    address operator (&) to access the local variable’s address on the runtime stack name value address number1 0xffbffa4c pointer1 0xffbffa48  So the output is: 0xffbffa4c printf(“0x%p”, &number1); //0xffbffa4c 5 0xffbffa4c
  13. Local Variable’s Address  Same example as the last slide,

    using number2  Note that %p is used to display addresses name value address number1 0xffbffa4c pointer1 0xffbffa48 number2 0xffbffa44 printf(“0x%p”, &number2); //0xffbffa44 5 0xffbffa4c 5
  14. What Not to Do with Pointers!  Never, ever dereference

    (*) a NULL pointer  This will crash your program with this error message: Segmentation fault (core dumped) int *pointer1 = NULL; printf(“%i\n”, *pointer1);
  15. Segmentation Fault and Core Dump  A segmentation fault happens

    when your program tries to access memory at an address that it is not supposed to have access to  A core dump is a file that stores the complete state of a process (running program) that has crashed  The term comes from magnetic-core memory, which was used as RAM in computers from 1955 to 1975
  16. Pointer Operator Review  Only use two operators for pointers

     & operator: address operator  Returns the address of a variable pointer1 = &number1; //0xffbffa4c
  17. Pointer Operator Review  * operator: dereferencing (indirection) operator 

    Returns the value of the object to which the address points to  See example code at: pointers.c number2 = *pointer1; //5
  18. Binky Pointer Video  Check out a really cool 3

    minute claymation video that also explains how pointers work!  See the link on the class webpage: http://cslibrary.stanford.edu/  From the Stanford CS Education Library
  19. Valid Pointer Operations  You can:  Assign pointers of

    the same type to each other  Add or subtract a pointer & an integer  Assign or compare a pointer to zero (NULL) or to an address
  20. Invalid Pointer Operations  You cannot:  Assign pointers of

    different types to each other without a cast  Add a float or double to a pointer  Add two pointers  Multiply, divide, do modulus, or shift a pointer
  21. Adding with a Pointer’s Address  With pointers, you can

    alter the value that the pointer is pointing to by: 1. Dereferencing the pointer 2. Changing the value that the pointer points to
  22. Assigning an Address to a Pointer  Runtime stack for

    each line of executable code address name value 0xffbffa47 letter1 0xffbffa40 pointer1 char letter1 = 'A'; char *pointer1 = NULL; pointer1 = &letter1; ‘A’ 0xffbffa47
  23. Adding One with a Pointer  Runtime stack for each

    line of executable code address name value 0xffbffa47 letter1 0xffbffa40 pointer1 /*unary operators associate right to left, so dereference, then add*/ ++*pointer1; ‘B’ 0xffbffa47
  24. Adding One Again with a Pointer  Dereference pointer, then

    add one address name value 0xffbffa47 letter1 0xffbffa40 pointer1 /*unary operators associate right to left, so have to use parentheses*/ (*pointer1)++; ‘C’ 0xffbffa47
  25. You Can Re-assign a Pointer  Now the pointer points

    to letter2! address name value 0xffbffa47 letter1 0xffbffa40 pointer1 0xffbffa3f letter2 char letter2 = 'z'; pointer1 = &letter1; pointer1 = &letter2; ‘C’ 0xffbffa3f ‘z’
  26. Dereferenced Pointer  Just like any other variable, you can

    do as you wish with a dereferenced pointer address name value 0xffbffa47 letter1 0xffbffa40 pointer1 0xffbffa3f letter2 *pointer1 = ((*pointer1 / 2) * 2) - 2; ‘C’ 0xffbffa3f ‘x’
  27. Adding to a Pointer’s Address  Can also add to

    (or subtract from) the address that is stored in the pointer variable
  28. Assigning an Address to a Pointer  Runtime stack representation

    of above code address name value 0xffbffa40 x 0xffbffa38 y 0xffbffa30 z 0xffbffa2c pointer1 double x=3.3, y=6.6, z=9.9; double *pointer1=&y; 3.3 6.6 9.9 0xffbffa38
  29. Dereferencing a Pointer  Change the value that the pointer

    points to address name value 0xffbffa40 x 0xffbffa38 y 0xffbffa30 z 0xffbffa2c pointer1 *pointer1 = *pointer1 + 1.1; 3.3 7.7 9.9 0xffbffa38
  30. Adding to a Pointer’s Address  Increments by 8 bytes,

    because points in an double address name value 0xffbffa40 x 0xffbffa38 y 0xffbffa30 z 0xffbffa2c pointer1 pointer1 = pointer1 + 1; 3.3 7.7 9.9 0xffbffa40
  31. Dereferencing a Pointer  Change the value of another variable

    with pointer2 address name value 0xffbffa40 x 0xffbffa38 y 0xffbffa30 z 0xffbffa2c pointer1 *pointer1 = *pointer1 + 1.1; 4.4 7.7 9.9 0xffbffa40
  32. Subtracting from a Pointer’s Address  What variable will pointer1

    point to? address name value 0xffbffa40 x 0xffbffa38 y 0xffbffa30 z 0xffbffa2c pointer1 pointer1 = pointer1 - 2; 4.4 7.7 9.9 0xffbffa30
  33. Memory Management  Declaring and initializing pointers  Address operator

    & dereferencing operator  Pointers and Arithmetic