1 Data Structures CSCI 132, Spring 2014 Lecture 10 Dynamic Memory and Pointers.

Slides:



Advertisements
Similar presentations
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Advertisements

Chapter 6 Data Types
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
Informática II Prof. Dr. Gustavo Patiño MJ
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.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
Computer programming1 Arrays. Computer programming2 ARRAYS Motivation Introduction to Arrays Static arrays Arrays and Functions Arrays, Classes, and typedef.
More Arrays Arrays and classes Multi-dimensional Arrays Dynamic arrays.
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.
Pointers and Dynamic Variables. Objectives on completion of this topic, students should be able to: Correctly allocate data dynamically * Use the new.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
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.
Pointers Applications
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Pointer Data Type and Pointer Variables
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Pointers CS362. Pointers A Pointer is a variable that can hold a memory address Pointers can be used to: Indirectly reference existing variables (sometimes.
Microsoft Visual C++.NET Chapter 61 Memory Management.
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.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
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.
Stack and Heap Memory Stack resident variables include:
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
1 STRUCTURES AND POINTERS. 2 A VARIABLE OF THIS COMPOSITE TYPE CAN HAVE MORE THAN ONE VALUE, GROUPED TOGETHER TO DESCRIBE AN ENTITY. THE COMPONENTS OF.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
1 Pointers and Arrays. 2 When an array is declared,  The compiler allocates sufficient amount of storage to contain all the elements of the array in.
Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.
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.
CSC 107 – Programming For Science. Today’s Goal  When lecture over, start understanding pointers  What a pointer is and what it is not  Why pointers.
Pointers Pointers are a unique class of variables whose purpose is to “point” to other variables Pointers allow programmers direct access to memory addresses.
CSC 107 – Programming For Science. Today’s Goal  When lecture over, start understanding pointers  What a pointer is and what it is not  Why pointers.
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.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
Object-Oriented Programming in C++
Dynamic memory allocation and Pointers Lecture 4.
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.
Dynamic Memory. We will follow different order from Course Book We will follow different order from Course Book First we will cover Sect The new.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
1 Pointers to structs. 2 A pointer to a struct is used in the same way as a pointer to a simple type, such as an int. Pointers to structs were introduced.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Lecture 23: Pointers. 2 Lecture Contents: t Pointers and addresses t Pointers and function arguments t Pointers and arrays t Pointer arrays t Demo programs.
Chapter 11 – Pointer Variables. Declaring a Pointer Variable u Declared with data type, * and identifier type* pointer_variable; u * follows data type.
Pointer Variables int i; declares an int variable and sets aside a named memory location to store the int int * iptr; declares a variable that holds the.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: 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 
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Revision on C++ Pointers TCP1201: 2013/2014. Pointer Basics  Why pointer is important? 1. Reference/Point to existing data without cloning the data.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
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.
Data Structures in C++ Pointers & Dynamic Arrays Shinta P.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
Pointers. What Is Pointer l every variable has memory address char c=’y’; int i=2; address of variable i is 0022 l address can used to refer to this variable.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
EGR 2261 Unit 11 Pointers and Dynamic Variables
Pointers, Polymorphism, and Memory Allocation
Pointers.
Dynamic Memory CSCE 121 J. Michael Moore.
Pointers and References
Dynamic Memory Allocation
Pointers, Dynamic Data, and Reference Types
Pointers and References
Dynamic Memory CSCE 121.
Pointers, Dynamic Data, and Reference Types
Presentation transcript:

1 Data Structures CSCI 132, Spring 2014 Lecture 10 Dynamic Memory and Pointers

2 Multiple includes for.h files Sometimes more than 1 source file needs to include a given.h file. The compiler may give an error, since the.h file is defined twice. Use a pre-processor directive to avoid this problem: #ifndef STACK_H #def STACK_H typedef.... class Stack {... }; #endif

3 Recall Arrays int numbers[5]; The name of the array, numbers, contains the base address of the array. numbers, is a pointer.

4 Declaring a Pointer A pointer is a variable that can store the memory address of some piece of data. Declaring a pointer: float *p; p is a pointer that can store a memory address of a floating point variable.

5 Allocating Memory Declaring a pointer does not automatically give it a valid memory address: float *p; To allocate memory, use the key word: new p = new float; This is dynamic allocation of memory. p p ?

6 Accessing memory pointed to To access the memory pointed to by a pointer, use the indirection operator (*): *p = 15.5; Example: float *p; p = new float; *p = 15.5; cout << "The content of memory cell pointed to by p is " << *p << endl; p15.5

7 General form for pointers Declaration of a pointer: type *variableName; Allocation of memory: new type or new type[n] //allocates n elements of specified type Accessing data with pointer: *pointerName

8 Illegal Pointer operations Because pointers hold addresses, you cannot assign data of type int or float or other data types to a pointer. The following is not allowed: int *p; float *q; p = 1000;// Not Allowed q = 15.5;// Not Allowed

9 Assigning a pointer One pointer can be assigned to another of the same type: int *p; int *q; p = new int; q = p; *p = 20; cout << *p << " " << *q << endl; p q 20

10 Pointer arithmetic Pointer arithmetic allows accessing of items in an array: int *p; p = new int[5]; *p is value of p[0] *(p+i) is value of p[i] Each increment of p moves the pointer by enough memory to move to next item in the array.

11 Pointers to structs Pointers to structs follow the same rules: struct stateParty { int dems; int repubs; }; stateParty *p; p = new stateParty; p ?? demsrepubs

12 Accessing struct members Struct members are accessed with the selection operator: p = new stateParty; (*p). dems = 10000; //The parentheses are essential! (*p). repubs = 7938; Alternatively: p - > dems = 10000; p - > repubs = 7938; p demsrepubs

13 Orphan structs If you assign one pointer to another, the struct previously pointed to by the latter is no longer accessible: q = p; p demsrepubs q demsrepubs

14 The memory heap C++ maintains a storage pool of available memory called the heap. The keyword new allocates memory from this storage pool. p = new stateParty; Heap p Heap p

15 Returning memory to the heap Use the keyword, delete, to return memory to the heap: delete p; p no longer points to a valid memory location The memory is returned to the heap and can be reallocated later.

16 Things to be careful about WARNINGS: If more than one pointer points to the same structure, if one is deleted, the other may still point to that location. However, C++ may reassign that memory, causing errors in the program execution. Make sure you no longer need a structure before you return it to the heap. Only use delete with pointers whose values were set by new.

17 Diagram each line struct electric { String20 current; int volts; }; electric *p, *q; p = new electric; strcpy( p - > current, "AC" ); p - > volts = 220; q = new electric; q -> volts = p - > volts; strcpy ( q - > current, "DC");

18 Diagram the pointers electric *a, *b, *c; a = new electric; b = new electric; c = new electric; a = b; b = c; c = a;