UEE1302 (1102) F10 Introduction to Computers and Programming (I)

Similar documents
Chapter 10 Pointers and Dynamic Arrays. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo

Chapter 10. Pointers and Dynamic Arrays. Copyright 2016 Pearson, Inc. All rights reserved.

Chapter 9. Pointers and Dynamic Arrays

Pointers and Dynamic Arrays

In this chapter, you will learn about: Pointers. Dynamic Arrays. Introduction Computer Science 1 CS 23021

Chapter Overview. Pointers and Dynamic Arrays. Pointers. Pointers. Declaring Pointers. Pointers Tell Where To Find A Variable. 9.

What have we learned about when we learned about function parameters? 1-1

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

UEE1302 (1102) F10 Introduction to Computers and Programming (I)

Homework #3 CS2255 Fall 2012

Pointers. Variable Declaration. Chapter 10

Pointers, Dynamic Data, and Reference Types

CSC 270 Survey of Programming Languages. What is a Pointer?

Lab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

Programming in C/C Lecture 2

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology

Pointers. Addresses in Memory. Exam 1 on July 18, :00-11:40am

UEE1302 (1102) F10: Introduction to Computers and Programming

Scott Gibson. Pointers & Dynamic Memory. Pre & Co Requisites. Random Access Memory. Data Types. Atomic Type Sizes


CS2255 HOMEWORK #1 Fall 2012

Pointers. A pointer value is the address of the first byte of the pointed object in the memory. A pointer does not know how many bytes it points to.

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

Exam 3 Chapters 7 & 9

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

DYNAMIC ARRAYS; FUNCTIONS & POINTERS; SHALLOW VS DEEP COPY

CS201- Introduction to Programming Current Quizzes

CS162 - POINTERS. Lecture: Pointers and Dynamic Memory

! Pass by value: when an argument is passed to a. ! It is implemented using variable initialization. ! Changes to the parameter in the function body

Chapter 9: Pointers Co C pyr py igh i t gh Pear ea so s n n E ducat ca io i n, n Inc. n c.

! The address operator (&) returns the address of a. ! Pointer: a variable that stores the address of another

Vectors and Pointers CS 16: Solving Problems with Computers I Lecture #13

C++ Programming Chapter 7 Pointers

In Java we have the keyword null, which is the value of an uninitialized reference type

Dynamic Allocation of Memory

C++ for Java Programmers

CPSC 427: Object-Oriented Programming

BITG 1113: POINTER LECTURE 12

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

Declaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples:

! A pointer variable (or pointer): ! An asterisk is used to define a pointer variable. ! ptr is a pointer to an int or

CSCI-1200 Data Structures Fall 2017 Lecture 5 Pointers, Arrays, & Pointer Arithmetic

Chapter-11 POINTERS. Important 3 Marks. Introduction: Memory Utilization of Pointer: Pointer:

CSCI 262 Data Structures. Arrays and Pointers. Arrays. Arrays and Pointers 2/6/2018 POINTER ARITHMETIC

Output of sample program: Size of a short is 2 Size of a int is 4 Size of a double is 8

Note 12/1/ Review of Inheritance Practice: Please write down 10 most important facts you know about inheritance...

Cpt S 122 Data Structures. Course Review Midterm Exam # 1

What is an algorithm?

C++ for Engineers and Scientists. Third Edition. Chapter 12 Pointers

Abstract Data Type (ADT) & ARRAYS ALGORITHMS & DATA STRUCTURES I COMP 221

APS105. Malloc and 2D Arrays. Textbook Chapters 6.4, Datatype Size

Pointers. Memory. void foo() { }//return

Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

CS 61C: Great Ideas in Computer Architecture. C Arrays, Strings, More Pointers

Pointer Data Type and Pointer Variables

CSC 211 Intermediate Programming. Arrays & Pointers

Programming with Arrays Intro to Pointers CS 16: Solving Problems with Computers I Lecture #11

Pointers and Arrays. Introduction To Pointers. Using The "Address Of" The Operator & Operator. Using The Dereference. The Operator * Operator

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

