A Computer Science Tapestry 12.1 Pointers, Memory, Abstractions l A pointer is the a variable/value that is a memory address  Addresses like 1, 2, 3,

Slides:



Advertisements
Similar presentations
Chapter 17 vector and Free Store Bjarne Stroustrup
Advertisements

Chapter 17 vector and Free Store John Keyser’s Modifications of Slides By Bjarne Stroustrup
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
Informática II Prof. Dr. Gustavo Patiño MJ
Engineering Problem Solving With C++ An Object Based Approach Chapter 9 Pointers and Creating Data Structures.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
Pointers Ethan Cerami Fundamentals of Computer New York University.
Pointers A pointer is a variable that contains memory address as its value. A variable directly contains a specific value. A pointer contains an address.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
1 Array, Pointer and Reference ( I ) Ying Wu Electrical Engineering and Computer Science Northwestern University EECS 230 Lectures.
1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Programming Pointers. COMP104 Lecture 32 / Slide 2 Pointers l Pointers are objects whose values are the locations of other objects l Pointers are memory.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Pointers CS362. Pointers A Pointer is a variable that can hold a memory address Pointers can be used to: Indirectly reference existing variables (sometimes.
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
CS204 – Advanced Programming Pointers and Linked Lists Part 1: Basics.
Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location. This is.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
A Computer Science Tapestry 12.1 Tools for programming l Syntatic details of control and variables  If, while, switch, function calls  Scope and lifetime.
1 Pointers Arrays have a disadvantage: Their size must be known at compile time. We would like the capability to allocate an array-like object of any needed.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
Pointers OVERVIEW.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
1 Pointers and Strings Chapter 5 2 What You Will Learn...  How to use pointers Passing arguments to functions with pointers See relationship of pointers.
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
 2000 Deitel & Associates, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close.
1 Workin’ with Pointas An exercise in destroying your computer.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 3 – August 28, 2001.
Review 1 List Data Structure List operations List Implementation Array Linked List.
C++ Programming Lecture 17 Pointers – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Pointers *, &, array similarities, functions, sizeof.
Welcome to CISC220H Data Structures in C++ sakai.udel.edu Office Hours: Mon/Wed 3:30PM - 4:30PM TA: Adnan Ozsoy.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
+ Pointers. + Content Address of operator (&) Pointers Pointers and array.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 2 – August 23, 2001.
Pointer. lvalues In C++, any expression that refers to an internal memory location is called an lvalue Appear on left side of assignment statement e.g.
Revision on C++ Pointers TCP1201: 2013/2014. Pointer Basics  Why pointer is important? 1. Reference/Point to existing data without cloning the data.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Cop3530sp12. Parameter passing call by value- appropriate for small objects that should not be altered by the function call by constant reference- appropriate.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
Struct s (7.4) Used as data aggregates for an entity can be different types of data e.g. for student id, name, GPA, address,... Similar to classes, but.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
CPS What's a pointer, why good, why bad? l Pointer is a memory address, it's an indirect reference to memory or an object.  Rather than say we.
Welcome to CISC220 Data Structures in C++ sakai.udel.edu Office Hours: Tues 3PM - 4PM / Thurs 1PM - 2PM TA: David.
P OINTERS A pointer is an address All data is stored in memory in some location that is indexed with an address Can refer to variables by name or by memory.
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.
Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
Pointers.
What's a pointer, why good, why bad?
Pointer.
Pointers Psst… over there.
Andy Wang Object Oriented Programming in C++ COP 3330
Pointers Psst… over there.
Pointer Basics Psst… over there.
Pointers, Dynamic Data, and Reference Types
Pointer Basics Psst… over there.
Pointers, Memory, Abstractions
Pointers, Dynamic Data, and Reference Types
SPL – PS2 C++ Memory Handling.
Presentation transcript:

A Computer Science Tapestry 12.1 Pointers, Memory, Abstractions l A pointer is the a variable/value that is a memory address  Addresses like 1, 2, 3, …, 0x0024ab03 Hexadecimal or base-16 digit represents 4 bits Character is 8 bits, integer is 32 bits  Every variable is stored somewhere in memory, typically we can ignore where double x = 32.6; int y = 18; string s = "hello";  The string variable s is actually the same size as int y Storage for the letters is elsewhere, string references it l What about a permanent, but forwardable address? l Other kinds of references ? "hello" 0x00 0x08 0x0c 0x??

A Computer Science Tapestry 12.2 Pointers l Using pointers presents some of the same problems as being Spiderman  With great power comes great responsibility…  New, more ugly, and harder to detect bugs l Why pointers?  Allow different sections of code to share data easily  Enable linked data structures l The BIG pointer concepts  Pointer declaration  NULL (0) pointer  BAD (uninitialized) pointers  Memory allocation from heap  Pointer dereference  Pointer assignment

A Computer Science Tapestry 12.3

A Computer Science Tapestry 12.4 Binky Code void main() { int *x; // Allocate the pointers x and y int *y; // but not the _pointee_ x = new int; *x = 42; // Dereference x - store 42 in pointee *y = 13; // CRASH! y = x; // Pointer assignment *y = 13; // Store 13 in its (shared) pointee }

A Computer Science Tapestry 12.5 What's a pointer, why good, why bad? l Pointer is a memory address, it's an indirect reference to memory or an object.  Rather than say we have an int, we say we have a pointer to an int  If x is an int, xptr can be a pointer to x Same thing works with Date, Dice, Student, … Not much use to have pointer to int, but pointer to class!! l Pointers force us to think about the machine and memory  Knowledge is powerful, freedom from it liberating l Pointers allow us to work at a lower level, but permit inheritance and a higher level of design/programming  Built-in array and tvector, C-style string and

A Computer Science Tapestry 12.6 Pointer example l new, dereference, selector operator, copy semantics CD c1("Beatles", "Rubber Soul", 1965); CD c2("Nirvana", "Nevermind", 1991); CD * c3 = new CD("REM", "Reveal", 2001); CD * c4; // what is the value of c4? CD c5; // what is the value of c5? cout << c1.title() << endl; cout title() << endl; cout << (*c3).title() << endl; c5 = c2; c2.changeTitle("Incesticide"); cout << c5.title() << endl; c4 = c3; c3->changeTitle("Out of Time"); cout title() << endl; What happens if we print c4->title() on first line? Why?

A Computer Science Tapestry 12.7 What's the point? l What's the difference between a vector of Dates and a vector of pointers to Dates? What about Courses, Students, etc.? tvector tv(1000); tvector tvp(1000);  Which takes up more space? What are values in vectors?  What happens when we write tv[0] = tv[2]; // if we change tv[2], affect tv[0]? tvp[0] = tvp[3]; // change *(tvp[3]), affect tvp[0], *tvp[0]? l Consider example of sorting by both name and age  Should we have two vectors of students?  Should we have two vectors of student pointers?  Is there a reason to prefer one to the other?

A Computer Science Tapestry 12.8 Thinking about pointersort.cpp The class Group uses a tvector myList  What changes if this is tvector myList? Changes to Group::add Changes to Group::print  Other changes needed? l What if we want to sort by age to print, leaving original order the same (why would we want to do this?)  Use another vector, sort it differently  Why is another vector a good idea?  Could use vector of indexes Both are indirect references

A Computer Science Tapestry 12.9 Local memory is ephemeral… // TAB -- The Ampersand Bug function // Returns a pointer to an int int* TAB() { int temp; return(&temp); // return a pointer to the local int } void Victim() { int* ptr; ptr = TAB(); *ptr = 42; // Runtime error! Pointee local to TAB }

A Computer Science Tapestry The trouble with pointers l Local vs. Heap allocation l Another address of bug Dice * makeDie(int sides) { return new Dice(sides); Dice d(sides); } return &d; }  What about the code below with different versions? Dice * cube = makeDie(4); cout NumSides() << endl; l Pointer Advice  Always initialize pointer variables, 0/NULL or new 0/NULL means errors are reproduceable Possible to assign another pointer value too  Don't call new unless you want another object allocated