Difference between revisions of "Intermediate C++ Game Programming Tutorial 2"

From Chilipedia
Jump to: navigation, search
(Created page with "In this two-part tutorial we learn all about pointers and pointer-related bullshits. Part one focuses on the basics--the most common pointer operations and the basic underlyin...")
 
(Part 1)
 
(27 intermediate revisions by the same user not shown)
Line 1: Line 1:
In this two-part tutorial we learn all about pointers and pointer-related bullshits. Part one focuses on the basics--the most common pointer operations and the basic underlying mechanisms. In part two, we take a look at some less commonly-used but still important pointer operations (pointer arithmetic), as well as some other concepts related to pointers. This tutorial introduces a lot of new concepts and features, but is light on practical examples. In the following tutorials we will get more hands-on practice with pointers, '''but also make sure to experiment with them on your own after finishing Tutorial 2'''.
+
In this two-part tutorial we learn all about pointers and pointer-related bullshits. Part one focuses on the basics--the most common pointer operations and the basic underlying mechanisms. In part two, we take a look at some less commonly-used but still important pointer operations (pointer arithmetic), as well as some other concepts related to pointers. This tutorial introduces a lot of new concepts and features, but is light on practical examples. In the following tutorials we will get more hands-on practice with pointers, but also '''<u>make sure to experiment with them on your own after finishing Tutorial 2'''</u>.
  
 
== Topics Covered ==
 
== Topics Covered ==
Line 9: Line 9:
 
* The indexing (subscript) operator <code>&#91;&#93;</code>
 
* The indexing (subscript) operator <code>&#91;&#93;</code>
 
* The <code>nullptr</code> pointer literal
 
* The <code>nullptr</code> pointer literal
 +
* The <code>sizeof</code> query operator
  
 
=== Part 2 ===
 
=== Part 2 ===
Coming soon!
+
* Pointer Arithmetic (adjustment,  comparison, and difference)
 +
* Pointer <code>const</code> Correctness
 +
* Pointers to Pointers
 +
* Pointer Reinterpretation
 +
* C++ Style Casting <code>reinterpret_cast<></code>
 +
* Linked Lists
  
 
== Video Timestamp Index ==
 
== Video Timestamp Index ==
[https://youtu.be/xxxx Tutorial 2]
+
[https://www.youtube.com/watch?v=9OBISBBZ3CQ Tutorial 2.1]<br />
 +
[https://www.youtube.com/watch?v=j1ZF__Ml5ds Tutorial 2.2]
 +
 
 +
== Bonus Video: Red Pill [Pointers] ==
 +
This tutorial has another associated Red Pill video that explores the actual machine instructed emitted by the compiler for pointer-related operations. Like the previous Red Pill video, this material is strictly optional and is not required to proceed with the tutorials--it will not be referenced in future tutorials. Viewer discretion is advised. The video can be found: [https://www.youtube.com/watch?v=gTXXof0gzqs here].
  
 
== Homework ==
 
== Homework ==
 
=== Part 1 ===
 
=== Part 1 ===
Write a function called <code>sum</code> which returns the sum of all elements of an int array.
+
Write a function called <code>sum</code> which returns the sum of all elements of an <code>int</code> array.<br />
 +
[https://youtu.be/AJrpTYNIs5A Solution Video].
 +
 
 +
=== Part 2 ===
 +
There are 2 assignments. Assignment 1: modify the <code>sum</code> function so that it uses pointer arithmetic. Assignment 2: create a new function to reverse an array of <code>int</code>s using pointer arithmetic techniques.<br />
 +
[https://youtu.be/GhwJAMp7phg Solution Video]
 +
 
 +
== Notes ==
 +
* At 24:30 in Part 1, Chili shows the calculation for indexing off of a pointer (<code>p[2]</code>) as B + 2 x 4 -> B, and then a memory store operation 69 -> int@[B], but in reality (optimized or not) the index calculation would be done as part of the memory access operation, so it would end up being something like 69 -> int@[B + 2 x 4].
  
 
== See also ==
 
== See also ==
 
* [[Intermediate C++ Game Programming Tutorial 3|Next in series (Tutorial 3)]]
 
* [[Intermediate C++ Game Programming Tutorial 3|Next in series (Tutorial 3)]]
 
* [[Intermediate C++ Game Programming Series]]
 
* [[Intermediate C++ Game Programming Series]]

Latest revision as of 23:42, 23 May 2017

In this two-part tutorial we learn all about pointers and pointer-related bullshits. Part one focuses on the basics--the most common pointer operations and the basic underlying mechanisms. In part two, we take a look at some less commonly-used but still important pointer operations (pointer arithmetic), as well as some other concepts related to pointers. This tutorial introduces a lot of new concepts and features, but is light on practical examples. In the following tutorials we will get more hands-on practice with pointers, but also make sure to experiment with them on your own after finishing Tutorial 2.

Topics Covered

Part 1

  • Declaring pointers
  • The addressof operator &
  • The dereference (indirection) operator *
  • The arrow (member of pointer) operator ->
  • The indexing (subscript) operator []
  • The nullptr pointer literal
  • The sizeof query operator

Part 2

  • Pointer Arithmetic (adjustment, comparison, and difference)
  • Pointer const Correctness
  • Pointers to Pointers
  • Pointer Reinterpretation
  • C++ Style Casting reinterpret_cast<>
  • Linked Lists

Video Timestamp Index

Tutorial 2.1
Tutorial 2.2

Bonus Video: Red Pill [Pointers]

This tutorial has another associated Red Pill video that explores the actual machine instructed emitted by the compiler for pointer-related operations. Like the previous Red Pill video, this material is strictly optional and is not required to proceed with the tutorials--it will not be referenced in future tutorials. Viewer discretion is advised. The video can be found: here.

Homework

Part 1

Write a function called sum which returns the sum of all elements of an int array.
Solution Video.

Part 2

There are 2 assignments. Assignment 1: modify the sum function so that it uses pointer arithmetic. Assignment 2: create a new function to reverse an array of ints using pointer arithmetic techniques.
Solution Video

Notes

  • At 24:30 in Part 1, Chili shows the calculation for indexing off of a pointer (p[2]) as B + 2 x 4 -> B, and then a memory store operation 69 -> int@[B], but in reality (optimized or not) the index calculation would be done as part of the memory access operation, so it would end up being something like 69 -> int@[B + 2 x 4].

See also