Week 3: Pointers (Part 2)

CA31-1K DIS. Pointers. TA: You Lu

Review of Important Topics in CS1600. Functions Arrays C-strings

CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010

Exercises with Linked Lists CS 16: Solving Problems with Computers I Lecture #15

Pointers and References

Principles of Programming Pointers, Dynamic Memory Allocation, Character Arrays, and Buffer Overruns

Dynamic Memory Allocation

Lecture 23: Pointer Arithmetic

Pointers and Dynamic Memory Allocation

LECTURE 02 INTRODUCTION TO C++

Chapter 1: Object-Oriented Programming Using C++

CPSC 427: Object-Oriented Programming

CSCI-1200 Data Structures Spring 2016 Lecture 6 Pointers & Dynamic Memory

CS 61c: Great Ideas in Computer Architecture

CSCI-1200 Data Structures Spring 2014 Lecture 5 Pointers, Arrays, Pointer Arithmetic

REFERENCES, POINTERS AND STRUCTS

What is Pointer? Pointer is a variable that holds a memory address, usually location of another variable.

C++ for Java Programmers

UEE1302(1066) F12: Introduction to Computers and Programming. Lab 12: Pointer & Dynamic Array (II)

Variation of Pointers

UEE1302(1102) F10: Introduction to Computers and Programming

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Arrays. Arizona State University 1

Dynamic Allocation in C

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Chapter 2: Introduction to C++

CS 230 Programming Languages

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

A pointer variable has a pointer type, and holds pointer values. If a variable uses a number of adjacent locations, the address of the

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

Pointer Assignments. CISC181 Introduction to Computer Science. Dr. McCoy. Lecture 18 October 29, The new Operator

Procedural Programming & Fundamentals of Programming

ALL ABOUT POINTERS C/C++ POINTERS

Before we start - Announcements: There will be a LAB TONIGHT from 5:30 6:30 in CAMP 172. In compensation, no class on Friday, Jan. 31.

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

C Pointers. Indirection Indirection = referencing a value through a pointer. Creating Pointers. Pointer Declarations. Pointer Declarations

KOM3191 Object Oriented Programming Dr Muharrem Mercimek ARRAYS ~ VECTORS. KOM3191 Object-Oriented Computer Programming

Transcription:

Computational Intelligence on Automation Lab @ NCTU UEE1302 (1102) F10 Introduction to Computers and Programming (I) Programming Lecture 10 Pointers & Dynamic Arrays (I) Learning Objectives Pointers Data type and pointer variables Declaration and manipulation Address versus dereference operators Memory management Pointers to user-defined data type Dynamic Arrays Creating and using Pointer arithmetic Multi-dimensional arrays PRO_10 PROF. HUNG-PIN(CHARLES) WEN 2 Memory Addresses Every byte in memory has an integer address Ex: a computer with 256MB RAM addresses start from 0 to 256 1024 1024 An int variable uses 4 bytes Ex: int a = 10; 0012FED4 each address stores one byte store in address 0012FED4 to 0012FED7 PRO_10 PROF. HUNG-PIN(CHARLES) WEN 3 10 Introduction to Pointers [Def] pointer: is a memory address of a variable Syntax: datatype * identifier; Recall: memory divided Numbered memory locations (index) Addresses used as content of a pointer variable Don t be panic! Seen this before. Call-by-reference parameters Address of actual argument was passed PRO_10 PROF. HUNG-PIN(CHARLES) WEN 4

