Slides:



Advertisements
Similar presentations
 Pointers, Arrays, Destructors Is this random stuff or are these somehow connected?
Advertisements

Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
Derived data types Dealing with data –Where the information is stored –What value is kept there –What kind of information is stored Address operator Pointers.
A pointer is the memory address of a variable. A memory address is a physical location within a system’s memory space. A pointer variable is variable used.
Data Structures (Second Part) Lecture 2 : Pointers Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
Introduction to Programming Lecture 39. Copy Constructor.
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
A C LOSER L OOK AT C LASSES 1. A SSIGNING O BJECTS One object can be assigned to another provided that both objects are of the same type. It is not sufficient.
Operator Overloading Fundamentals
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
CSE 332: C++ copy control II Copy Control (Part II) Review: copy control consists of 5 distinct operations –A copy constructor initializes an object by.
OBJECT ORIENTED PROGRAMMING Instructor: Rashi Garg Coordinator: Gaurav Saxena.
Chapter 15 Memory Management: Four main memory areas for a C++ program: Code: code for instructions, methods, etc. static data: Global variables (declared.
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.
A RRAYS, P OINTERS AND R EFERENCES 1. A RRAYS OF O BJECTS Arrays of objects of class can be declared just like other variables. class A{ … }; A ob[4];
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
1 Pointers & functions Pointers allow us to simulate pass by reference. void swap(int &a, int &b) { int temp = a; a = b; b = temp; } int main () { int.
CSE 2501 Review Declaring a variable allocates space for the type of datum it is to store int x; // allocates space for an int int *px; // allocates space.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Pointers Example Use int main() { int *x; int y; int z; y = 10; x = &y; y = 11; *x = 12; z = 15; x = &z; *x = 5; z = 8; printf(“%d %d %d\n”, *x, y, z);
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
C++ When do you need to go native to C, C with C#? Almost never if you are creating a new application, for which you have control to code every component.
Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.
March 6, 2014CS410 – Software Engineering Lecture #10: C++ Basics IV 1 Structure Pointer Operator For accessing members in structures and classes we have.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review Part-I.
Chapter 9 Pointers and Dynamic Arrays (9.1). Pointers A variables which holds the memory address for a variable of a specific type. Call-by-Reference.
CPSC 252 Dynamic Memory Allocation Page 1 Dynamic memory allocation Our first IntVector class has some serious limitations the capacity is fixed at MAX_SIZE.
Tracing through E01, question 9 – step 1 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include using namespace std; void mysteryFunction(int.
Object-Oriented Programming in C++ More examples of Association.
References and Pointers CS 244 Connect Speakers for this Presentation Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics,
Programming II Array of objects. this Using the this Pointer this Objects use the this pointer implicitly or explicitly. – this is – this is used implicitly.
Pointers Value, Address, and Pointer. Values and Addresses int x, y, z; y x z values of x,
1 Object-Oriented Programming Using C++ A tutorial for pointers.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
1 Classes classes and objects - from object-oriented programming point of view class declaration class class_name{ data members … methods (member functions)
1 Memory as byte array Pointers Arrays relationship to pointers Operator ‘new’ Operator ‘delete’ Copy ctor Assignment operator ‘this’ const pointer Allocating.
1 Linked Multiple Queues. 2 A real world example. Not in the book. Sometimes we have a fixed number of items that move around among a fixed set of queues.
Current Assignments Project 3 has been posted, due next Tuesday. Write a contact manager. Homework 6 will be posted this afternoon and will be due Friday.
Object Lifetimes and Dynamic Objects Lecture-7. Object Lifetimes and Dynamic Objects External (Global) Objects Persistent (in existence) throughout the.
Pointers. What Is Pointer l every variable has memory address char c=’y’; int i=2; address of variable i is 0022 l address can used to refer to this variable.
Motivation and Overview
Pointers Revisited What is variable address, name, value?
Dynamic Memory CSCE 121 J. Michael Moore.
Pointers Psst… over there.
Dynamically Allocated Memory
Basic notes on pointers in C
Static Data Member and Functions
Pointers Psst… over there.
Pointer Basics Psst… over there.
understanding memory usage by a c++ program
Pointers, Dynamic Data, and Reference Types
Dynamic Memory A whole heap of fun….
Return by Reference CSCE 121 J. Michael Moore.
Pointers & Functions.
Chapter 12 Pointers and Memory Management
Practical Session 4 Rule of 5 R-value reference Unique pointer
Summary: Abstract Data Type
Dynamic Memory A whole heap of fun….
Introduction to Programming
A simple function.
Pointers and dynamic objects
Pointers & Functions.
The Stack.
Pointer Basics Psst… over there.
Move Semantics CSCE 121.
Pointers and dynamic objects
More on C++ G Carl Evans.
Presentation transcript:

void f() { int x{0}; // Construction int y{x};// Copy Construction y = x; // Copy Assignment }// Destruction

class MyClass { /*... */ }; void f() { MyClass x{}; // Construction MyClass y{x};// Copy Construction y = x; // Copy Assignment } // Destruction

class MyClass { public: MyClass() { puts("Constructing"); } ~MyClass() { puts("Destroying"); } MyClass(MyClass const&) { puts("Copying"); } void operator=(MyClass const&) { puts("Assigning"); } };

class MyClass { /*... */ }; void f() { MyClass x{}; // Constructing MyClass y{x}; // Copying y = x; // Assigning }// y.~MyClass() // Destroying // x.~MyClass() // Destroying

class MyClass { /*... */ }; void f() { MyClass x; MyClass* p{&x}; // p is a pointer to x // *p is another name for x }

void f() { MyClass x; MyClass y; // Different objects have different addresses MyClass* px{&x}; // px points to x MyClass* py{&y}; // py points to y assert(px != py); }

void f() { MyClass* p{nullptr}; }

void f() { MyClass x{}; MyClass* px1{&x}; MyClass* px2{px1}; assert(px1 == px2); px1 = nullptr; assert(px1 != px2); }

// For value types, x and y are distinct int x = 0; int y = x; y = 42; // For reference types, x and y refer to the same object List x = new List (); List y = x; x.Add(1);

// x and *p denote the same object int x{0}; int* p{&x}; // Changes the value of x via *p *p = 42;

void f() { MyClass x{}; // Construction MyClass y{x}; // Copy Construction y = x; // Copy Assignment }// y.~MyClass() // Destruction // x.~MyClass() // Destruction Lifetime of y Lifetime of x

MyClass x{}; // Global variable void f() { static MyClass y{}; // Function-local static variable }

void f() { MyClass* p{new MyClass{}}; delete p; } Lifetime of *p

MyClass* f() { MyClass* p{new MyClass{}}; return p; } void g() { MyClass* p{ f() }; delete p; }

void f() { int a = 0; // Copy initialization int b(0); // Direct initialization int c{0}; // Brace initialization int d{}; }

int global_int; // Zero-initialized void f() { int local_int; // Not initialized }

void f() { int local_int{}; MyClass local_MyClass{}; }

std::vector v{ 1, 2, 3, 4, 5, 6 }; for (size_t i{0}; i != v.size(); ++i) { std::cout << v[i] << std::endl; }

std::vector v{ 1, 2, 3, 4, 5, 6 }; for (std::vector ::iterator it = v.begin(); it != v.end(); ++it) { std::cout << *it << std::endl; }

std::vector v{ 1, 2, 3, 4, 5, 6 }; for (auto it = v.begin(); it != v.end(); ++it) // std::vector ::iterator { std::cout << *it << std::endl; }

std::set s{ 1, 2, 3, 4, 5, 6 }; for (auto it = s.begin(); it != s.end(); ++it) // std::vector ::iterator not anymore! { std::cout << *it << std::endl; }

std::map m { { 1, 2 }, { 2, 4 }, { 3, 6 }, { 4, 8 }, { 5, 10 }, { 6, 12 } }; for (auto it = m.begin(); it != m.end(); ++it) { std::cout first second << std::endl; }

for (auto value : container) { std::cout << value << std::endl; } Called “range for” in C++

std::vector v{ 1, 2, 3, 4, 5, 6 }; for (auto it = v.begin(); it != v.begin() + 3; ++it) { std::cout << *it << std::endl; }

std::vector v{ 1, 2, 3, 4, 5, 6 }; v.erase(v.begin() + 3); // Removes the 4 v.insert(v.begin() + 2, 10); // Inserts a 10 after the 2 int const x{*(v.begin() + 1)}; // Gets the 2 *(v.begin() + 1) = 9; // Changes the 2 to a 9 int const y{*(v.begin() )}; // Invalid!

What about operations like sort and find? v.sort(); // Nope; doesn't exist v.find(4); // Nope; doesn't exist

std::vector v{ 1, 3, 5, 2, 4, 6 }; std::sort(v.begin(), v.end());

std::vector v{ 1, 3, 5, 2, 4, 6 }; auto const it{std::find(v.begin(), v.end(), 2)}; if (it != v.end()) { std::cout << "I found a two!" << std::endl; }

std::vector v{ 1, 2, 3, 4, 5, 6 }; if (std::binary_search(v.begin(), v.end(), 2)) { std::cout << "I found a two!" << std::endl; }

std::vector v{ 1, 2, 3, 4, 5, 6 }; std::for_each(v.begin(), v.end(), [](int const x) { std::cout << x << std::endl; })

std::vector v{ 1, 2, 3, 4, 5, 6 }; std::transform(v.begin(), v.end(), v.begin(), [](int const x) { return x * x; });

std::vector v{ 1, 2, 3, 4, 5, 6 }; std::vector w(v.size()); std::transform(v.begin(), v.end(), w.begin(), [](int const x) { return x * x; });

std::vector v{ 1, 2, 3, 4, 5, 6 }; std::vector w; std::transform(v.begin(), v.end(), std::back_inserter(w), [](int const x) { return x * x; });

std::vector v{ 1, 3, 5, 2, 4, 6 }; std::sort(v.begin(), v.end(), [](int const lhs, int const rhs) { return lhs > rhs; });

std::vector v{ 1, 2, 3, 4, 5, 6 }; bool const all_are_even(std::all_of(v.begin(), v.end(), [](int const x) { return x % 2 == 0; }));

std::vector v{ 1, 2, 3, 4, 5, 6 }; std::set s(v.begin(), v.end());

int a[6]{ 1, 3, 5, 2, 4, 6 }; std::sort(begin(a), end(a));

void f(int* const data, size_t const count) { int* const first{data}; int* const last {data + count}; std::sort(first, last); }

template bool all_of(Iterator const first, Iterator const last, Function const f) { for (Iterator it{first}; it != last; ++it) { if (!f(*it)) return false; } return true; }

void f() { MyClass* p{new MyClass{}}; delete p; }

void f() { char* const buffer{new char[1024 * 1024]{}}; //...use the buffer... delete[] buffer; }

void f() { char* const buffer{new char[1024 * 1024]{}}; //... throw std::runtime_error{"oops“}; //... delete[] buffer; }

void f() { char* const buffer{new char[1024 * 1024]{}}; //... return; //... delete[] buffer; }

void f() { // Not actual C++ char* const buffer{new char[1024 * 1024]{}}; try { //...use the buffer... } finally { delete[] buffer; }

void f() { // Not actual C++ using (char* const buffer{new char[1024 * 1024]{}}) { //...use the buffer... }

void f() { scoped_buffer buffer{new char[1024 * 1024]{}}; //...use the buffer... }// buffer.~scoped_buffer()

struct scoped_buffer { public: scoped_buffer(char* const p) : _p{p} { } ~scoped_buffer() { delete[] _p; } char* get() const { return _p; } private: scoped_buffer(scoped_buffer const&) = delete; void operator=(scoped_buffer const&) = delete; char* _p; };

void f() { scoped_buffer buffer{new char[1024 * 1024]{}}; //...use the buffer... }// buffer.~scoped_buffer()

void f(size_t const buffer_size) { std::unique_ptr const buffer{new char[buffer_size]{}}; //...code that uses the buffer... }

std::unique_ptr f(size_t const buffer_size) { std::unique_ptr buffer{new char[buffer_size]{}}; return buffer; }

std::mutex _sync; void f() { _sync.lock(); //...synchronized code... _sync.unlock(); }

void f() { std::unique_lock lock(_sync); //...synchronized code... }

void f(char const* const file_name) { FILE* file(fopen(file_name, "r")); //...code that uses the file... fclose(file); }

void f(char const* const file_name) { std::ifstream file(file_name); //...code that uses the file... }

void f() { int x{0}; // x is an object int& rx{x}; // rx is a reference to x // it is another name for x rx = 42;// Change the memory shared by x and rx std::cout << x << std::endl;// Prints 42 }

void f(std::vector & v) { //...Use the vector... }

void f() { int const x{0}; x = 42; // Invalid; x is const }

void f() { int x(0); int const& rx(x); int const* px(&x); x = 42; // Ok, x is not const rx = 42; // Invalid; rx is const *px = 42; // Invalid; *px is const }

void f(std::vector const& v) { //...Use the vector but can't modify it... }