POINTER I POINTER DECLARATION DECLARATION BY DYNAMIC POINTER.

Slides:



Advertisements
Similar presentations
Dynamic memory allocation
Advertisements

Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
A pointer is the memory address of a variable. A memory address is a physical location within a system’s memory space. A pointer variable is variable used.
Data Structures (Second Part) Lecture 2 : Pointers Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
Introduction to Programming Lecture 39. Copy Constructor.
Pointers & Dynamic Memory Allocation Mugurel Ionu Andreica Spring 2012.
POINTER Prepared by MMD, Edited by MSY1.  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference.
Informática II Prof. Dr. Gustavo Patiño MJ
Pointers CS 308 – Data Structures. Getting the address of a variable You need to use the address operator & #include void main() { int num; num = 22;
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.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
 2006 Pearson Education, Inc. All rights reserved Pointers.
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.
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.
Memory and C++ Pointers.  C++ objects and memory  C++ primitive types and memory  Note: “primitive types” = int, long, float, double, char, … January.
Computer Science 210 Computer Organization Pointers.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
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.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Pointers CSE 5100 Data Structures and Algorithms.
Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility,
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.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Pointers review Let a variable aa be defined as ‘int *aa;’, what is stored in aa? Let a variable aa be defined as ‘int ** aa;’ what is stored in aa? Why.
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.
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.
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.
Object-Oriented Programming in C++
Dynamic memory allocation and Pointers Lecture 4.
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.
Pointers and Dynamic Memory Allocation. Declaring a pointer.
1 CHAPTER 5 POINTER. 2 Pointers  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference  Dynamic.
Chapter 7 Pointers: Java does not have pointers. Used for dynamic memory allocation.
Review 1 List Data Structure List operations List Implementation Array Linked List.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 7: Pointers.
Pointer II POINTER AND ARRAY POINTER AND STRUCTURE.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
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.
POINTERS IN C. Introduction  A pointer is a variable that holds a memory address  This address is the location of another object (typically another.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
Pointers 1. Introduction Declaring pointer variables Pointer operators Pointer arithmetic 2 Topics to be Covered.
1 CSC103: Introduction to Computer and Programming Lecture No 17.
Learners Support Publications Operators.
CSC Pointers Powerful feature of the C++ language One of the most difficult to master Essential for construction of interesting data structures.
Pointers and Arrays Dynamic Variables and Arrays.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
© 2016 Pearson Education, Ltd. All rights reserved.
Introduction to Programming
Chapter 10: Pointers Starting Out with C++ Early Objects Ninth Edition
8 Pointers.
Dynamically Allocated Memory
Dynamic Memory Allocation Reference Variables
Dynamic Memory Allocation
Dynamic Memory A whole heap of fun….
Dynamic Memory A whole heap of fun….
Dynamic Memory.
C Programming Lecture-8 Pointers and Memory Management
Creating and Using Pointer Variables in C++ By: Ed Brunjes
Presentation transcript:

POINTER I POINTER DECLARATION DECLARATION BY DYNAMIC POINTER

INTRODUCTION Is a data type that stores the address of a value. Allows direct manipulation of data by using the computer’s memory location. To enable the new variables are dynamically created at run time program. Can be used with arrays and structures to refer to a dynamic array structure.

POINTER DECLARATION General form of the pointer declaration Data_Types*Name_Pointer; Type of data Operator Indicators: For reference the actual value. To show variable is pointer type. Variable

POINTER DECLARATION continue… The symbol ‘*’ has a dual role: 1. Declares that the variable is of type pointer. 2. Allows access to the accused by the pointer value is. Operator ‘&’ is used to give an address to the pointer.

EXAMPLE Sample declaration and giving the address to the pointer. char grade; int tahunKursus = 2; float CPA = 3.30; int *P2 = &tahunKursus; float *P3 = &CPA; char *P1; P1 = &grade; if (*P3 >= 3.0) *P1 = ‘A’; cout<<“\n The contents of the memory is accused of pointing P1“ << grade; cout<<“\n The contents of the memory is accused of pointing P2“ << *P2; cout<<“\n The contents of the memory is accused of pointing P3“ << *P3;

POINTER DECLARATION continue… There are two ways to declare a pointer: 1. Declaration of the grant using the address operator &. Example: 2. Pointer variable declaration and assignment statement and then use the & operator for the provision of address. Example: int * P2 = &tahunKursus; char *P1; P1 = &grade;

POINTER DECLARATION continue… Pointer will only point to a memory location that has the same type of data with the data type declaration.

DESCRIPTION OF THE CONTENTS OF MEMORY StatementDescription of the contents of memory char grade; int tahunKursus = 2; float CPA = 3.30; grade tahunKursus CPA This statement provide value to variable normal and all variable has memory location separate.

DESCRIPTION OF THE CONTENTS OF MEMORY StatementDescription of the contents of memory int *P2 = &tahunKursus; float *P3 = &CPA; char *P1; P1 = &grade; grade tahunKursus CPA This statement provide pointers P1, P2, P3 by location memory for the variable the normal change using operator &. Therefore, the pointer P1 and p/u grade, P2 and tahunKursus and P3 and the attributes of a CPA memory location same P1 P2 P

DESCRIPTION OF THE CONTENTS OF MEMORY StatementDescription of the contents of memory if (*P3 >= 3.0) *P1 = ‘A’; A grade tahunKursus CPA This statement provide value to memory location accused by the P1 and also the p/u grade using pointer operator, ie *. Grant value to the accused who by P3 or the value of CPA variables P1 P2 P

DECLARATION BY DYNAMIC Pointer Pointer can also get a dynamic address Address can be found in the dynamic heap memory address DRP computing is no longer used by any process Memory management must be done as soon as possible so it can be used optimally.

DECLARATION BY DYNAMIC po inter cont... Can be done by using new and delete operators Dynamic memory allocation can be done with the new operator If memory is insufficient, the value NULL will be returned. Therefore, it is important bg ensure every request for a memory location has beensuccessfully done bg avoid NULL pointer value.

EXAMPLES OF THE USE OF OPERATOR new # include void main ( ) { char *gred; gred = new gred; if (gred) { cout <<“Pemberian ingatan berjaya”; cout <<“Sila masukkan gred anda”; cin >> *gred; cout <<“Gred anda : ” << *gred; } else cout <<“Pemberian ingatan tidak berjaya”; delete gred; }

EXAMPLES OF THE USE OF OPERATOR new cont... New operator will return the address of the computer's memory heap taken DRP kpd p /u grade pointer. Use the statement if (grade) is to ensure that the pointer got a grade in a dynamicmemory. If the computer's memory is insufficient, pointing grades will get a NULL value and theimplementation of the program is in the else.

EXAMPLES OF THE USE OF OPERATOR new cont... When a dynamically allocated memory is no longer required in the program, it should bereturned to the heap memory This process of using operator delete This allows the memory is reused by other programs or proc esses.

MEMORY CONTENTS OF SAMPLES USING OPERATOR new ext... StatementPicture More Memory And Information char *gred; Grade pointer variable is created ? gred

MEMORY CONTENTS OF SAMPL ES USING OPERATOR new ext... StatementPicture More Memory And I nformation gred = new gred; Pointer variable grades obtained by t he dynamic memory address for the accused tothe new operator. Memory location can only be filled by the only char gred 1021

MEMORY CONTENTS OF SAMPL ES USING OPERATOR new ext... StatementPicture More Memory And Information if (gred) : cin>>*gred;The value of memory location pointer accused by grade are included gred ‘A’ 1021

MEMORY CONTENTS OF SAMPL ES USING OPERATOR new ext... StatementPicture More Memory And Information cout <<“Your Gred :” <<*gred;Output : Your Gred : A Statements that use the operator * This will display the value of the accused by pointing grades.

MEMORY CONTENTS OF SAMPL ES USING OPERATOR new ext... StatementPicture More Memory And Information delete gred; Grade pointer variable will s tore the value NULL. gred