Pointer Variables Pointers can be viewed as one datatype can store pointers in variables not int, double, char and etc. instead, a pointer (or an arrow) to int, double, char and etc. double * p; p is declared a pointer-to-double variable can hold pointers to variables of type double but not other types like char EX: sizeof(p)=8 on x86_64 workstation. PRO_10 PROF. HUNG-PIN(CHARLES) WEN 5 Declaring Pointer Variables Pointers declared like other types add * before variable names produce pointer to that type 3 forms are equivalent int *ptr; //most suggested by textbook int* ptr; //most convenient practically int * ptr; '*' must be located before each variable int *ptr1, var1, *ptr2, var2; ptr1, ptr2 hold pointers to int variables var1, var2 are ordinary int variables PRO_10 PROF. HUNG-PIN(CHARLES) WEN 6 Addresses and Numbers Content of a pointer variable is an address address is an integer number (index for memory location) recall sizeof(double*) But pointer is actually NOT typed in integer not in int datatype not crazy due to abstraction C++ forces pointers be used as addresses cannot be used as numbers even though it is a number PRO_10 PROF. HUNG-PIN(CHARLES) WEN 7 Dereferencing Operator * Two roles of '*' in C/C++: binary multiplication operator, ex: 8*5 a unary operator, ex: int* iptr; As '*' is used as a unary operator, called dereferencing operator (or indirection operator) refer to object to which its operand (that is, a pointer) points 1200 iptr 1800 8 PRO_10 PROF. HUNG-PIN(CHARLES) WEN 8

Address Operator: & &: the "address of" operator also used to specify call-by-reference parameters not coincident Recall: call-by-reference parameters pass address of the actual argument void func(int& var2); func(var1); //int& var2 = var1; var2 and var1 will have the same address Operator s two uses are closely related Pointing To (1/2) Terminology, view talk of pointing, not addresses pointer variable points to ordinary variable leave address talk out int *p1, *p2, v1, v2; p1 = &v1; set pointer variable p1 to point to int variable v1 or p1 is assigned the address of v1 PRO_10 PROF. HUNG-PIN(CHARLES) WEN 9 PRO_10 PROF. HUNG-PIN(CHARLES) WEN 10 Pointing to (2/2) int *p1, *p2, v1, v2; p1 = &v1; Two ways to refer to v1 now: variable v1 itself: cout << v1; via pointer p1: cout << *p1; Dereference operator, * pointer variable derereferenced mean: retrieve mata that p1 points to Consider: int v1 = 0; int* p1; p1 = &v1; *p1 = 45; cout << v1 << endl; cout << *p1 << endl; Produces output: 45 45 Another Example p1 and v1 refer to same variable PRO_10 PROF. HUNG-PIN(CHARLES) WEN 11 PRO_10 PROF. HUNG-PIN(CHARLES) WEN 12

Assignment of Pointers (1/2) Assignment of Pointers (2/2) Pointer variables can be assigned: int *p1, *p2; p1 = p2; //ex: address of p2 is 5678 assign one pointer to another make p1 point to where p2 points p1 is assigned the same address as p2 How about this one? *p1 = *p2; assign the value pointed to by p1 to the value pointed to by p2 copy the content that p2 points to the content that p1 points PRO_10 PROF. HUNG-PIN(CHARLES) WEN 13 PRO_10 PROF. HUNG-PIN(CHARLES) WEN 14 Initialize Pointer Variables C/C++ does not automatically initialize variables Initialize a pointer constant value 0 (a.k.a. null pointer int* ptr = 0; store the null pointer in ptr ptr points to nothing constant NULL is also equivalent ex: int* prt = NULL; Constant 0 is the only number that can be directly assigned to pointer variables. PRO_10 PROF. HUNG-PIN(CHARLES) WEN 15 The new Operator Since pointers can refer to variables no real need to have a standard identifier Can dynamically allocate variables operator new creates variables no identifiers to refer to them just a pointer! p1 = new int; creates a new nameless variable, and assigns p1 to point to it can access with *p1 use just like ordinary variable PRO_10 PROF. HUNG-PIN(CHARLES) WEN 16

