Docsity

Data Structure - Pointer Story - Notes, Study notes of Data Structures and Algorithms

Partial preview of the text

Download Data Structure - Pointer Story - Notes and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Basic Pointers

  • (^) There are too many things that can only be done with pointers. But with increased power comes increased responsibility.
  • (^) Pointers allow new and more ugly types of bugs, and pointer bugs can crash in random ways which makes them more difficult to debug.
  • (^) Nonetheless, even with their problems, pointers are an irresistibly powerful programming construct.

Why Have Pointers?

  • (^) Pointers solve two common software problems.
  • (^) First, pointers allow different sections of code to share information easily.
  • (^) You can get the same effect by copying information back and forth, but pointers solve the problem better.
  • (^) Second, pointers enable complex "linked" data structures like linked lists and binary trees.
  • (^) A pointer works a little differently— it does not store a simple value directly. Instead, a pointer stores a reference to another value.

Pointer Dereference

  • (^) The "dereference" operation follows a pointer's reference to get the value of its pointee.
  • (^) The value of the dereference of numPtr above is
    1. When the dereference operation is used correctly, it just accesses the value of the pointee.
  • (^) The only restriction is that the pointer must have a pointee for the dereference to access. Almost all bugs in pointer code involve violating that one restriction. A pointer must be assigned a pointee before dereference operations will work.

Pointer Assignment

  • (^) The assignment operation (=) between two pointers makes them point to the same pointee.
  • (^) It's a simple rule for a potentially complex situation, so it is worth repeating: assigning one pointer to another makes them point to the same thing.
  • (^) The example below adds a second pointer, second, assigned with the statement second = numPtr;
  • (^) The result is that second points to the same pointee as numPtr. In the drawing, this means that the second and numPtr boxes both contain arrows pointing to num. Assignment between pointers does not change or even touch the pointees. It just changes which pointee a pointer refers to.

Shallow Copying

  • (^) Sharing can enable communication between two functions.
  • (^) One function passes a pointer to the value of interest to another function. Both functions can access the value of interest, but the value of interest itself is not copied.
  • (^) This communication is called "shallow" since instead of making and sending a (large) copy of the value of interest, a (small) pointer is sent and the value of interest is shared.
  • (^) The recipient needs to understand that they have a shallow copy, so they know not to change or delete it since it is shared.

Deep Copying

  • (^) The alternative where a complete copy is made and sent is known as a "deep” copy.
  • (^) Deep copies are simpler in a way, since each function can change their copy without interfering with the other copy, but deep copies run slower because of all the copying.

Bad Pointers

  • (^) When a pointer is first allocated, it does not have a pointee. The pointer is "uninitialized” or simply "bad".
  • (^) A dereference operation on a bad pointer is a serious runtime error. If you are lucky, the dereference operation will crash or halt immediately (Java behaves this way). If you are unlucky, the bad pointer dereference will corrupt a random area of memory, slightly altering the operation of the program so that it goes wrong some indefinite time later.
  • (^) Each pointer must be assigned a pointee before it can support dereference operations. Before that, the pointer is bad and must not be used.

Bad Pointers

  • (^) Bad pointers are very common. In fact, every pointer starts out with a bad value
  • (^) One way to think about pointer code is that operates at two levels — pointer level and pointee level.
  • (^) The trick is that both levels need to be initialized and connected for things to work. 1. the pointer must be allocated, 2. the pointee must be allocated, and 3. the pointer must be assigned to point to the pointee.

Pointer Variables

  • (^) Pointer variables are declared just like any other variable. The declaration gives the type and name of the new variable and reserves memory to hold its value. int* numPtr; // Declare the int* (pointer to int) variable "numPtr“. // This allocates space for the pointer, but not the pointee. // The pointer starts out “bad”.

The & Operator — Reference To

  • (^) There are several ways to compute a reference to a pointee suitable for storing in a pointer.
  • (^) The simplest way is the & operator. The & operator can go to the left of any variable, and it computes a reference to that variable.

The * Operator — Dereference

  • (^) The star operator (*) dereferences a pointer.
  • (^) The * is a unary operator which goes to the left of the pointer it dereferences. The pointer must have a pointee, or it's a runtime error.

Example Pointer Code void PointerTest() { // allocate three integers and two pointers int a = 1; int b = 2; int c = 3; int* p; int* q; // Here is the state of memory at this point. // T1 -- Notice that the pointers start out bad...