Computer Programming Basics Assistant Professor Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea.

Slides:



Advertisements
Similar presentations
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Advertisements

Pointer applications. Arrays and pointers Name of an array is a pointer constant to the first element whose value cannot be changed Address and name refer.
1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the design and concepts behind pointer arithmetic ❏ To write.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
Chapter 9. 2 Objectives You should be able to describe: Addresses and Pointers Array Names as Pointers Pointer Arithmetic Passing Addresses Common Programming.
Dynamic Objects. COMP104 Dynamic Objects / Slide 2 Memory Management * Static Memory Allocation n Memory is allocated at compiling time * Dynamic Memory.
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.
Dynamic Objects. COMP104 Dynamic Objects / Slide 2 Memory Management * Static Memory Allocation n Memory is allocated at compilation time * Dynamic Memory.
Pointers and dynamic objects COMP171 Fall Pointers and dynamic objects/ Slide 2 Topics * Pointers n Memory addresses n Declaration n Dereferencing.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Copyright 2004 Scott/Jones Publishing Starting Out with C++: Early.
Review of pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Pointers. COMP104 Pointers / Slide 2 Pointers * A pointer is a variable used for storing the address of a memory cell. * We can use the pointer to reference.
Pointers and Dynamic Variables. Objectives on completion of this topic, students should be able to: Correctly allocate data dynamically * Use the new.
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.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
ARRAYS AND POINTERS Although pointer types are not integer types, some integer arithmetic operators can be applied to pointers. The affect of this arithmetic.
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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 9: Pointers.
1 Chapter 9 Pointers. 2 Topics 8.1 Getting the Address of a Variable 8.2 Pointer Variables 8.3 Relationship Between Arrays and Pointers 8.4 Pointer Arithmetic.
Pointers Applications
Pointers. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program execution.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
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.
Chapter 9 Pointers Fall 2005 Csc 125 Introduction to C++
Pointers Chapter 9. Getting The Address Of A Variable Each variable in program is stored at a unique address Use address operator & to get address of.
February 11, 2005 More Pointers Dynamic Memory Allocation.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Dynamic Memory Allocation 9.8.
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.
Pointers Pointer a data type stores a memory address points to whatever the memory location contains A pointer is a variable that can store a memory address.
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.
Chapter 9 Pointers and Dynamic Arrays (9.1). Pointers A variables which holds the memory address for a variable of a specific type. Call-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.
Object-Oriented Programming in C++
Introduction to C++ Programming Language Assistant Professor Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University,
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Lecture 6 C++ Programming Arne Kutzner Hanyang University / Seoul Korea.
POINTERS.
Pointers It provides a way of accessing a variable without referring to its name. The mechanism used for this is the address of the variable.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 7 – Pointers.
Computer Programming Basics Assistant Professor Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea.
CS162 - Topic #12 Lecture: –Arrays with Structured Elements defining and using arrays of arrays remember pointer arithmetic Programming Project –Any questions?
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
CSCI 125 & 161 / ENGR 144 Lecture 16 Martin van Bommel.
POINTERS AND MEMORY ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
Introduction to C++ Programming Language
CSCE 210 Data Structures and Algorithms
Pointers and dynamic objects
Dynamic Memory A whole heap of fun….
Introduction to C++ Programming Language
Pointers.
Objectives You should be able to describe: Addresses and Pointers
7 Arrays.
Dynamic Objects.
Dynamic Memory A whole heap of fun….
Chapter 9: Pointers and String
Pointers and dynamic objects
Pointers, Dynamic Data, and Reference Types
Presentation transcript:

Computer Programming Basics Assistant Professor Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea

CHAPTER 9 Pointers – Part 2

Pointers to arrays a &a[0] same ‘a’ is a pointer only to the first element, not the whole array