Example of Pointer Manipulations (1/2) Example of Pointer Manipulations (2/2) #include <iostream> using namespace std; int main() { int *p1, *p2; p1 = new int; *p1 = 45; p2 = p1; cout << *p1 == << *p1 << endl; cout << *p2 == << *p2 << endl; *p2 = 23; cout << *p1 == << *p1 << endl; cout << *p2 == << *p2 << endl; PRO_10 PROF. HUNG-PIN(CHARLES) WEN 17 p1 = new int(101); //initialize as well *p2 = 77; cout << *p1 == << *p1 << endl; cout << *p2 == << *p2 << endl; return 0; } result on screen: *p1 == 45 *p2 == 45 *p1 == 23 *p2 == 23 *p1 == 101 *p2 == 77 PRO_10 PROF. HUNG-PIN(CHARLES) WEN 18 Pointers and Functions Pointers are full-fledged types can be used just like other types can be function parameters can be returned from functions int* ffindpointer(double* p); this declaration of function ffindpointer: has one pointer-to-double parameter returns pointer-to-int variable PRO_10 PROF. HUNG-PIN(CHARLES) WEN 19 Memory Management Heap (a.k.a. freestore) reserved for dynamically-allocated variables all new dynamic variables consume memory in freestore if too many could use all freestore memory Future new operations will fail if the freestore is full PRO_10 PROF. HUNG-PIN(CHARLES) WEN 20

Checking new Success (1/2) Older compilers: test if null returned by call to new: int* p; p = new int; if (p == NULL) { cout << "Insufficient memory.\n"; exit(1); } if new succeeds, program continues Checking new Success (2/2) For newer compilers, if new operation fails: Program terminates automatically Produces error message Still good practice to use NULL check PRO_10 PROF. HUNG-PIN(CHARLES) WEN 21 PRO_10 PROF. HUNG-PIN(CHARLES) WEN 22 Freestore Size Varies with implementations/machines Typically large most programs won t use all memory but not for buggy programs or some particular applications Memory management still good practice solid software engineering principle memory is always finite regardless of how much there is! The delete Operator De-allocate dynamic memory when no longer needed return memory to freestore int* p; p = new int(5); //some processing delete p; //delete space that p points to de-allocate dynamic memory pointed to by pointer p literally destroys memory space PRO_10 PROF. HUNG-PIN(CHARLES) WEN 23 PRO_10 PROF. HUNG-PIN(CHARLES) WEN 24

Dangling Pointers delete p; destroy dynamic memory but p still points the original address called dangling pointer if p is then dereferenced (*p) unpredictable results! often disastrous! Avoid dangling pointers assign pointer to NULL after delete: delete p; p = NULL; User-defined Pointer Types Define pointer types able to declare pointers like other variables eliminate need for '*' in pointer declaration typedef int* tintptr; define a new type alias Consider these declarations: tintptr p; int* p; two forms are equivalent PRO_10 PROF. HUNG-PIN(CHARLES) WEN 25 PRO_10 PROF. HUNG-PIN(CHARLES) WEN 26 Pointer to Structure Access data in the struct through a variable by using. operation C++ provides member access operator arrow (->) to retrieve components of user-defined variables given a struct tpersonal_id tpersonal_id myself = ( Charles,30); tpersonal_id* current; current = &myself; cout << current->name << endl; cout << (*current).age << endl; Dynamic v.s.. Automatic Variables Local variables declared within function definition Not dynamic created when function is called destroyed when function call completes often called automatic variables properties controlled for you Dynamic variables created with new operator created and destroyed while program runs PRO_10 PROF. HUNG-PIN(CHARLES) WEN 27 PRO_10 PROF. HUNG-PIN(CHARLES) WEN 28

Dynamic Arrays Array variables actually pointer variables! Standard array fixed dimensions for array size for each dimension needs to be a constant. Dynamic array size not specified at programming time determined while program running Array Variables v.s.. Pointers (1/3) Recall: arrays stored in memory addresses, sequentially array variable "refers to" first indexed variable so array variable is a kind of pointer variable! int a[10]; int * p; a and p are both pointer variables! PRO_10 PROF. HUNG-PIN(CHARLES) WEN 29 PRO_10 PROF. HUNG-PIN(CHARLES) WEN 30 Array Variables v.s.. Pointers (2/3) Recall previous example: int a[10]; typedef int* IntPtr; IntPtr p; a and p are pointer variables Performing assignments: p = a; //LEGAL p now points where a points To first indexed variable of array a a = p; //ILLEGAL! array pointer a is constant pointer Array Variables v.s.. Pointers (3/3) Array variable int a[10]; more than a pointer variable "const int*" type array was allocated in memory already variable a MUST point there always! cannot be changed! In contrast to ordinary pointers which can (& typically do) change PRO_10 PROF. HUNG-PIN(CHARLES) WEN 31 PRO_10 PROF. HUNG-PIN(CHARLES) WEN 32

