Pointer Data Type and Pointer Variables III

Slides:



Advertisements
Similar presentations
Chapter 7: User-Defined Functions II
Advertisements

Informática II Prof. Dr. Gustavo Patiño MJ
1 Day 03 Introduction to C. 2 Memory layout and addresses r s int x = 5, y = 10; float f = 12.5, g = 9.8; char c = ‘r’, d = ‘s’;
Chapter 9. 2 Objectives You should be able to describe: Addresses and Pointers Array Names as Pointers Pointer Arithmetic Passing Addresses Common Programming.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
Pointer Data Type and Pointer Variables
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
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.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
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.
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
Pointer and Array Lists Chapter 3, Summary CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
A FIRST BOOK OF C++ CHAPTER 16 DATA STRUCTURES. OBJECTIVES In this chapter, you will learn about: Single Structures Arrays of Structures Structures as.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
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.
Scis.regis.edu ● CS-362: Data Structures Week 6 Part 2 Dr. Jesús Borrego 1.
Pointer Data Type and Pointer Variables III CS1201: Programming Language 2 By: Nouf Aljaffan Edited by : Nouf Almunyif.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
Pointers and Arrays Dynamic Variables and Arrays.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
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.
C LANGUAGE UNIT 3. UNIT 3 Arrays Arrays – The concept of array – Defining arrays – Initializing arrays.
Object Lifetime and Pointers
EGR 2261 Unit 11 Pointers and Dynamic Variables
Chapter 7: User-Defined Functions II
Day 03 Introduction to C.
Arrays (review) CSE 2011 Winter May 2018.
Strings (Continued) Chapter 13
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Pointers, Polymorphism, and Memory Allocation
Linked lists.
Day 03 Introduction to C.
Pointer Data Type and Pointer Variables II
CNG 140 C Programming (Lecture set 10)
Pointer Data Type and Pointer Variables
Dynamic Memory Allocation
Chapter 10: Pointers 1.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Object Oriented Programming COP3330 / CGS5409
Chapter 15 Pointers, Dynamic Data, and Reference Types
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
CS1201: Programming Language 2
Pointers, Dynamic Data, and Reference Types
Dynamic Memory Allocation
Objectives You should be able to describe: Addresses and Pointers
CS1201: Programming Language 2
Dynamic Memory.
Pointer Data Type and Pointer Variables
Pointer Data Type and Pointer Variables
Linked lists.
Pointers, Dynamic Data, and Reference Types
SPL – PS2 C++ Memory Handling.
Presentation transcript:

Pointer Data Type and Pointer Variables III CS1201: Programming Language 2 By:Nouf Aljaffan Edited by : Nouf Almunyif Pointer Data Type and Pointer Variables III

Dynamic Variables Variables that are created during program execution are called dynamic variables. new and delete operators used to create and destroy dynamic variables, respectively. When a program requires a new variable, the operator new is used. When a program no longer needs a dynamic variable, the operator delete is used. In C++, new and delete are reserved words.

Operator new The operator New has two forms: New ALLOCATES memory (a variable) of the designated type and RETURNS a pointer to it—that is, the address of this allocated memory. Moreover, the allocated memory is uninitialized.

Operator new p = new int; Similarly, the statement: Char *q; Consider the following declaration: int *p; int x; The statement: p = &x; Similarly, the statement: Char *q; q = new char[16]; creates an array of 16 components of type char and stores the base address of the array in q. stores the address of x in p. However, no new memory is allocated. On the other hand, consider the following statement: p = new int; This statement creates a variable during program execution somewhere in memory and stores the address of the allocated memory in p.

int *p; p=new int; *p =10; string *str; str=new string; *str="hello";

Operator delete Example only marks the memory spaces that these pointer variables point to as deallocated.

Dynamic Arrays limitations of a static array is that every time you execute the program, the size of the array is fixed. Using the same data type. It is ERROR to do int t=5; int p[t]; Two approaches are used if you cannot even guess the array size. to declare an array that is large enough to process a variety of data sets. during program execution, you could prompt the user to enter the size of the array and then create an array of the appropriate size. Pointers help in creating arrays during program execution and process. An array created during the execution of a program is called a dynamic Array.

Dynamic Array To create a dynamic array, we use the second form of the new operator. allocates 10 contiguous memory locations, each of type int, and stores the address of the first memory location into p. the statement: *p = 25; stores 25 into the first memory location, and the statements: p++; //p points to the next array component *p = 35; store 35 into the second memory location.

Of course, after performing a few increment operations, it is possible to lose track of the first array component. C++ allows us to use array notation to access these memory locations. For example, the statements: p[0] = 25; p[1] = 35; p[0] refers to the first array component, p[1] refers to the second array component

p[i] refers to the (i)th array component p[i] refers to the (i)th array component. After the preceding statements execute, p still points to the first array component. the following for loop initializes each array component to 0: When the array notation is used to process the array pointed to by p, p stays fixed at the first memory location. Moreover, p is a dynamic array created during program execution.

The following program segment illustrates how to obtain a user’s response to get the array size and create a dynamic array during program execution.

List itself is a variable, and the value stored in list is the base address of the array—that is, the address of the first array component. Suppose the address of the first array component is 1000.

Because the value of list, which is 1000, is a memory address, list is a pointer variable. However, the value stored in list, which is 1000, cannot be altered during program execution. That is, the value of list is constant. Therefore, the increment and decrement operations cannot be applied to list. In fact, any attempt to use the increment or decrement operations on list results in a compile-time error.

copies the value of list, which is 1000, the base address of the array, into p. We are allowed to perform increment and decrement operations on p. An array name is a constant pointer.