Array name as a pointer int main () { int a[3]; cout << &a[0]; //→ 1004 cout << a; //→ 1004 } int main () { int a[3]; cout << &a[0]; //→ 1004 cout << a; //→ 1004 } a[0]a[1]a[2] a The name of an array is a pointer constant to its first element 4

Dereference of array name

Array names as pointers

To access an array, any pointer to the first element can be used instead of the name of the array. Note:

Multiple array pointers

Pointer Arithmetic and Arrays Given pointer, p, p ± n is a pointer to the value n elements away.

Pointer arithmetic and different types

Dereferencing array pointers

Find smallest

Pointers and other operators int a[20]; int * p = &(a[5]); for(int i=0;i<20;i++) a[i] = i; cout << p+5 << endl;//address of a[10] cout << *(p+5) << endl;//10 cout << *(p++) << endl;//5 cout << *(--p) << endl;//5 int * q = &(a[10]); cout << *(p + (q-p)/2) << endl;//7 return 0;

Pointers to two-dimensional arrays

table[0] is identical to *(table + 0) table[0][0] is identical to *(*(table) table[i][j] is identical to *(*(table + i) + j)

Two dimension array and pointer FF FF FF FF1C FF FF FF FF2C FF FF FF FF3C int Dim2[3][4]

Two dimension array and pointer FF FF FF FF1C FF FF FF FF2C FF FF FF FF3C int Dim2[3][4] Dim2[0] Dim2[1] Dim2[2] Dim2[0][0]Dim2[0][1]Dim2[0][2]Dim2[0][3] Dim2[1][0]Dim2[1][1]Dim2[1][2]Dim2[1][3] Dim2[2][0]Dim2[2][1]Dim2[2][2]Dim2[2][3]

Two dimension array and pointer FF FF FF FF1C FF FF FF FF2C FF FF FF FF3C [3][4] int Dim2[3][4] Dim2[0] *(Dim2+0) Dim2+0 Dim2[1] *(Dim2+1) Dim2+1 Dim2[2] *(Dim2+2) Dim2+2 Dim2 It’s a “Pointer to Pointer” Points to start of array

Two dimension array and pointer FF FF FF1C FF FF FF FF2C FF FF FF FF3C int Dim2[3][4] Dim2[1]+1 *(Dim2+1) FF10 *(Dim2[1]+1) *(*(Dim2+1)+1)

Simple codes

Results

Passing one dimensional array to a function void doIt(int ary[]); void doThat(int *ary); int main() { int a[10]; int *p = &(a[0]); doIt(a); doThat(a); doIt(p); doThat(p); doIt(&(a[0])); doThat(&(a[0])); }

Passing 2 dimensional array to a function void doIt(int ary[][20]); void doThat(int **ary);//wrong int main() { int a[10][20]; int (*p)[20] = a; doIt(a); doThat(a);//wrong doIt(p); doThat(p);//wrong doIt(&(a[0])); doThat(&(a[0]));//wrong }

Passing 3 dimensional array to a function void doIt(int ary[][10][20]); int main() { int a[5][10][20]; int (*p)[10][20] = a; doIt(a); doIt(p); doIt(&(a[0])); }

Example: variables for multiplying array elements Results: Please enter an integer: 2 Please enter an integer: 7 Please enter an integer: 12 Please enter an integer: 3 Please enter an integer: 6 Doubled size is:

Memory allocation

A conceptual view of memory

Memory management functions

You can refer to dynamic memory only through a pointer. Note:

new memory allocation for a single data item

Memory allocation for an array

Freeing memory

Memory allocated by new must be released with delete, and memory allocated by new[…] must be released with delete[ ]. Note:

Array of pointers: A ragged array

Dynamic vs. static memory allocation Array Dynamic Memory Allocation using Pointer Declarationint arrayVar[50];int* ptrVar; Memory Allocation Not required (automatically at program execution) ptrVar = new int[50]; Memory Release Impossible (memory reserved until termination) delete ptrVar; Too much data case Impossible to increase memory size Easy (allocate more memory) Too small data case Impossible to decrease Memory size Easy (release and maintain small memory) Pros Easy to programming using just an INDEX Optimized memory usage Cons Fixed memory space (Big program), Weak for unexpected memory request Complex to manage pointers (side effect expected)