Workshop 1++: A bit of C++

Slides:



Advertisements
Similar presentations
1.00 Lecture 37 A Brief Look at C++: A Guide to Reading C++ Programs.
Advertisements

Writing Modern C++ Marc Grégoire Software Architect April 3 rd 2012.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
Review of pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Memory and C++ Pointers.  C++ objects and memory  C++ primitive types and memory  Note: “primitive types” = int, long, float, double, char, … January.
1 CSE 303 Lecture 21 Classes and Objects in C++ slides created by Marty Stepp
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
ITEC 352 Lecture 18 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Exam –Average 76 Methods for functions in assembly.
Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Mutability: C++ const usage SWE 332 Fall 2011 Paul Ammann.
C++ (intro) Created by Hwansoo Han Edited by Ikjun Yeom.
Peyman Dodangeh Sharif University of Technology Spring 2014.
Engineering Classes. Objectives At the conclusion of this lesson, students should be able to: Explain why it is important to correctly manage dynamically.
Lecture 7.  There are 2 types of libraries used by standard C++ The C standard library (math.h) and C++ The C++ standard template library  Allows us.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
Objects and Memory Mehdi Einali Advanced Programming in Java 1.
Memory Management.
Dynamic Allocation in C
CSE 374 Programming Concepts & Tools
Object Lifetime and Pointers
Pointer to an Object Can define a pointer to an object:
CMSC 341 Lecture 2 – Dynamic Memory and Pointers (Review)
Memory Management with Classes
CSE 374 Programming Concepts & Tools
Motivation and Overview
Pointers, Polymorphism, and Memory Allocation
COM S 326X Deep C Programming for the 21st Century Prof. Rozier
COSC 220 Computer Science II
C++ Object-Oriented Programming
Introduction to Classes
Pointers Revisited What is variable address, name, value?
Lecture 6 C++ Programming
Pointers Psst… over there.
This pointer, Dynamic memory allocation, Constructors and Destructor
Advanced Programming Behnam Hatami Fall 2017.
Pointers and References
Pointers Psst… over there.
Dynamic Memory Allocation
CSC 253 Lecture 8.
Object Oriented Programming COP3330 / CGS5409
Introduction to Classes
CSC 253 Lecture 8.
Chapter 15 Pointers, Dynamic Data, and Reference Types
Smart Pointers.
Pointers, Dynamic Data, and Reference Types
Brought to you by C++ Tutorial Brought to you by
Initialization List.
Built-In (a.k.a. Native) Types in C++
Reference Variables The symbol “&” has a few different purposes depending on where it occurs in code. When it appears in front of a variable name, it is.
Chapter 15 Pointers, Dynamic Data, and Reference Types
Chapter 8 Namespaces and Memory Realities
Variables Title slide variables.
Reference Parameters.
Dynamic Memory Allocation (and Multi-Dimensional Arrays)
Jordi Cortadella and Jordi Petit Department of Computer Science
How to use Strings (way to quick briefing)
CISC/CMPE320 - Prof. McLeod
Objects Managing a Resource
Pointers and References
Pointers and References
Destructors, Copy Constructors & Copy Assignment Operators
Pointers, Dynamic Data, and Reference Types
Automating Memory Management
Destructors, Copy Constructors & Copy Assignment Operators
SPL – PS2 C++ Memory Handling.
Initialization List.
Presentation transcript:

Workshop 1++: A bit of C++ Wouter van Toll Path Planning course September 17, 2015

Workshop 1++: A bit of C++ C++ at first sight Industry standard May feel old-fashioned Separate header files Not much is built in (e.g. visualization, geometry) Manage memory yourself (to some extent) Easy to make mistakes Powerful if you know what you’re doing Could be more efficient than other languages Improvements Smarter compilers Standard libraries (STL): std::vector, std::map, iterators I’ll name a few differences to e.g. C# / Java October 22, 2019 Workshop 1++: A bit of C++

Stack vs. heap October 22, 2019

Stack vs. heap Two ways to create/manage objects in memory Stack: Thing t(1,2); Creates a Thing. t is destroyed when out of scope Heap: Thing* t = new Thing(1,2); Creates a Thing. t points to its memory address Pointer can go out of scope; object stays in memory You need to delete the object yourself at some point Main pro: efficiency, control Objects can share pointers Pointers can be set to nullptr Main con: memory management Undeleted objects: Pointers to deleted objects memory leaks October 22, 2019 Workshop 1B: C++

Pointers and references Pointers to stack objects Thing t(1,2); Thing* ptr = &t; ptr->doSomething(); Or you can use references Thing t(1,2); Thing& ref = t; ref.doSomething(); References to heap objects Thing* t = new Thing(1,2); Thing& ref = *t; Thing ref = *t; Some differences Pointers can be null, references cannot std::vector<Thing*> is allowed, std:: vector<Thing&> is not * = pointer &t = “memory address of t” & = reference *t = “the Thing to which t points” Creates a copy of t! October 22, 2019 Workshop 1B: C++

const and functions Objects are copied unless you say otherwise* Let’s say class X stores a Point Point X::getLocation() { return this->p; } Point& X::getLocation() { ... } const Point& X::getLocation() { ... } const Point& X::getLocation() const { ... } Point& X::getLocation() const { ... } Point p = x.getLocation(); Point& p = x.getLocation(); const Point& p = x.getLocation(); Point p( x.getLocation() ); const Point& p = average(p1,p2); const Point p = average(p1,p2); Point& p = average(p1,p2); Returns a copy of the point Returns a mutable reference Returns a non-mutable reference Promises that X won’t change Compile error (why?) Copies the point (again?)* OK? Depends on the function No copy; p cannot be changed No copy; p can be changed Possibly equivalent nowadays Compile error/warning (why?) October 22, 2019 Workshop 1B: C++

const and parameters Pass object parameters as references to prevent copying void computeNewVelocity(Character& dude) { ... } const determines which parameters can change Point average(const Point& a, const Point& b) { ... } Non-const is useful for C-style “out parameters” void average(const Point& a, const Point& b, Point& result) { ... } Sometimes more efficient, but often less intuitive I only use out-parameters if I need multiple return values I use const a lot Makes you think about what does what. No side effects. Many languages don’t have it (why?) Many students don’t like it (why?) You could hack your way around it October 22, 2019 Workshop 1B: C++

The final slide Questions? Remarks? October 22, 2019 Workshop 1B: C++