Pointers II. Class 31

Similar documents
Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

Variables, Memory and Pointers

cout << "How many numbers would you like to type? "; cin >> memsize; p = new int[memsize];

Pointers, Dynamic Data, and Reference Types

Memory and Pointers written by Cathy Saxton

Homework #3 CS2255 Fall 2012

Pointers. Addresses in Memory. Exam 1 on July 18, :00-11:40am

Memory Allocation. Static Allocation. Dynamic Allocation. Dynamic Storage Allocation. CS 414: Operating Systems Spring 2008

Exam 3 Chapters 7 & 9

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors


In Java we have the keyword null, which is the value of an uninitialized reference type

Pointers and Terminal Control

In this chapter, you will learn about: Pointers. Dynamic Arrays. Introduction Computer Science 1 CS 23021

Computer Programming

Chapter 9. Pointers and Dynamic Arrays

Understand Execution of a Program

Name, Scope, and Binding. Outline [1]

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

FORM 2 (Please put your name and form # on the scantron!!!!) CS 161 Exam II:

CSCI 262 Data Structures. Arrays and Pointers. Arrays. Arrays and Pointers 2/6/2018 POINTER ARITHMETIC

Before we start - Announcements: There will be a LAB TONIGHT from 5:30 6:30 in CAMP 172. In compensation, no class on Friday, Jan. 31.

CA31-1K DIS. Pointers. TA: You Lu

Pointer Data Type and Pointer Variables

Separate Compilation Model

Linked Memory. Pointers Linked Lists. September 21, 2017 Cinda Heeren / Geoffrey Tien 1

Computer Programming

Pointers and References

[0569] p 0318 garbage

Exercise 1.1 Hello world

Chapter Overview. Pointers and Dynamic Arrays. Pointers. Pointers. Declaring Pointers. Pointers Tell Where To Find A Variable. 9.

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

CS201 Some Important Definitions

CS201- Introduction to Programming Current Quizzes

Run-Time Data Structures

CSci 1113 Midterm 1. Name: Student ID:

MM1_ doc Page E-1 of 12 Rüdiger Siol :21

Today s lecture. Pointers/arrays. Stack versus heap allocation CULTURE FACT: IN CODE, IT S NOT CONSIDERED RUDE TO POINT.

CS 161 Exam II Winter 2018 FORM 1

DELHI PUBLIC SCHOOL TAPI

DYNAMIC ARRAYS; FUNCTIONS & POINTERS; SHALLOW VS DEEP COPY

CSC 1300 Exam 4 Comprehensive-ish and Structs

Object-Oriented Programming for Scientific Computing

Advanced Programming & C++ Language

Short Notes of CS201

Comp 11 Lectures. Mike Shah. June 26, Tufts University. Mike Shah (Tufts University) Comp 11 Lectures June 26, / 57

Declaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples:

CS201 - Introduction to Programming Glossary By

Documentation. Programming / Documentation Slide 42

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 5: Control Structures II (Repetition)

Exercise: Inventing Language

Lecture 7: Binding Time and Storage

6. Pointers, Structs, and Arrays. March 14 & 15, 2011

CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 11

Compiler Construction D7011E

Outline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples.

Lab 2.1: Fixing a C++ program

Quiz Start Time: 09:34 PM Time Left 82 sec(s)

CSCI-1200 Data Structures Spring 2016 Lecture 6 Pointers & Dynamic Memory

Run-time Environments - 3

CS201 Latest Solved MCQs

CS399 New Beginnings. Jonathan Walpole

Lab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1

Functions and Procedures

CSC1322 Object-Oriented Programming Concepts

Chapter 5. Names, Bindings, and Scopes

Come and join us at WebLyceum

6. Pointers, Structs, and Arrays. 1. Juli 2011

Memory and C++ Pointers

Pointers. Reference operator (&) ted = &andy;

Chapter-11 POINTERS. Important 3 Marks. Introduction: Memory Utilization of Pointer: Pointer:

! A pointer variable (or pointer): ! An asterisk is used to define a pointer variable. ! ptr is a pointer to an int or

Appendix G: Writing Managed C++ Code for the.net Framework

CS 11 C track: lecture 5

pointers + memory double x; string a; int x; main overhead int y; main overhead

C10: Garbage Collection and Constructors

C++ Programming: From Problem Analysis to Program Design, Third Edition

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

Type Aliases. Examples: using newtype = existingtype; // C++11 typedef existingtype newtype; // equivalent, still works

What is Pointer? Pointer is a variable that holds a memory address, usually location of another variable.

Implementing Abstractions

Dynamic Memory: Alignment and Fragmentation

CISC220 Lab 2: Due Wed, Sep 26 at Midnight (110 pts)

Today USING POINTERS. Functions: parameters and arguments. Todaywewilllookattopicsrelatingtotheuseofpointers

Programming Languages

Agenda / Learning Objectives: 1. Map out a plan to study for mid-term Review the C++ operators up to logical operators. 3. Read about the tips

Arrays and functions Multidimensional arrays Sorting and algorithm efficiency

[CSE10200] Programming Basis ( 프로그래밍기초 ) Chapter 9. Seungkyu Lee. Assistant Professor, Dept. of Computer Engineering Kyung Hee University

Compiling Techniques

Constants, References

内存管理. Memory management

a data type is Types

CS103L SPRING 2018 UNIT 7: FILE I/O

BEng (Hons) Electronic Engineering. Resit Examinations for / Semester 1

Pointers and Dynamic Arrays

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 8: Dynamic Memory Allocation

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

Principles of Programming Pointers, Dynamic Memory Allocation, Character Arrays, and Buffer Overruns

Memory Allocation. Copyright : University of Illinois CS 241 Staff 1

Outline. Introduction. Pointer variables. Pointer operators. Calling functions by reference. Using const with pointers. Examples.

Transcription:

Pointers II Class 31

Compile Time all of the variables we have seen so far have been declared at compile time they are written into the program code you can see by looking at the program how many variables will exist in the entire program they are in an area of memory called the stack this is fine when you know exactly how many variables you will need in the entire program

Compile Time all of the variables we have seen so far have been declared at compile time they are written into the program code you can see by looking at the program how many variables will exist in the entire program they are in an area of memory called the stack this is fine when you know exactly how many variables you will need in the entire program but sometimes you do not know in advance how many variables you will need

Run Time it is possible to create (and destroy) variables at runtime this is called dynamic allocation while a program is running, the logic may dictate that a new variable is needed, not known when the program was written

Run Time it is possible to create (and destroy) variables at runtime this is called dynamic allocation while a program is running, the logic may dictate that a new variable is needed, not known when the program was written there is a pool of memory that is available to be drawn from at need this pool is called the heap

New a program can allocate a piece of this memory at runtime by using the operator new int *value = new int;

New a program can allocate a piece of this memory at runtime by using the operator new int *value = new int; this line of code requests 4 bytes of memory from the heap

New a program can allocate a piece of this memory at runtime by using the operator new int *value = new int; this line of code requests 4 bytes of memory from the heap the operating system responds with the address of the 4-byte chunk in the heap

New a program can allocate a piece of this memory at runtime by using the operator new int *value = new int; this line of code requests 4 bytes of memory from the heap the operating system responds with the address of the 4-byte chunk in the heap since new provides an address, it must be assigned to a pointer

New a program can allocate a piece of this memory at runtime by using the operator new int *value = new int; this line of code requests 4 bytes of memory from the heap the operating system responds with the address of the 4-byte chunk in the heap since new provides an address, it must be assigned to a pointer this new variable can be accessed by dereferencing the pointer variable value

Dynamic Allocation here is a program fragment that uses a dynamically allocated variable double *value = new double; *value = 25.0; *value *= 2.0; cout << "the value is: " << *value << endl;

Diagramming Dynamic Memory even though a computer s memory is one huge linear list of locations we diagram the stack and heap as separate areas double *value = new double; *value = 25.0; value double* 25.0 double

Dynamic Allocation of Arrays in reality, allocating a single variable isn t very useful the true power of dynamic allocation is in allocating an entire array at runtime cout << "how many values do you need to store? "; cin >> value_count; int *values = new int[value_count]; finally we can create an array based on a variable!

Stack and Heap the stack area of memory is where all statically (compile-time) declared variables exist the operating system gives it to your program when your program starts running and reclaims it when your program finishes

Stack and Heap the stack area of memory is where all statically (compile-time) declared variables exist the operating system gives it to your program when your program starts running and reclaims it when your program finishes the heap is where all dynamically (run-time) declared variables are allocated the programmer is responsible for allocating it, and the programmer is responsible for de-allocating it before your program finishes

Stack and Heap the stack area of memory is where all statically (compile-time) declared variables exist the operating system gives it to your program when your program starts running and reclaims it when your program finishes the heap is where all dynamically (run-time) declared variables are allocated the programmer is responsible for allocating it, and the programmer is responsible for de-allocating it before your program finishes when you use memory from the heap you have to give it back before the program finishes

De-Allocating Memory just as the new operator allocates a piece of memory the delete operator gives it back double *value = new double; must always be followed eventually by: delete value;

De-Allocating Memory just as the new operator allocates a piece of memory the delete operator gives it back double *value = new double; must always be followed eventually by: delete value; int *values = new int[50]; must always be followed eventually by: delete [] values; // note the syntax!! every program must have exactly as many deletes as news

we will not do sections 9.9 9.10 9.11 Skipped Sections