CO4301 – Advanced Games Development Week 1 Introduction

Slides:



Advertisements
Similar presentations
COMP171 Data Structure & Algorithm Tutorial 1 TA: M.Y.Chan.
Advertisements

Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Computer Science II Exam I Review Monday, February 6, 2006.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 9: Pointers.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
EE4E. C++ Programming Lecture 1 From C to C++. Contents Introduction Introduction Variables Variables Pointers and references Pointers and references.
 200 Total Points ◦ 74 Points Writing Programs ◦ 60 Points Tracing Algorithms and determining results ◦ 36 Points Short Answer ◦ 30 Points Multiple Choice.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading; String and Array Objects.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
1 Advanced Issues on Classes Part 3 Reference variables (Tapestry pp.581, Horton 176 – 178) Const-reference variables (Horton 176 – 178) object sharing:
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review Part-I.
Parameter Passing Mechanisms Reference Parameters § §
C++ Memory Overview 4 major memory segments Key differences from Java
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
1 Becoming More Effective with C++ … Day Two Stanley B. Lippman
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
More about Java Classes Writing your own Java Classes More about constructors and creating objects.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
Defining Your Own Classes II
Yan Shi CS/SE 2630 Lecture Notes
Object Lifetime and Pointers
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Dynamic Storage Allocation
Chapter 6 CS 3370 – C++ Functions.
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Introduction to C++ Systems Programming.
Motivation and Overview
Introduction to C++ Introduced by Bjarne Stroustrup of AT&T’s Bell Laboratories in mid-1980’s Based on C C++ extended C to support object-oriented programming.
Object-Oriented Programming & Design Lecture 18 Martin van Bommel
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
CS212: Object Oriented Analysis and Design
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Object Oriented Programming COP3330 / CGS5409
Introduction to Classes
Chapter 10: Pointers Starting Out with C++ Early Objects Ninth Edition
CS 2308 Exam I Review.
7 Arrays.
Chapter 9: Pointers.
Chapter 6 Methods: A Deeper Look
Pointers, Dynamic Data, and Reference Types
Built-In (a.k.a. Native) Types in C++
10.2 const (Constant) Objects and const Member Functions
Introduction to C++ Introduced by Bjarne Stroustrup of AT&T’s Bell Laboratories in mid-1980’s Based on C C++ extended C to support object-oriented programming.
CMPE 135: Object-Oriented Analysis and Design November 27 Class Meeting Department of Computer Engineering San Jose State University Fall 2018 Instructor:
Object-Oriented Programming
7 Arrays.
Overloading functions
COP 3330 Object-oriented Programming in C++
9-10 Classes: A Deeper Look.
Modern C++ Pitfalls Mihai Todor
CISC/CMPE320 - Prof. McLeod
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Pointers, Dynamic Data, and Reference Types
Gentle* Introduction to Modern C++
9-10 Classes: A Deeper Look.
CS 144 Advanced C++ Programming April 30 Class Meeting
Classes and Objects Object Creation
CMSC 202 Constructors Version 9/10.
Introduction to Classes and Objects
Presentation transcript:

CO4301 – Advanced Games Development Week 1 Introduction Gareth Bellaby

Introduction

Module contents Advanced Programming Concurrency Concurrency for game logic. Parsers. Rapid prototyping Games programming patterns Optimising algorithms

Module contents Optimisation and efficiency. Binary search Red/black trees (sets) Self-balancing trees Balance search

Questions I need to check the starting point for some of these topics. What do you know about the various topics?

C11

C11 3 reasons to do this: Nice introduction to the module Important for careers C11 has loads of cool stuff

C11 Overview: Types: auto, nullptr, new enums, new style functions, decltype Simplified initialisation, member defaults Range-based for loop Standard library: smart pointers, hash-tables, regex and much more Constructor delegation and deletion, “final” virtual functions Extensive support for multithreading Lambda expressions: (anonymous functions, Javascript-like) Constant expressions: code executed by the compiler Parameters & return values: rvalues and move semantics Template extensions such as variadic templates For more details see: https://isocpp.org/faq

References

References Previously stated that references are useful but not default. Laurent has said that he’s now using references by default. Reading the C++ faq and comments from people such as Stroustrup it certainly seems to be the case that references are now the preferred form. Use const references. Use non-const references.

