CS102: Pointers

Pointer Introduction:

  • Pointers store a memory location

    • They “point” to a memory location

  • All pointers have the same size:

    • 64-bit machine: 8-bytes (64-bits)

    • 32-bit machine: 4-bytes (32-bits)


Pointer Syntax:

  • Declaring:

    • A pointer is marked by an asterisk (can be at the end of the data type or the beginning of the variable name)

    • The asterisk denotes that this variable stores a memory address.

  • Address of (&):

  • Gets the memory address of a variable

  • Use the (&) operator, also known as the “address-of” operator

int k = 22; 
cout « k; //Prints 22 (the value of k)
cout « &k; //Prints the memory address of k


Using Pointers:

  • We use * in two ways: to declare pointers and to dereference pointers

int *ptr_k; ////Instantiates a pointer
int k; //Instantiates a variable

*ptr_k = &k; //Puts memory address of k into pointer ptr_k

*ptr_k = 771; //Puts the value 771 into the variable located by ptr_k


cout << k; //Prints 771


Allocating/De-Allocating New Memory:

  • The “new” keyword creates a new dynamic variable of a specified type and return s a pointer that points to this new variable.

  • Allocates memory using the “(NonPointingVariable) = new (VariableType)” method

  • De-Allocates memory using the “delete (NonPointingVariable)” method

//Allocation
int *k; 
k  = new int; 
k* = 60; 
cout « *k; 
//Output: 60 (if the code ended here)

//De-Allocation: 
delete k; 

Allocation/De-Allocating New Memory to Objects:

  • Allocates memory using the “(NonPointingVariable) = new (ClassName)()” method

  • De-Allocates memory using the “delete (NonPointingVariable)” method

//Allocation: 
Dog *pup; 

pup = new Dog(); 

(*pup).age = 10; 

//Deallocation: 
delete pup; 

Allocation/De-Allocating New Memory to Arrays:

  • Allocates memory using the “(NonPointingArray) = new (data type)[ ]” method

  • De-Allocates memory using the “delete[] (NonPointingArray)” method

    • Individual elements CANNOT BE DELETED

//Allocation: 
int *k_array; 

k_array = new int[5]; 

k_array[2] = 10; 

*(k_array + 2) = 10; 

//De-Allocation: 
delete[] k_array; 


Pointer Arithmetic

  • When adding to a pointer, the current value, offset value, and data type size must be identified.

    • Current: The original value of the pointer

    • Offset: The value being added to the pointer

    • Data Size: The number of bytes in the data type of the current and the offset

  • Will likely need to convert between decimal, binary, and hex data types


Variable Pointer Arithmetic:

int *a = (int*) 0x64; //0h64 (Hex) = 100 (Decimal)

cout << (a + 10); // Current: 100
		  // Offset: 10
		  // Data Type Size: 4
		  // = (current) + (offset) * (size)
		  // = (100) + (10) * (4)
		  // Output: 140 (decimal) 
		  // = 0x8A (Hex)

Subscript on a Pointer:

  • Allows for the pointer to be deref


Array Pointer Arithmetic:

int array[] = {1,2,3,4,5]; 

int *pointer_a = array + 1; //*pointer_a now points one element before the beginning of the array, from array[0] to array[1].

cout << *(pointer_a + 1); //*pointer_a now points two elements before the beginning of the array, from array[0] to array[2]. Then, the pointer is dereferenced so that the output will be a value instead of a memory address.

//Output: 3


Data Size Table:

Signed vs. Unsigned Values:

  • Signed: Range split between positive and negative values, each exactly the same distance from 0.\

  • Unsigned: Same range as signed variables, but have a lower bound of ‘0’ to their range.