Informática II Prof. Dr. Gustavo Patiño MJ 16- 18 24-09-2013.

Slides:



Advertisements
Similar presentations
Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
Advertisements

Chapter 6 Data Types
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
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 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.
Dale/Weems/Headington
1 Chapter 4 Stack and Queue ADT. 2 Stacks of Coins and Bills.
Dynamic Memory Allocation in C++. Memory Segments in C++ Memory is divided in certain segments – Code Segment Stores application code – Data Segment Holds.
Memory Arrangement Memory is arrange in a sequence of addressable units (usually bytes) –sizeof( ) return the number of units it takes to store a type.
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.
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.
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.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
1 C++ Plus Data Structures Nell Dale Chapter 4 ADTs Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
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.
February 11, 2005 More Pointers Dynamic Memory Allocation.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
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.
SEN 909 OO Programming in C++ Final Exam Multiple choice, True/False and some minimal programming will be required.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Storage Bindings Allocation is the process by which the memory cell or collection of memory cells is assigned to a variable. These cells are taken from.
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.
Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.
1 C++ Syntax and Semantics, and the Program Development Process.
1 C++ Plus Data Structures Nell Dale Chapter 4 ADTs Stack and Queue Modified from the slides by Sylvia Sorkin, Community College of Baltimore County -
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 Data Structures CSCI 132, Spring 2014 Lecture 10 Dynamic Memory and Pointers.
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.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 13: Pointers You are not responsible for virtual functions (starting on.
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.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
Pointers and Dynamic Memory Allocation. Declaring a pointer.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
C HAPTER 03 Pointers Compiled by Dr. Mohammad Alhawarat.
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
Review 1 List Data Structure List operations List Implementation Array Linked List.
1 Final Exam Tues 3/16/10 (2-3:50) (Same classroom) Old Textbook - Chapters 11-16, 18 Focus is on 15, 16 and 18 Final Exam Tues 3/16/10 (2-3:50) (Same.
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
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.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
1 C++ Plus Data Structures Abstract Data Types Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus Modified by.
CSC Pointers Powerful feature of the C++ language One of the most difficult to master Essential for construction of interesting data structures.
Pointers and Dynamic Memory Allocation
Yan Shi CS/SE2630 Lecture Notes
Chapter 14 Dynamic Data and Linked Lists
Pointers and Memory Overview
CSC113: Computer Programming (Theory = 03, Lab = 01)
Pointers Revisited What is variable address, name, value?
Chapter 10: Pointers Starting Out with C++ Early Objects Ninth Edition
Dynamic Memory Allocation
Chapter 15 Pointers, Dynamic Data, and Reference Types
Pointers, Dynamic Data, and Reference Types
Chapter 15 Pointers, Dynamic Data, and Reference Types
Dynamic Memory.
C++ Plus Data Structures
Pointers, Dynamic Data, and Reference Types
Presentation transcript:

Informática II Prof. Dr. Gustavo Patiño MJ

 Review on Pointers  Reference Variables  Dynamic Memory Allocation  The new operator  The delete operator  Dynamic Memory Allocation for Arrays

C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long double

 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 because the value of str itself cannot be changed by assignment. It “points” to the memory location of a char. str [0] [1] [2] [3] [4] [5] [6] [7] ‘H’ ‘e’ ‘l’ ‘l’ ‘o’ ‘\0’ 6000

 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 the address of the variable. int x; float number; char ch; x number ch

 The address of a non-array variable can be obtained by using the address-of operator & (dereference operator). int x; float number; char ch; cout << “Address of x is “ << &x << endl; cout << “Address of number is “ << &number << endl; cout << “Address of ch is “ << &ch << endl;

 A pointer variable is a variable whose value is the address of a location in memory.  To declare a pointer variable, you must specify the type of value that the pointer will point to, for example, int* ptr; // ptr will hold the address of an int char* q; // q will hold the address of a char

x ptr NOTE: Because ptr holds the address of x, we say that ptr “points to” x int x; x = 12; int* ptr; ptr = &x;

x ptr NOTE: The value pointed to by ptr is denoted by *ptr int x; x = 12; int* ptr; ptr = &x; cout << *ptr;

int x; x = 12; int* ptr; ptr = &x; *ptr = 5; x ptr 5 // changes the value at the address ptr points to 5

char ch; ch = ‘A’; char* q; q = &ch; *q = ‘Z’; char* p; p = q; 4000 A ch q Z 6000 p 4000 // now p and q both point to ch

ptr char msg[ ] =“Hello”; char* ptr; ptr = msg; *ptr = ‘M’ ; ptr++; *ptr = ‘a’; ‘H’ ‘e’ ‘l’ ‘l’ ‘o’ ‘\0’ msg 3000 ‘M’‘a’ 3001

 Static memory - where global and static variables live  Heap memory - dynamically allocated at execution time  -"small" amount of "managed" memory accessed using pointers   Stack memory - used by automatic variables In C and C++, three types of memory are used by programs:

 STATIC DATA: Allocated at compiler time  DYNAMIC DATA: explicitly allocated and deallocated during program execution by C++ instructions written by programmer using operators new and delete  AUTOMATIC DATA: automatically created at function entry, resides in activation frame of the function, and is destroyed when returning from function

 In C, functions such as malloc() are used to dynamically allocate memory from the Heap.  In C++, this is accomplished using the new and delete operators.  new is used to allocate memory during execution time.  returns a pointer to the address where the object is to be stored.  always returns a pointer to the type that follows the new.

 If memory is available, in an area called the heap (or free store) new allocates the requested object or array, and returns a pointer to (address of ) the memory allocated.  Otherwise, program terminates with error message.  The dynamically allocated object exists until the delete operator destroys it. 17 new DataType [IntExpression] new DataType

2000 ??? ptr 5000 ‘B’ char* ptr; ptr = new char; *ptr = ‘B’; cout << *ptr; NOTE: Dynamic data has no variable name

 There is a pointer constant called the “null pointer” denoted by NULL  But NULL is not memory address 0.  NOTE: It is an error to dereference a pointer whose value is NULL. Such an error may cause your program to crash, or behave erratically.  It is the programmer’s job to check for this. while (ptr != NULL) {... // ok to use *ptr here }

 The object or array currently pointed to by Pointer is deallocated, and the value of Pointer is undefined. The memory is returned to the free store.  Good idea to set the pointer to the released memory to NULL  Square brackets are used with delete to deallocate a dynamically allocated array. 20 delete Pointer delete [ ] Pointer

5000 ‘B’ 2000 ptr ??? NOTE: delete deallocates the memory pointed to by ptr char* ptr; ptr = new char; *ptr = ‘B’; cout << *ptr; delete ptr;

char *ptr ; ptr = new char[ 5 ]; strcpy( ptr, “Bye” ); ptr[ 1 ] = ‘u’; delete ptr; ptr = NULL; ‘B’ ‘y’ ‘e’ ‘\0’ ‘u’ ptr 3000 ??? 6000 ???NULL // deallocates the array pointed to by ptr // ptr itself is not deallocated // the value of ptr becomes undefined