James Tam Pointers In this section of notes you will learn about another type of variable that stores addresses rather than data.

Slides:



Advertisements
Similar presentations
James Tam Linked Lists in Pascal Linked Lists In this section of notes you will learn how to create and manage a dynamic list.
Advertisements

Chapter 6 Type Checking. The compiler should report an error if an operator is applied to an incompatible operand. Type checking can be performed without.
C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
1 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
POINTER Prepared by MMD, Edited by MSY1.  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference.
James Tam Arrays In this section of notes you will be introduced to a homogeneous composite type, one- dimensional arrays.
James Tam Records You will learn in this section of notes how to create a new, composite type, that can be composed of different types of elements.
James Tam Breaking Problems Down This section of notes shows you how to break down a large problem into smaller modules that are easier to implement and.
James Tam Breaking Problems Down This section of notes shows you how to break down a large programming problem into smaller modules that are easier to.
James Tam Pointers In this section of notes you will learn about another type of variable that stores addresses rather than data.
James Tam Arrays In this section of notes you will be introduced to a homogeneous composite type, one- dimensional arrays.
James Tam Making Decisions In Pascal In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
James Tam Arrays In this section of notes you will be introduced to a homogeneous composite type, one- dimensional arrays.
James Tam Introduction To Defining New Types in Pascal In this section of notes you how and why programmers can define new types.
James Tam Arrays In this section of notes you will be introduced to a homogeneous composite type arrays.
James Tam Making Decisions In Pascal In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
James Tam Intermediate Pascal to Java examples Pascal To Java: The Transition Documentation Variables and constants Advanced input and output Decision.
James Tam Arrays In this section of notes you will be introduced to a composite type where all elements must be of the same type (homogeneous): arrays.
James Tam Introduction To Files In Pascal In this section of notes you will learn how to read from and write to files in your Pascal programs.
James Tam Records You will learn in this section of notes how to create a new, composite type, that can be composed of different types of elements.
J. Michael Moore From James Tam’s material Multi-Dimensional Arrays CSCE 110.
James Tam Breaking Problems Down This section of notes shows you how to break down a large problem into smaller modules that are easier to implement and.
James Tam Records You will learn in this section of notes how to create a new, composite type, that can be composed of different types of elements.
J. Michael Moore Arrays CSCE 110 From James Tam’s material.
J. Michael Moore Modules: Getting information out CPSC 110 Influenced by material developed by James Tam & Jennifer Welch.
James Tam Records You will learn in this section of notes how to create a new, composite type, that can be composed of different types of elements.
James Tam Getting Started With Pascal Programming What is the basic structure of a Pascal Program Variables in Pascal Performing input and output with.
James Tam Arrays In this section of notes you will be introduced to a homogeneous composite type, one- dimensional arrays.
James Tam Arrays In this section of notes you will be introduced to a homogeneous composite type, one- dimensional arrays.
James Tam Breaking Problems Down This section of notes shows you how to break down a large problem into smaller modules that are easier to implement and.
James Tam Making Decisions In Pascal In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
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.
James Tam Records You will learn in this section of notes how to create a new, composite type, that can be composed of different types of elements.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
J. Michael Moore Modules – the Basics CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
Link Lists In this section of notes you will learn how to create and manage a dynamic list. Link Lists in Pascal.
J. Michael Moore Arrays CSCE 110. J. Michael Moore Typical (although simplified) Problem Write a program that will track student grades in a class. The.
James Tam Pointers In this section of notes you will learn about another type of variable that stores addresses rather than data.
Input and Output (IO) CSCE 110 Drawn from James Tam's material.
James Tam Pointers In this section of notes you will learn about a third type of variable that stores addresses rather than data.
J. Michael Moore Scope & Testing Modules CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
James Tam Breaking Problems Down This section of notes shows you how to break down a large problem into smaller modules that are easier to implement and.
James Tam Introduction To Files In Pascal In this section of notes you will learn how to read from and write to files in Pascal.
J. Michael Moore Modules – the Basics CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
James Tam Making Decisions In Pascal In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
James Tam Breaking Problems Down This section of notes shows you how to break down a large problem into smaller modules that are easier to implement and.
1 Contents. 2 Run-Time Storage Organization 3 Static Allocation In many early languages, notably assembly and FORTRAN, all storage allocation is static.
Review of C++ Programming Part II Sheng-Fang Huang.
Runtime Environments Compiler Construction Chapter 7.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Chapter 7: Pointers Basic concept of pointers Pointer declaration Pointer operator (& and *) Parameter passing 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.
Chapter 13: Structures. In this chapter you will learn about: – Single structures – Arrays of structures – Structures as function arguments – Linked lists.
Review 1 List Data Structure List operations List Implementation Array Linked List.
Functions & Pointers in C Jordan Erenrich
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
ISBN Chapter 10 Implementing Subprograms.
POINTERS IN C. Introduction  A pointer is a variable that holds a memory address  This address is the location of another object (typically another.
You learned how to declare pointer variables how to store the address of a variable into a pointer variable of the same type as the variable how to manipulate.
History of Computing – Pascal
Dynamic Memory Allocation
CSC 253 Lecture 8.
Pascal Prof. Steven A. Demurjian
CSC 253 Lecture 8.
Alternate Version of STARTING OUT WITH C++ 4th Edition
Pascal Subprogram Procedure Function Build in Function (e.g. Sin(x))
Arrays In this section of notes you will be introduced to a homogeneous composite type, one-dimensional arrays One-dimensional arrays in Pascal.
Arrays In this section of notes you will be introduced to a composite type where all elements must be of the same type (homogeneous): arrays Homogeneous.
A simple function.
Presentation transcript:

James Tam Pointers In this section of notes you will learn about another type of variable that stores addresses rather than data

James Tam Types Of Variables Pascal Variables a. Simple (atomic) b. Aggregate (composite) integercharbooleanreal Homogenous (arrays) Heterogeneous (records) 1.Data

James Tam Types Of Variables Pascal Variables a. Simple (atomic) b. Aggregate (composite) integercharbooleanreal Homogenous (arrays) Heterogeneous (records) 1.Data 2. Addresses (pointers)

James Tam Declaration Of Pointer Variables Format: type type name = ^ type pointed to 1 ; : : begin var pointer name : type name; Example: type IntegerPointer = ^integer; : : begin var numPtr1, numPtr2 : IntegerPointer; 1 An alternative is to use the instead of the “up-arrow” ^ to declare a pointer variable (not recommended)

James Tam Allocating Memory For Pointers Static vs. dynamic memory arrays Allocating dynamic memory Reserving some dynamic memory and having the pointer point to it. Format new (pointer name); Example new (numPtr1);

James Tam De-allocating Memory For Pointers De-allocating memory Returning back the dynamically allocated memory Format dispose (pointer name); Example dispose (numPtr1);

James Tam De-allocating Memory For Pointers: Followup Should also be followed by a statement so that the pointer no longer points to the de-allocated memory. Format: pointer name := NIL; Example numPtr1 := NIL;

James Tam Using Pointers Important! Are you dealing with the pointer or what the pointer is pointing to (allocated memory)? Pointer name Pointer name ^ (de-reference pointer)

James Tam Using Pointers Important! Are you dealing with the pointer or what the pointer is pointing to (allocated memory)? Pointer name Pointer name ^ (de-reference pointer) pointer X variable

James Tam Accessing Pointers Format: (Pointer) pointer name (Memory pointed to) pointer name ^ Example: (Pointer) writeln(numPtr2); (Memory pointed to) writeln(numPtr1^);

James Tam Accessing Pointers Format: (Pointer) pointer name (Memory pointed to) pointer name ^ Example: (Pointer) writeln(numPtr2); (Memory pointed to) writeln(numPtr1^);

James Tam Using Pointers : Allowable Operations Assignment := Relational Equity = Inequality <>

James Tam Using Pointers : Assignment Format: (Pointer) pointer name := pointer name; (Memory pointed to) pointer name ^ := expression; Example: (Pointer) numPtr1 := numPtr2; (Memory pointed to) numPtr1^ := 100;

James Tam Using Pointers : Allowable Operations (Equality) Format: (Pointer) if (pointer name 1 = pointer name 2) then (Memory pointed to) if (pointer name 1 ^ = pointer name 2 ^) then Example: (Pointer) if (numPtr1 := numPtr2) then (Memory pointed to) if (numPtr1 ^ := numPtr2 ^) then

James Tam Using Pointers : Allowable Operations (Inequality) Format: (Pointer) if (pointer name 1 <> pointer name 2) then (Memory pointed to) if (pointer name 1 ^ <> pointer name 2 ^) then Example: (Pointer) if (numPtr1 <> numPtr2) then (Memory pointed to) if (numPtr1 ^ <> numPtr2 ^) then

James Tam Pointers : First Example A full version of this example can be found in Unix under: /home/231/examples/pointers/pointer1.p program pointer1 (output); type IntegerPointer = ^integer; : : begin var num, temp : integer; var numPtr1, numPtr2 : IntegerPointer; writeln('Example One'); num := 10; new(numPtr1); new(numPtr2); numPtr1^ := 100; numPtr2^ := 100;

James Tam Pointers : First Example (2) writeln('num = ':11, num:3); writeln('numPtr1^ = ':11, numPtr1^:3); writeln('numPtr2^ = ':11, numPtr2^:3); if (numPtr1 = numPtr2) then writeln('numPtr1 and numPtr2 point to the same location in memory') else writeln('numPtr1 and numPtr2 point to two separate locations'); if (numPtr1 ^= numPtr2^) then writeln('The data pointed to by numPtr1 and numPtr2 are equal.') else writeln('The data pointed to by numPtr1 and numPtr2 are not equal.'); writeln('Example two'); temp := num; num := numPtr1^; writeln('num = ':11, num:3); writeln('numPtr1^ = ':11, numPtr1^:3);

James Tam Pointers: First Example (3) writeln('Example three'); numPtr1^ := num; num := 2; writeln('num = ':11, num:3); writeln('numPtr1^ = ':11, numPtr1^:3); writeln('Example four'); numPtr2 ^ := 66; numPtr1 := numPtr2; if (numPtr1 = numPtr2) then writeln('numPtr1 and numPtr2 point to the same location in memory') else writeln('numPtr1 and numPtr2 point to two separate locations'); numPtr2^ := 33; writeln('numPtr1^ = ':11, numPtr1^); writeln('numPtr2^ = ':11, numPtr2^);

James Tam Pointers: First Example (4) dispose(numPtr1); dispose(numPtr2); numPtr1 := NIL; numPtr2 := NIL;

James Tam Pointers As Value Parameters Need to define a type for the pointer first! Example (defining type) type CharPointer = ^char; Format (passing pointer): procedure procedure name (pointer name (1) : type of pointer (1); pointer name (2) : type of pointer (1); : : pointer name (n) : type of pointer (n)); function function name (pointer name (1) : type of pointer (1); pointer name (2) : type of pointer (1); : : pointer name (n) : type of pointer (n));

James Tam Pointers As Value Parameters (2) Example (passing pointer): procedure proc1 (charPtr : CharPointer);

James Tam Pointers As Variable Parameters Need to define a type for the pointer first! Example (defining type) type CharPointer = ^char; Format (passing pointer): procedure procedure name (var pointer name (1) : type of pointer (1); var pointer name (2) : type of pointer (1); : : var pointer name (n) : type of pointer (n)); Example (Passing pointer): procedure proc2 (var charPtr : CharPointer);

James Tam Pointers: Second Example A full version of this program can be found in Unix under: /home/231/examples/pointers/pointer2.p program pointer2 (output); type CharPointer = ^char; procedure proc1 (charPtr : CharPointer); var temp : CharPointer; begin writeln; writeln('In procedure proc1'); new(temp); temp^ := 'A'; charPtr := temp; writeln('temp^ = ', temp^); writeln('charPtr^ = ', charPtr^); end;

James Tam Pointers: Second Example (2) procedure proc2 (var charPtr : CharPointer); var temp : CharPointer; begin writeln; writeln('In procedure proc2'); new(temp); temp^ := 'A'; charPtr := temp; writeln('temp^ = ', temp^); writeln('charPtr^ = ', charPtr^); end;

James Tam Pointers: Second Example (4) begin(* Main program *) var charPtr : CharPointer; new (charPtr); charPtr^ := 'a'; writeln; writeln('In the main program.'); writeln('charPtr^ = ', charPtr^); proc1(charPtr); writeln('After proc1'); writeln('charPtr^ = ', charPtr^); proc2(charPtr); writeln('After proc2'); writeln('charPtr^ = ', charPtr^); writeln; end.(* End of main program *)

James Tam You Should Now Know How to declare new types that are pointers to data How to declare variables that are pointers The difference between static and dynamically allocated memory How to dynamically allocate memory How to de-allocate memory Why and when to set pointers to NIL How to access a pointer and how to access what the pointer points to How to assign values to a pointer and how to assign values to what the pointer points to What operations can be performed on pointers and how does each one work How to pass pointers as value and variable parameters