References - quote When should I use references, and when should I use pointers? Use references when you can, and pointers when you have to. References are usually preferred over pointers whenever you don’t need “reseating”. This usually means that references are most useful in a class’s public interface. References typically appear on the skin of an object, and pointers on the inside. The exception to the above is where a function’s parameter or return value needs a “sentinel” reference — a reference that does not refer to an object. This is usually best done by returning/taking a pointer, and giving the NULL pointer this special significance (references must always alias objects, not a dereferenced null pointer). https://isocpp.org/wiki/faq/references

References A reference always points to the same object. A reference cannot be null. A pointer can point at multiple objects. A pointer can be null (or 0). Further: A reference cannot be uninitialised.

Smart Pointers

Smart pointers Smart pointers introduce two important things: Automatic memory management: no need to worry about memory leaks. Delete gets called automatically when an object is killed. Ownership of an object. A single pointer owns a single object at any time.

C11 Include the <memory> library A smart pointer variable is declared using unique_ptr. Need to supply the data type No need to use delete. The memory is managed for you. When you declare a unique_ptr, the compiler creates a raw pointer and wraps it around with extra code to manage the memory. unique_ptr <obj> ptr(new obj);

Ownership The pointer owns the block of memory being pointed to. It owns it uniquely. An unique_ptr prevents copying of its contained address. It means that only one thing can own the memory block at a time and you have to explicitly transfer ownership to another pointer. Why this done?

Ownership The transferral of ownership is done using the keyword move. ptr2 = move(ptr1);

Reuse of an unique_ptr You can reuse the same pointer to create a new object, or a whole series of them. The method reset is used to do this. unique_ptr <coords> tmp(new coords); tmp.reset(new coords);

Raw pointer A smart point is implemented using a standard pointer. Terminology is that the standard pointer is called a “raw pointer”. You can obtain the raw pointer using get(). coords* t; t = ptr.get(); cout << t->x << endl;

Raw pointer For obvious reasons using a raw pointer is dangerous. It allows you full access to the pointer and memory, exactly what the introduction of smart pointers was intended to stop. Raw pointers can be useful to observe the contents of a unique pointer. Do not use them to undermine or circumvent the (useful) limitations of a unique pointer.

Ranged-based for loop C11 introduces ranged-based loop. Don’t need to declare an iterator A good place to use auto! Works for anything that has begin() and end() string str = "hello world"; for (auto i : str) cout << i << endl; deque<int> scores; for (auto value : scores) cout << value;

Functions Use move() to transfer ownership of a unique pointer to a function. And use it as well to ownership via the return statement. unique_ptr<int> Transfer(unique_ptr <int> t) { t = 2; return std::move(t); }

Reference Use of a reference works fine. void foo(unique_ptr <coords>& p) { p->x = 5; }

Shared pointers You also have use of shared pointers. This is meant for when multiple pointers share ownership of a block of memory and so (obviously) it only gets deleted when all of the owners disappear. Implemented using an internal count. As a rule do not use shared pointers. It undermines the intention of smart pointers. Think of an alternative approach if shared pointers look to be a solution.

Lambda Expressions

Lambda expressions Unnamed local function Describes a simple operation Described at point of use, so makes it easier to read. A good example of use being sort.

Capture list a

Efficiency As a general statement there is no difference between a lambda function and a function in terms of efficiency. It does not magically make things faster. Indeed a compiler may build an equivalent function at compile time. However, it can be more efficient. If you pass a pointer to a function it may not be easy for the compiler to generate an inline version of the function. A lamdba function is an instance of a class.

Purpose It can help the compiler to optimise by providing a hint as to context. It allows you to use throw-away functions or functions that only have a local meaning or importance. This helps preserve localisation and supports safe coding.

Why “lambda” Lambda origins lie in the lambda calculus, which is a formal system in maths for describing computation. Uses the greek letter lambda (λ). lambda (λ) is used in lambda expressions to indicate binding a variable in a function. In the Lambda calculus you can write function anonymously, i.e. without giving them a name. So Lambda functions are the C++ implementation of anoymous functions.

Lambda functions Square(x) = x2 Can be written as: (x) |-> x2 In other words, as an anonymous function.