25-Aug-14 Pointers and References. Machine addresses Computer memory consists of one long list of addressable bytes A pointer is a data item that contains.

Slides:



Advertisements
Similar presentations
UNIT 9: Pointers Data Variable and Pointer Variable Pass by Reference
Advertisements

Chapter 10 Pointers and Dynamic Arrays. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Pointers Pointer variables.
Introduction to Linked Lists In your previous programming course, you saw how data is organized and processed sequentially using an array. You probably.
COL 106 Shweta Agrawal and Amit Kumar
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Introduction to Computer Programming in C
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
1 Chapter 3 Arrays, Linked Lists, and Recursion. 2 Static vs. Dynamic Structures A static data structure has a fixed size This meaning is different than.
Topic 9 – Introduction To Arrays. CISC105 – Topic 9 Introduction to Data Structures Thus far, we have seen “simple” data types. These refers to a single.
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Pointer. Warning! Dangerous Curves C (and C++) have just about the most powerful, flexible and dangerous pointers in the world. –Most other languages.
Introduction and a Review of Basic Concepts
Trees. Definition of a tree A tree is like a binary tree, except that a node may have any number of children –Depending on the needs of the program, the.
Object References. Objects An array is a collection of values, all of the same type An object is a collection of values, which may be of different types.
C pointers (Reek, Ch. 6) 1CS 3090: Safety Critical Programming in C.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Odds and Ends Strings (from Chapter 9) StringTokenizer.
Adapted from Dr. Craig Chase, The University of Texas at Austin.
Chapter 10Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Announcements/Reminders l Project 6 due on Thursday March 31 l Exam.
Stacks, Queues, and Deques
Stacks, Queues, and Deques. A stack is a last in, first out (LIFO) data structure –Items are removed from a stack in the reverse order from the way they.
Stacks, Queues, and Deques
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
Binary Search Trees. BST Properties Have all properties of binary tree Items in left subtree are smaller than items in any node Items in right subtree.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
Data Objects (revisited) Recall that values are stored in data objects, and that each data object holds one value of a particular type. Data objects may.
Lecture 2 Object Oriented Programming Basics of Java Language MBY.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Storage Management. The stack and the heap Dynamic storage allocation refers to allocating space for variables at run time Most modern languages support.
Pointers OVERVIEW.
Data structures Abstract data types Java classes for Data structures and ADTs.
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.
1 Jeff Edmonds York University COSC 2011 Lecture 2 Abstract Positions/Pointers Positions in an Array Pointers in C References in Java Implementing Positions.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Generics and Collections Course Lecture Slides 19 th July 2010 “Never.
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Tree Data Structures. Heaps for searching Search in a heap? Search in a heap? Would have to look at root Would have to look at root If search item smaller.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
Announcements There is a Quiz today. There were problems with grading assignment 2, but they should be worked out today The web page for correcting the.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Windows Programming Lecture 03. Pointers and Arrays.
Recap Resizing the Vector Push_back function Parameters passing Mechanism Primitive Arrays of Constants Multidimensional Arrays The Standard Library string.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Computer Organization and Design Pointers, Arrays and Strings in C
Java Review: Reference Types
Pointers and References
Introduction to Pointers
Binary Trees, Binary Search Trees
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Introduction to Pointers
Pointers and References
Pointers and References
Dynamic Data Structures and Generics
Dynamic Data Structures and Generics
CS2013 Lecture 7 John Hurley Cal State LA.
ENERGY 211 / CME 211 Lecture 10 October 13, 2008.
Introduction to Pointers
Presentation transcript:

25-Aug-14 Pointers and References

Machine addresses Computer memory consists of one long list of addressable bytes A pointer is a data item that contains an address 3FA71CF2 3FA71CF3 3FA71CF4 3FA71CF5 3FA71CF6 3FA71CF7 3FA71CF8 3FA71CF9 3FA71CFA 3FA71CFB 3FA71CFC 3FA71CFD 3FA71CFE 3FA71CFF 3FA71D00 3FA71D01 : : 3FA71CF6 A reference is a data item that contains an address 3FA71CF6 C has pointers, but Java has references So what’s the difference?

C (and C++) vs. Java In C you can dereference (follow) a pointer In Java you can dereference (follow) a reference In C you can assign one pointer variable to another In Java you can assign one reference variable to another In C you can determine whether one pointer is larger than, equal to, or smaller than another pointer In Java you can determine whether one reference is equal to another reference In C you can create a pointer to anything In Java you can have references to objects In C you can do integer arithmetic on pointers

References Recall that an Abstract Data Type (ADT) has a set of values and a set of operations on those values Pointers and references have the same set of values (memory addresses) Pointers have more defined operations than references Pointers are more flexible and more general than references References are safer than pointers (from error or malicious misuse) References allow automatic garbage collection (pointers don’t) A (non-abstract) Data Type also has an implementation The implementations of pointers and references are similar Java references carry information about the thing referenced; in C, it’s up to the compiler to figure out what it can

Data structures Basically, pointers and references are the same thing; they point to (refer to) something else in memory A Data Structure is a description of how data is organized in memory Many (not all) data structures are built from objects pointing/referring to one another Understanding pointers (references) is fundamental to this course If this course were taught in C or C++ instead of Java, all the “nuts and bolts” would be the same This course is in Java, but it’s not about Java You need to know how to create your own data structures I will also teach some Java-specific packages In real life, it’s stupid to redo work that’s already been done for you

A trivial example We use a lot of references in Java: class Person { String name; Person spouse; Person (String n) { name = n; } } … Person john = new Person("John"); Person mary = new Person("Mary"); john.spouse = mary; mary.spouse = john; "John" "Mary" john name spouse mary name spouse

A more serious example A binary tree is a data structure in which every node (object) has zero, one, or two children (references to other nodes) Arithmetic expressions can be represented as binary trees To evaluate an arithmetic expression: If it is a leaf, return its value Otherwise, evaluate its two subtrees, and perform the indicated operation + / 12 null 4 7 A binary tree representing the arithmetic expression 12/4+7

Binary trees in Java public class BinaryTree { public Object value; // the information in this node private BinaryTree leftChild; private BinaryTree rightChild; // Constructors and methods... } To make binary trees as useful as possible, we make the value in a node an Object As implementers of the BinaryTree class, our job is to make sure that the structure of the binary tree is always valid We don’t really care what the user puts in the value field

Size of objects Objects in Java have, generally speaking, a fixed size The size of all primitives is known Objects don’t actually “contain” other objects—they just have references to other objects, and a reference is 4 bytes So what about Vectors? A Vector is like an array, but it gets bigger as you put things into it A Vector actually has two “sizes”—its capacity, which is how many references it can hold, and its size, which is how many references are in it right now When you exceed the capacity of a Vector, Java creates a new Vector for you

The magic of Vectors Suppose you have two references to a Vector, and you use one of them to add elements to the Vector What happens if Java decides to replace this Vector with a bigger one? It looks like the second reference is a “dangling pointer,” referring to nothing This doesn’t happen! Java protects you from this error But how? v1 v2

Vector’s secret trick A reference to a Vector is actually a reference to a reference to a Vector In this way, the “real” reference has to be changed in only one place, and all the other, indirect references automatically work It’s clever, but it isn’t magic v2 v1

The End