Standard vs. Dynamic Arrays Standard array limitations must specify size first estimate maximum may not know until program runs! waste memory const int MAX_SIZE=100000; int iarray[max_size]; what if the user only need 100 integers? Dynamic arrays can grow and shrink as needed PRO_10 PROF. HUNG-PIN(CHARLES) WEN 33 Creating Dynamic Arrays Use new operator dynamically allocate with pointer variable treat like standard arrays int isize = 0; cin >> isize; typedef double* DoublePtr; DoublePtr d; d = new double[isize]; //size in brackets create dynamically allocated array variable d contain isize elements of type double PRO_10 PROF. HUNG-PIN(CHARLES) WEN 34 Deleting Dynamic Arrays Allocated dynamically at run-time so should be destroyed at run-time Continue the previous example: d = new double[isize];//size in brackets //Processing delete [] d; //delete array that p points de-allocate all memory for dynamic array brackets [] indicate array is there note that d still points there. dangling! should add "d = NULL;" immediately PRO_10 PROF. HUNG-PIN(CHARLES) WEN 35 Pointer Arithmetic Can perform arithmetic on pointers arithmetic over memory address support addition/subtraction compared using relational operators (==,!=, <, >, etc.) int nums[100]; int* iptr; iptr = &nums[0]; //equivalently,iptr = nums; iptr +=4; if (iptr == &nums[4]) cout << jump to 4th element << endl; PRO_10 PROF. HUNG-PIN(CHARLES) WEN 36

Alternative Array Manipulation Use pointer arithmetic! Step through array without indexing([]): for (int idx = 0;idx < arraysize;idx++) cout << *(d + idx) << " "; Equivalent to: for (int idx = 0;idx < arraysize;idx++) cout << d[idx] << " "; Only addition/subtraction on pointers no multiplication, division Can use ++ and -- on pointers Advanced Pointer Notation (1/2) Access to multi-dimensional arrays can be made using pointer notation Consider the declaration: int nums[2][3] = {{16,18,20},{25,26,27}}; create an array of elements and a set of pointer constants named nums, nums[0] and nums[1] address of the first element in first row of nums is nums[0] int variable pointed to by nums[1] is num[1][0] PRO_10 PROF. HUNG-PIN(CHARLES) WEN 37 PRO_10 PROF. HUNG-PIN(CHARLES) WEN 38 Advanced Pointer Notation (2/2) pointer notation subscript notation actual value *nums[0] nums[0][0] 16 *(nums[0]+1) nums[0][1] 18 *(nums[0]+2) nums[0][2] 20 *nums[1] nums[1][0] 25 *(nums[1]+1) nums[1][1] 26 *(nums[1]+2) nums[1][2] 27 Dynamic Multi-dimensional Arrays Multi-dimensional arrays are arrays of arrays various ways to create dynamic multidimensional arrays. typedef int* IntArrayPtr; IntArrayPtr* m = new IntArrayPtr[3]; for (int idx = 0; idx < 3; idx++) m[idx] = new int[4]; declare one array m of 3 IntArrayPtr pointers make each allocated array of 4 integers create one 3x4 dynamic array PRO_10 PROF. HUNG-PIN(CHARLES) WEN 39 PRO_10 PROF. HUNG-PIN(CHARLES) WEN 40

