ITEC 320 Lecture 11 Pointers(1). Pointers Review Packages –Generic –Child Homework 3 is posted.

Slides:



Advertisements
Similar presentations
Chapter 6 Data Types
Advertisements

ITEC 320 Lecture 3 In-class coding / Arrays. Arrays Review Strings –Advantages / Disadvantages Input –What two methods are used? Conditionals Looping.
Various languages….  Could affect performance  Could affect reliability  Could affect language choice.
Pointers Revisited l What is variable address, name, value? l What is a pointer? l How is a pointer declared? l What is address-of (reference) and dereference.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
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.
Run-Time Storage Organization
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
Memory and C++ Pointers.  C++ objects and memory  C++ primitive types and memory  Note: “primitive types” = int, long, float, double, char, … January.
1 C++ Pointers Gordon College. 2 Regular variables Regular variables declared –Memory allocated for value of specified type –Variable name associated.
Memory Layout C and Data Structures Baojian Hua
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
ITEC 320 Lecture 16 Packages (1). Review Questions? –HW –Exam Nested records –Benefits –Downsides.
CS3012: Formal Languages and Compilers The Runtime Environment After the analysis phases are complete, the compiler must generate executable code. The.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
February 11, 2005 More Pointers 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.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
CS 11 C++ track: lecture 4 Today: More on memory management the stack and the heap inline functions structs vs. classes.
ITEC 320 Lecture 10 Packages (2). Review Packages –What parts do they have? –Syntax?
Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.
Basic Semantics Associating meaning with language entities.
1 Records Record aggregate of data elements –Possibly heterogeneous –Elements/slots are identified by names –Elements in same fixed order in all records.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 4 Pointers and Dynamic Arrays Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
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 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
Pointers in C++. 7a-2 Pointers "pointer" is a basic type like int or double value of a pointer variable contains the location, or address in memory, of.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Chapter 7 Pointers: Java does not have pointers. Used for dynamic memory allocation.
1 CS Programming Languages Class 09 September 21, 2000.
ISBN Chapter 6 Data Types Pointer Types Reference Types Memory Management.
C Programming Lecture 16 Pointers. Pointers b A pointer is simply a variable that, like other variables, provides a name for a location (address) in memory.
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
Objects and Variables Local variables – Confined to single context: allocated on stack – Primitive types such as int or object references – Must be initialized.
ITEC 320 Lecture 9 Nested records / Packages. Review Project ?’s Records Exam 1 next Friday.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
ISBN Chapter 12 Support for Object-Oriented Programming.
Memory Management in Java Mr. Gerb Computer Science 4.
Objects and Memory Mehdi Einali Advanced Programming in Java 1.
Records type city is record -- Ada Name: String (1..10); Country : String (1..20); Population: integer; Capital : Boolean; end record; struct city { --
Data Types Chapter 6: Data Types Lectures # 13. Topics Chapter 6: Data Types 2 Introduction Primitive Data Types Character String Types Array Types Associative.
C11, Implications of Inheritance
CSC 533: Programming Languages Spring 2016
Object Lifetime and Pointers
Storage Allocation Mechanisms
Dynamic Storage Allocation
Data Types In Text: Chapter 6.
CSC 533: Programming Languages Spring 2015
Overview 4 major memory segments Key differences from Java stack
Concepts of programming languages
Checking Memory Management
Pointers Revisited What is variable address, name, value?
Dynamic Memory CSCE 121 J. Michael Moore.
Dynamically Allocated Memory
Dynamic Memory Allocation
CSC 253 Lecture 8.
Complex Data Types One very important measure of the “goodness” of a PL is the capability of its data types to model the problem space variables Design.
CSC 253 Lecture 8.
Overview 4 major memory segments Key differences from Java stack
Dynamic Memory.
RUN-TIME STORAGE Chuen-Liang Chen Department of Computer Science
Dynamic Memory CSCE 121.
CSC 533: Programming Languages Spring 2019
Run-time environments
Presentation transcript:

ITEC 320 Lecture 11 Pointers(1)

Pointers Review Packages –Generic –Child Homework 3 is posted

Pointers Outline Pointers What? Why? Relationship to Java Examples

Pointers Visualizatio n versus Your iPhone is waiting for pickup at 123 Computer Lane. Your pickup code is

Pointers Technical Definition A pointer contains the memory address of where a particular value is stored A variable contains the particular value A reference allows access to a particular value at a particular memory address Issues –How to access the value (de-referencing) –Typing Relationship to hardware

Pointers Currently class Box { int l; int w; } class Client { public static void main() { Box p; -- A local variable p = new Box(); p.l := 3; p.w := 4; System.out.println(p.l + p.w); Box q = null; -- What if no initialization? System.out.println (q.l); -- What happens here? }

Pointers Ada procedure boxexample is type Box is record l, w: Integer := 0; end record; b1, b2: Box; begin b1.l := 11; b1.w := 22; b2 := (33, 44); if b1 = b2 then put_line("Same"); else put_line("Different"); end if; end boxexample;

Pointers Ada procedure boxptrexample1 is type Box is record l, w: Integer := 0; end record; -- A type for pointers to boxes type BoxPtr is access Box; b1: Box; p1: BoxPtr; begin b1 := (11, 22); p1 := new Box; -- can do new Box’(1,2) as well p1.all := (33, 44); if b1 = p1.all then put_line("Same"); else put_line("Different"); end if; end boxptrexample1

Pointers Deallocation New creates memory What happens when you are finished with it? Call the dispose () procedure (make sure var deleting is in out or local) –Have to create it unfortunately with Ada.Unchecked_Deallocation; procedure dispose is new Ada.Unchecked_Deallocation( Object => Box, -- The type of object being deallocated Name => BoxPtr -- The type of pointer being followed );

Pointers Comparison s Similarities –Both refer to values allocated with new –Strongly typed –NULL points to invalid data –Refers to addresses –Create garbage –Implicitly dereference

Pointers Difference s Separate types for pointer and values Can access both pointer and value Point to stack and heap Pointer arithmetic (requires type checking to be turned off) No automatic garbage collection Dangling references Implicit / Explicit dereferencing

Pointers Mixing To set a pointer to regular data requires changes with ada.text_io; use ada.text_io; with ada.integer_text_io; use ada.integer_text_io; procedure pointer is type IP is access all Integer; ref: IP; x : aliased Integer := 5; begin ref := x'Access; put(ref.all); end pointer; Power user mode

Pointers Rationale Why do you think languages have pointers / references? Memory management Compile time benefits –You know what size references are… –Class a reference points to can be any size

Pointers Error type Box is... type BoxPtr is access Box; b1: Box := (2, 3); p1: BoxPtr; begin p1.all := b1; put(p1.all.w); What is wrong with this?

Pointers Memory Managemen t Java –How does it work? Ada –Explicitly create and release memory –Speed concerns –Ways around it

Pointers Memory Leak Java Foo f; f = new Foo(); Ada declare f: Foo; begin f = new Foo; How do you fix this? Why is this normal Java?

Pointers Problems Dereference a NULL pointer Aliases –2 pointers pointing to same data Dangling pointer –Pointing to something that has been disposed Creating garbage / memory link

Pointers Review Pointers Next time –Linked list of integers –Updating linked list to be generic