CAP 2 AC

Subdecks (1)

Cards (319)

  • C's pointer variables provide a level of indirection to accessing program memory
  • Pointer variables
    • They allow a programmer to:
    • Implement functions whose parameters can modify values in the caller's stack frame
    • Dynamically allocate (and deallocate) program memory at runtime when the program needs it
    • Efficiently pass large data structures to functions
    • Create linked dynamic data structures
    • Interpret bytes of program memory in different ways
  • Pointer variable

    Stores the address of a memory location in which a value of a specific type can be stored
  • A pointer provides a level of indirection for accessing values stored in memory
  • Pointer variable declaration
    type_name *var_name
  • Initializing a pointer variable
    Declare a pointer variable
    2. Initialize the pointer variable (make it point to something)
    • Use the address operator (&) with a variable to get the variable's address value
  • Pointer variables can also be assigned the special value NULL, which represents an invalid address
  • Dereferencing a pointer
    Using the dereference operator (*) to access the value at the location the pointer points to
  • Pointer variable usage
    • ptr = &x; // initialize ptr to the address of x
    *ptr = 8; // assign 8 to the memory location ptr points to
    2. ptr2 = &x; // ptr2 is assigned the address of x
    *ptr2 = 10; // the memory location ptr2 points to is assigned 10
    y = *ptr2 + 3; // y is assigned what ptr2 points to plus 3
    3. ptr1 = ptr2; // ptr1 gets the address value stored in ptr2
    *ptr1 = 100; // assign 100 to the memory location ptr1 points to
  • Carefully consider the types of the relevant variables when using pointer variables
  • Drawing pictures of memory can help with understanding what pointer code is doing
  • Common errors involve misusing the dereference operator (*) or the address operator (&)
  • When using pointer variables, carefully consider the types of the relevant variables. Drawing pictures of memory (like those shown above) can help with understanding what pointer code is doing.
  • Some common errors involve misusing the dereference operator ( * ) or the address operator ( & ).
  • If your program dereferences a pointer variable that does not contain a valid address, the program crashes.
  • Initializing pointer variables to NULL
    A program can then test a pointer's value for NULL before dereferencing it
  • Implementing and calling a function with a pass by pointer parameter
    Declare the function parameter to be a pointer to the variable type
    2. When making the function call, pass in the address of a variable as the argument
    3. In the body of the function, dereference the pointer parameter to change the argument's value
  • All arguments in C are passed by value and follow pass-by-value semantics: the parameter gets a copy of its argument value, and modifying the parameter's value does not change its argument's value.
  • In the pass-by-pointer pattern, the parameter still gets the value of its argument, but it is passed the value of an address. Changing a pointer parameter's value will not change its argument's value, but by dereferencing a pointer parameter, the function can change the contents of memory that both the parameter and its argument refer to.
  • Dynamic memory allocation allows a C program to request more memory as it's running, and a pointer variable stores the address of the dynamically allocated space.
  • Reasons for dynamic memory allocation
    • Do not know the size of arrays or other data structures until runtime
    Need to allow for a variety of input sizes
    Want to allocate exactly the size of data structures needed for a particular execution
    Grow or shrink the sizes of memory allocated as the program runs
  • Heap memory
    Dynamically allocated memory occupies the heap memory region of a program's address space
  • Heap memory is anonymous memory, where "anonymous" means that addresses in the heap are not bound to variable names.
  • malloc
    A function in the standard C library that a program can call to allocate memory in the heap
  • free
    A function in the standard C library that a program can call to deallocate memory in the heap
  • A call to malloc fails if there is not enough free heap memory to satisfy the requested number of bytes to allocate.
  • When a program no longer needs the heap memory it dynamically allocated with malloc, it should explicitly deallocate the memory by calling the free function.
  • C programmers often dynamically allocate memory to store arrays. A successful call to malloc allocates one contiguous chunk of heap memory of the requested size.
  • Statically declared arrays are allocated either on the stack (for local variables) or in the data region of memory (for global variables)
  • Statically declared array
    An array variable declared with a fixed size, where the memory for the array is allocated at compile-time
  • When passing an array to a function, C copies the value of the base address to the parameter
  • Both the parameter and the argument refer to the same memory locations - the parameter pointer points to the argument's array elements in memory
  • Modifying the values stored in the array through an array parameter modifies the values stored in the argument array
  • Dynamically allocated array
    An array allocated on the heap at runtime using malloc()
  • Dynamically allocating an array
    1. Call malloc() with the total number of bytes to allocate for the array
    2. Assign the returned address to a pointer variable
  • A single call to malloc() allocates a contiguous chunk of heap space of the requested size
  • Whether an array is statically declared or dynamically allocated, array elements represent contiguous memory locations (addresses)
  • The location of element i in an array is at a base address + i * sizeof(element_type)
  • malloc
    Function to allocate the appropriate number of bytes for an array
  • Allocating a 1D array
    1. Call malloc with sizeof(int) * 50
    2. Check return value
    3. Use [] notation to access elements
    4. Free heap space when done