Lecture Contents Arrays and Vectors: Concepts of pointers and references. Pointer declarations and initialization. Pointer Operators. Dynamic Memory Allocation.

Slides:



Advertisements
Similar presentations
Pointers.
Advertisements

Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Chapter 6 Data Types
Dynamic Memory Allocation I Topics Basic representation and alignment (mainly for static memory allocation, main concepts carry over to dynamic memory.
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
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.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 10: Pointers by.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Copyright 2004 Scott/Jones Publishing Starting Out with C++: Early.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
Lecture 2 Pointers Pointers with Arrays Dynamic Memory Allocation.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 10: 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.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Pointers CSE 2451 Rong Shi.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
C++ Pointers Copies from SEE C++ programming course and from Starting Out with C++: Early Objects, 8/E by Tony Gaddis, Judy Walters and Godfrey Muganda.
Chapter 9 Pointers Fall 2005 Csc 125 Introduction to C++
Stack and Heap Memory Stack resident variables include:
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9: Pointers.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 26: Exam 2 Preview.
1 Pointers Arrays have a disadvantage: Their size must be known at compile time. We would like the capability to allocate an array-like object of any needed.
Chapter 1 Arrays, Pointers, and Structures Saurav Karmakar Spring 2007.
Pointers OVERVIEW.
1 Pointers and Strings Chapter 5 2 What You Will Learn...  How to use pointers Passing arguments to functions with pointers See relationship of pointers.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 5: Pointers and Strings Outline Introduction Pointer Variable Declarations and Initialization.
Types(1). Lecture 52 Type(1)  A type is a collection of values and operations on those values. Integer type  values..., -2, -1, 0, 1, 2,...  operations.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 3 – August 28, 2001.
Data Types Declarations Expressions Data storage C++ Basics.
CS 1704 Introduction to Data Structures and Software Engineering.
CS 261 – Data Structures C Pointers Review. C is Pass By Value Pass-by-value: a copy of the argument is passed in to a parameter void foo (int a) { a.
Review 1 List Data Structure List operations List Implementation Array Linked List.
Pointers Class #9 – Pointers Pointers Pointers are among C++ ’ s most powerful, yet most difficult concepts to master. We ’ ve seen how we can use references.
C++ Programming Lecture 17 Pointers – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 2 – August 23, 2001.
Lecture 10: 2/17/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Pointers and Dynamic Arrays.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
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.
1  Lecture 12 – Pointer FTMK, UTeM – Sem /2014.
Pointers in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
Windows Programming Lecture 03. Pointers and Arrays.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Topic 5 Addresses, Pointers and Arrays. 2 Objectives (Textbook Chapter 14) You should be able to describe: Addresses and Pointers Pointer Operators Pointer.
Lecture 6 C++ Programming
Dynamic Memory Allocation
Pointers, Dynamic Data, and Reference Types
C++ Programming Lecture 17 Pointers – Part I
C Programming Lecture-8 Pointers and Memory Management
Pointers and References
Pointers, Dynamic Data, and Reference Types
SPL – PS2 C++ Memory Handling.
CSE 303 Concepts and Tools for Software Development
Presentation transcript:

Lecture Contents Arrays and Vectors: Concepts of pointers and references. Pointer declarations and initialization. Pointer Operators. Dynamic Memory Allocation. Functions and pointers. Array and Pointers. Pointer to pointer. Pointer to function. Common pitfalls. Some other C-complex data types. Examples.

Pointers and References Pointers and References: Address is an important property of variables. Pointer variable is a variable for containing address of the other variables. int *countPtr, count;

Pointers and References Pointers and References: A reference is an alias of other variables. It also contains the address of the aliased variables. e.g: int count; int& countRef=count; Difference between pointers and references: Pointers are dynamic and changeable. References are static and used just as a variable alias. Pointers have operations references do not. Pointers could be NULL, whereas References must contain variable addresses at all time.

Pointer Declaration Declarations and initialization: int count int *countPtr = &count; int& countAlias=count; int *undecided=NULL; Note: Reference must be initialized when being declared. Pointer could be initialized with NULL (pointing to nowhere). To get an address for initializing pointers, operator & is needed.

Pointer Operators Pointer Operators: &: to get the address of a variable. –double q,*p=&q; –Compared with reference: double q, &p=q; * = To get the value of the variables being pointed by pointers. –*ptr=5; *q=8.95; cout<<*ptr; –Compared with reference: q=5; cout<<q; –cout<<p print out the address containing in p; –Cout<<p print out the value of variable aliased by p if p is a reference.

Pointer Operators Pointer Operators: &: to get the address of a variable. –double q,*p=&q; –Compared with reference: double q, &p=q; * = To get the value of the variables being pointed by pointers. –*ptr=5; *q=8.95; cout<<*ptr; –Compared with reference: q=5; cout<<q; –cout<<p print out the address containing in p; –Cout<<p print out the value of variable aliased by p if p is a reference.

Pointer Expression Pointer expression and arithmetic (impossible with references): Since pointers contain values as addresses of variables (i.e. integer number), it is possible to use pointer value in arithmetic expressions. But some operands would be meaningless to use and strongly recommended not to use. Pointer expressions are very controversial as they has been claimed as the cause of many profound bugs --> pointer expressions is impossible in JAVA (where it uses only reference).

Pointer Expression Pointer expression and arithmetic (impossible with references): Since pointers contain values as addresses of variables (i.e. integer number), it is possible to use pointer value in arithmetic expressions. But some operands would be meaningless to use and strongly recommended not to use. Pointer expressions are very controversial as they has been claimed as the cause of many profound bugs --> pointer expressions is impossible in JAVA (where it uses only reference).

Pointer Expression Usual operators with pointers: –++, --, -, +=,-= The arithmetic operators is only meaningful if the pointers are now pointing to an array (series of elements). Ptr++ = point to the next element in an array. Ptr--=point to the previous element in the a. Ptr+2= point to the next next elements. Ptr1-Ptr2 = How many elements are there between the two pointers.

Constant Pointers Constant Pointers: const int *Ptr; //pointer to constant int *const Ptr; //constant pointer A constant pointer contain a constant address (of other variable) it is not possible to change the content of a constant pointer (i.e. to point it to other address). 4 different cases: Non-constant Pointer to non-constant data. Non-constant Pointer to constant data. Constant Pointer to non-constant data. Constant Pointer to constant data.

Memory Allocation Dynamic Memory Allocation: Variable could be created in the running time -> dynamic variable vs static variables created at compiling time. Memory need to be allocated in the run time for dynamic variable. Pointers are used to refer to dynamic variable through their addresses. Dynamic variables, dynamic data structures make program more powerful.

Memory Allocation Dynamic Memory Allocation: In C language: functions malloc() andcalloc() are used to allocate memory dynamically; free() is used to free the memory. In C++: new and delete Syntax: int *q, *intPtr= new int; double *P=new double(3.14); cin>>size; q=new int[size]; …

Memory Allocation Dynamic Memory Allocation: In C++: new and delete: If successful new return the address of the allocated memory (dynamic variable). If not successful (such as out of memory) then its return NULL. To check if a variable was correctly created examine the pointer. …… p= new int; if(p) //successful "or if (p!=NULL)" …. else //fail to create the dynamic variable …..

Memory Allocation Dynamic Memory Allocation: delete is used to free the memory for dynamic variable when it is no longer needed. delete p; Note: Always free dynamic variable and release memory when the variable is no longer needed, as the could be reallocated to other dynamic variables. After a variable is free the pointer is pointing nowhere (the variable cease to exist); an attempt to use the pointer after delete will cause bug (hang-up). Attempt to free NULL pointer will result in bug (hang-up). Attempt to free constant pointer will cause error.

Function and Pointer Functions and pointers: Passing argument with pointers:

Function and Pointer Functions and pointers: Returning a pointer: int *Max_Address() { int *ptr; ….. ptr=new int; ……. return ptr; }

Function and Pointer Functions and pointers: Returning a reference: int &Max_Address(int &m,int &n) { return (m>n ? m:n; }

Array and Pointer Array and Pointers: Pointer could be used to access array elements using pointer arithmetic. int a[10], *intPtr; ….. intPtr=a; //intPtr pointing to a[0] *(p+5)=4; //a[5]=4 intPtr=a[7]; //intPtr pointing to a[7] intPtr++; //intPtr pointing to a[8]

Array and Pointer Array and Pointers: Dynamic arrays: Pointers could be used to create dynamic arrays of elements. int *intPtr; int size; ….. cin>>size; intPtr=new int[size+10]; …. *(Ptr+3)=5; Ptr[3]=5; delete intPtr;

Array and Pointer Array and Pointers: Array of pointers: int *Ptr[5]; char *Ptr="Hello, World"; char *Ptr[4]={"Spring", "Summer", "Autumn", "Winter" }; Since an array could be represented by a pointer. An array of pointer is equivalent to pointer to pointer: int **Ptr[5]; char **Ptr[4]; … cout<<*Ptr[2];

Pointer to Functions Pointer to functions:

Common Pitfalls Some Common Pitfalls with Pointers: Fail to initialize pointer. Attempt to use uninitialized pointers. Attempt to use pointer to released memory area. Deleting NULL pointers. Confuse between constant pointer and pointer to constant. Misuse of pointer arithmetic. Forget to release memory of dynamic variables when it is no longer needed. (lose address -> wasted memory). Multiple release of same memory area.

Enumeration Type Some other Complex Data Types: 1. Enumeration: is an integral type of finite number of values defined by users. enum { }; enum Semester {Spring, Fall, Summer}; Semester s1,s2; …. s1=Summer; s2=Fall; … if (s1==Fall) cout<<"Fall Semester!";

Structures Some other Complex Data Types: 2. Structure: User-defined complex data type is a combination of other data type. struct { ; }; E.g: struct Student { int age; string name; boolean gender; }; struct Student s1, s2;

Structures Some other Complex Data Types: 2. Structure: User-defined complex data type is a combination Using typedef struct _Student { int age; string name; boolean gender; }; #typedef struct _Student Student; Student s1, s2;

Structure and Pointer Some other Complex Data Types: 2. Structure: User-defined complex data type is a combination Accessing the field data - dot operator: s1.age=23; cout<<s2.name; Pointer with structures: Student *s1,*s2; …. s1=new Student; s1->age=23; s1->name="Peter";

Lecture Contents Some Examples: Write a function for copying an array. Re-implement selection sort using an dynamic array. Write a function that receive an array of n pointers to float and return the pointer that pointing to the maximal float.

Further Readings Textbook 1: Chapter 8. Textbook 2: Chapter 3. Workbook 1: Chapter 6.