February 11, 2005 More Pointers Dynamic Memory Allocation.

Slides:



Advertisements
Similar presentations
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.
Advertisements

DYNAMIC MEMORY MANAGEMENT. int x; When the source code containing this statement is compiled and linked, an executable file is generated. When the executable.
Kernighan/Ritchie: Kelley/Pohl:
Pointers Typedef Pointer Arithmetic Pointers and 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.
Dynamic Memory Allocation in C++. Memory Segments in C++ Memory is divided in certain segments – Code Segment Stores application code – Data Segment Holds.
Dynamic Objects. COMP104 Lecture 31 / Slide 2 Static verses Dynamic Objects * Static object n Memory is acquired automatically  int A[10]; n Memory is.
Computer programming1 Arrays. Computer programming2 ARRAYS Motivation Introduction to Arrays Static arrays Arrays and Functions Arrays, Classes, and typedef.
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.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science) MCA, MSc[IT], MTech[IT], MPhil (Comp. Sci), PGDCA, ADCA, Dc. Sc. & Engg.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
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.
11 Introduction to Object Oriented Programming (Continued) Cats II.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Outline Midterm results Static variables Memory model
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.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
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.
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 Writing a Good Program 8. Elementary Data Structure.
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.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
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.
Current Assignments Start Reading Chapter 6 Project 3 – Due Thursday, July 24 Contact List Program Homework 6 – Due Sunday, July 20 First part easy true/false.
CS102 Introduction to Computer Programming Chapter 9 Pointers.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Chapter 7 Pointers: Java does not have pointers. Used for dynamic memory allocation.
Week 2. Functions: int max3(int num1, int num2, int num3) {int result; result = max(max(num1,num2),num3); return result; } //max3.
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
System Programming Practical Session 7 C++ Memory Handling.
11 Introduction to Object Oriented Programming (Continued) Cats.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
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.
Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
Pointers What is the data type of pointer variables?
EGR 2261 Unit 11 Pointers and Dynamic Variables
Overview 4 major memory segments Key differences from Java stack
Introduction to Programming
Pointers and Memory Overview
Pointers Revisited What is variable address, name, value?
group work #hifiTeam
Dynamic Memory Allocation Reference Variables
Dynamic Memory Allocation
CSC 253 Lecture 8.
CSC 253 Lecture 8.
Overview 4 major memory segments Key differences from Java stack
Popping Items Off a Stack Lesson xx
Pointers, Dynamic Data, and Reference Types
Dynamic Memory A whole heap of fun….
Dynamic Memory Allocation
Chapter 12 Pointers and Memory Management
Dynamic Memory A whole heap of fun….
Dynamic Memory.
CS410 – Software Engineering Lecture #5: C++ Basics III
Pointers, Dynamic Data, and Reference Types
SPL – PS2 C++ Memory Handling.
Presentation transcript:

February 11, 2005 More Pointers Dynamic Memory Allocation

Two types of memory Two main types of memory are the stack and the heap They can be physically next to each other or not. This is up to the compiler writer. Their differences are functional.

Stack The stack is for variables whose size and scope are known at compile time. Function parameters are ordinarily put on the stack and local variables defined inside of functions are defined on the stack. (unless they are static). Stack memory is automatically released when the scope is done because the size and scope of the memory were known at compile time. The ordinary way of declaring variables gets memory from the stack.

Heap The heap is for variables whose size and scope are not known until runtime. For variables defined on the heap, the compiler does not know the size and scope and therefore cannot release their memory You need to release their memory explicitly or else you will have ‘memory leaks’;

Creating Variables for Compile Time When we know how many variables of a certain type we need in a program, we can declare those variables in the source code to be compiled at compile time. If we do not know how many we need until runtime, then we need to use dynamic memory allocation (on the heap)

Syntax To do this we need two new C++ operators new and delete

Example int *iptr; iptr = new int; *iptr = 27; cout << “The address of the new variable is << iptr << endl; cout << “The value of the new variable is << *iptr << endl; Then to delete the memory, do this: delete iptr;

We can also do this with arrays. In this case, when you delete, you need to do this: delete [] arrayName; or C++ will only delete the memory in the first memory location. (A very common error)

Lets write a program that: 1.Asks the user for a positive integer. 2.Creates an array of that many integers. 3.Asks the user for integers to put in the array and fills it up one at a time.

#include using namespace std; int main() { int *numbers, howMany = 0; cout << “How many integers would you like to create?” << endl; cin >> howMany; numbers = new int[howMany]; cout << “Please enter your “ << howMany << “ integers and hit enter after each one.” << endl; for (int i=0; i<howMany; i++) { cin>>numbers[i]; } cout << “lets check that they are here. Hit enter “ << endl; char tmp; cin >> tmp; for (int i=0; i<howMany; i++) { cout << numbers[i] << endl; } return 0; }

#include using namespace std; int main() { int *numbers, howMany = 0; cout << “How many integers would you like to create?” << endl; cin >> howMany; numbers = new int[howMany]; You will need to delete this memory cout << “Please enter your “ << howMany << “ integers and hit enter after each one.” << endl; for (int i=0; i<howMany; i++) { cin>>numbers[i]; } cout << “lets check that they are here. Hit enter “ << endl; char tmp; cin >> tmp; for (int i=0; i<howMany; i++) { cout << numbers[i] << endl; } return 0; }

#include using namespace std; int main() { int *numbers, howMany = 0; cout << “How many integers would you like to create?” << endl; cin >> howMany; numbers = new int[howMany]; You will need to delete this memory cout << “Please enter your “ << howMany << “ integers and hit enter after each one.” << endl; for (int i=0; i<howMany; i++) { cin>>numbers[i]; } cout << “lets check that they are here. Hit enter “ << endl; char tmp; cin >> tmp; for (int i=0; i<howMany; i++) { cout << numbers[i] << endl; } delete [] numbers; return 0; }

Remember Memory that is allocated within a function statically, that is, not dynamically, is released automatically when the function returns. But memory that is allocated dynamically exists until delete is called or the program ends, whichever comes first. In particular, dynamically allocated memory continues to exist after the function it was created in (if it was created in a function) returns.

Example Consider this function that returns a pointer. What is wrong with it? char *getName() { char name[100]; cout << “Enter your name.” << endl; cin>>name; return name; }

Example We can do this: char *getName() { char *name; name = new char[100]; cout << “Enter your name.” << endl; cin>>name; return name; } You would then delete the memory after the above function returned.

This Monday Chapter 10 – Characters, Strings and the String Class