Example: 2-dimensional 2 Dynamic Arrays Shallow vs. Deep Copies int *Mat1[4];//2 nd dimension is fixed at for (int ( row = 0; row < 4; row++) Mat1[row] = new int[6];//create 6 columns 4 rows Mat1[0], Mat1[1], Mat1[2] and Mat1[3] are declared each row has 6 columns to be created (most common) int **Mat2; //2-level pointer Mat2 = new int *[4];//create 4 rows dynamically for (int ( row = 0; row < 4; row++) Mat2[row] = new int [6]; //create 6 columns both Mat2 and *Mat2 are pointers PRO_10 PROF. HUNG-PIN(CHARLES) WEN 41 Shallow copy (copy-by-address) two or more pointers point to the same memory address Deep copy (copy-by-value) two or more pointers have their own data int *first, *second; first = new int[10]; second = first; //shallow copy second = new int[10]; for (int idx=0;idx<10;idx++) //deep copy second[idx] = first[idx]; PRO_10 PROF. HUNG-PIN(CHARLES) WEN 42 Delete Dynamic Arrays Passing Arrays by Function After a dynamic array is of no use any more, deallocate the memory by delete operation Clean reversely from last allocated memory //reallocate a dynamic 5x9 matrix int** Mat = new int *[5]; //create 5 rows for (int row = 0; row < 9; row++) Mat[row]=new int [9];//create 9 columns //some processing for (int row = 0; row < 9; row++) //clean columns delete [] Mat[row]; delete [ ] Mat; //clean rows Mat = NULL; PRO_10 PROF. HUNG-PIN(CHARLES) WEN 43 When array is passed to a function, only pass the address of the first element in main function int max = FindMax(array, size); in function declaration section int FindMax(int* val, int num) { } Parametr receives the address of array array val is one pointer Another form: int FindMax(int val[], int num) PRO_10 PROF. HUNG-PIN(CHARLES) WEN 44

Returning an Array by Function (1/2) Array type pointers are NOT allowed as return-type of function int [] somefunction(); //ILLEGAL! Instead return pointer to array base type: int* somefunction(); //LEGAL! Return a integer pointer after function call in main (or caller) function, int* iptr = somefunction(); Only ONE array (address) can be returned! PRO_10 PROF. HUNG-PIN(CHARLES) WEN 45 Returning an Array by Function (2/2) One more example: int* display(); void main() { cout << *display() << endl; } int* display() { int* iptr = new int (0); int b[2] = {10, 20}; for (int idx = 0; idx < 2; idx++) *iptr += b[idx]; return iptr; } PRO_10 PROF. HUNG-PIN(CHARLES) WEN 46 Allocating C-StringsC A typical problem is to store an array of C- Strings. first declare an array of points to C-Strings dynamically allocate space for C-Strings char word[100]; char * wvec[50]; // n: index of wvec; default is 0 while (cin >> word) { int len = strlen(word)+1; char * nword = new char [len]; strcpy(nword, word); wvec[n] = nword; n++; } PRO_10 PROF. HUNG-PIN(CHARLES) WEN 47 Expanding Dynamic Arrays A program can start with a small array and then expands it only if necessary //assume MAX = 10 at first int * ivec = new int[max]; //initialize n to 0 while (cin ( >> ivec[idx]) { n++; if (n >= MAX) { MAX *= 2; int * tmp = new int [MAX]; for (int ( i=0; i<n; i++) tmp[i] ] = ivec[i]; delete [] ivec; ivec = tmp; } //end of if }//end of while PRO_10 PROF. HUNG-PIN(CHARLES) WEN 48

Common Programming Errors (1/2) Using a pointer to access nonexistent array elements Incorrectly apply address and indirect operators int *ptr1 = &45; int *prt2 = &(miles+10); Illegal to take the address of a value Taking addresses of pointer constants int nums[25]; int * pt; pt = &nums; Correct form: pt = nums; Common Programming Errors (2/2) Taking addresses of a register variable Internal registers do not have addresses Initializing pointer variables incorrectly int *pt = 5; pt is a pointer to an integer must be a valid address of another integer variable or NULL Forgetting to the bracket set, [], after the delete operator when dynamically deallocating memory PRO_10 PROF. HUNG-PIN(CHARLES) WEN 49 PRO_10 PROF. HUNG-PIN(CHARLES) WEN 50 Summary Pointer is memory address Provides indirect reference to variable Dynamic variables Created and destroyed while program runs Freestore Memory storage for dynamic variables Dynamically allocated arrays Size determined as program runs PRO_10 PROF. HUNG-PIN(CHARLES) WEN 51