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

Similar documents
Pointers, Dynamic Data, and Reference Types

Propedéutico de Programación

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

Pointers and Dynamic Memory Allocation

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

Pointers II. Class 31

Variation of Pointers

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

Dynamic Memory Allocation

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.

Exam 3 Chapters 7 & 9

CS162 - POINTERS. Lecture: Pointers and Dynamic Memory

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

UEE1302 (1102) F10 Introduction to Computers and Programming (I)

Storage Allocation CSE 143. Pointers, Arrays, and Dynamic Storage Allocation. Pointer Variables. Pointers: Review. Pointers and Types

Short Notes of CS201

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

CS201 - Introduction to Programming Glossary By

Tema 6: Dynamic memory

1 Memory management in C++

Homework #3 CS2255 Fall 2012

Chapter 10 Pointers and Dynamic Arrays. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo

CSC 1300 Exam 4 Comprehensive-ish and Structs

Launchpad Lecture -10

Chapter 10. Pointers and Dynamic Arrays. Copyright 2016 Pearson, Inc. All rights reserved.

Variables, Memory and Pointers

Pointer Data Type and Pointer Variables

Linked-List Basic Examples. A linked-list is Linear collection of self-referential class objects, called nodes Connected by pointer links

CSC 211 Intermediate Programming. Pointers

Pointers and References

Memory and C++ Pointers

a data type is Types

CS201- Introduction to Programming Current Quizzes

CS 376b Computer Vision

Vector and Free Store (Vectors and Arrays)


CS201 Some Important Definitions

Heap Arrays and Linked Lists. Steven R. Bagley

[0569] p 0318 garbage

Introduction to Computer Science Midterm 3 Fall, Points

! A data structure representing a list. ! A series of nodes chained together in sequence. ! A separate pointer (the head) points to the first

DYNAMIC ARRAYS; FUNCTIONS & POINTERS; SHALLOW VS DEEP COPY

CSC 211 Intermediate Programming. Arrays & Pointers

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

Suppose we find the following function in a file: int Abc::xyz(int z) { return 2 * z + 1; }

CS 161 Exam II Winter 2018 FORM 1

CSCI-1200 Data Structures Fall 2017 Lecture 5 Pointers, Arrays, & Pointer Arithmetic

C11: Garbage Collection and Constructors

Dynamic Allocation of Memory

Pointers. Developed By Ms. K.M.Sanghavi

CS24 Week 3 Lecture 1

Computer Programming

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

Complex data types Structures Defined types Structures and functions Structures and pointers (Very) brief introduction to the STL

Chapter 4. ADT Sorted List

Chapter 9. Pointers and Dynamic Arrays

CSI33 Data Structures

COMP26120: Linked List in C (2018/19) Lucas Cordeiro

nptr = new int; // assigns valid address_of_int value to nptr std::cin >> n; // assigns valid int value to n

Structured Data. CIS 15 : Spring 2007

1. Which of the following best describes the situation after Line 1 has been executed?

Memory and Pointers written by Cathy Saxton

CPSC 427: Object-Oriented Programming

Stack memory - "scratch pad" memory that is used by automatic variables.

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

Chapter 2 (Dynamic variable (i.e. pointer), Static variable)

Linked Lists CS 16: Solving Problems with Computers I Lecture #16

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors

KOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming

Computer Programming

Structures and Pointers

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):

Separate Compilation Model

Pointers and Terminal Control

Pointers and Memory 1

Introduction. Primitive Data Types: Integer. Primitive Data Types. ICOM 4036 Programming Languages

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

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

Dynamic Allocation in C

! The address operator (&) returns the address of a. ! Pointer: a variable that stores the address of another

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

Elementary Data Structures: Lists

REFERENCES, POINTERS AND STRUCTS

CSCI-1200 Data Structures Spring 2014 Lecture 5 Pointers, Arrays, Pointer Arithmetic

Heap Arrays. Steven R. Bagley

Programming in C/C Lecture 2

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology

Chapter 2. Procedural Programming

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

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

CSC1322 Object-Oriented Programming Concepts

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

CSCI-1200 Data Structures Fall 2012 Lecture 5 Pointers, Arrays, Pointer Arithmetic

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

EL2310 Scientific Programming

switch case Logic Syntax Basics Functionality Rules Nested switch switch case Comp Sci 1570 Introduction to C++

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

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

Object-Oriented Programming for Scientific Computing

Dynamic Allocation in C

Transcription:

Exam 1 on July 18, 2005 10:00-11:40am 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. This is the address of the variable. int x; float number; char ch; 2004 2012 x number ch 1

Obtaining Memory Addresses The address of a variable can be obtained by using the address-of 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; What is a Pointer Variable? 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. int *; // will hold the address of an int char *q; // q will hold the address of a char Using a Pointer Variable int x; x = 12; int *; = &x; 3000 12 x Because holds the address of x, we say that points to x 2

Unary operator * is the deference operator int x; x = 12; int *; = &x; cout << *; 3000 12 x == * The value pointed to by is denoted by * Using the dereference operator int x; x = 12; int *; = &x; *=5; cout << x; 3000 12 5 x *=5 changes the value at address to 5 Another Example char ch; ch = A ; char *q; q = &ch; *q = Z ; char *p; p = q 3000 q A Z ch 4000 p 3

C++ Data Types Simple Integral char short int long enum Floating array struct Structured union class float double long double Address pointer reference 10 The NULL Pointer There is a pointer constant 0 called the null pointer denoted by NULL in cstddef. But NULL is not memory address 0. 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. if (!= NULL) { //ok to use * here } 11 Allocation of Memory STATIC ALLOCATION Static allocation is the allocation of memory space at compile time. DYNAMIC ALLOCATION Dynamic allocation is the allocation of memory space at run time by using operator new. 4

Three Kinds of Program Data STATIC DATA: memory allocation exists throughout execution of program. static long SeedValue; AUTOMATIC DATA: automatically created at function entry, resides in activation frame of the function, and is destroyed when returning from function. DYNAMIC DATA: explicitly allocated and deallocated during program execution by C++ instructions written by programmer using unary operators new and delete Using Operator new If memory is available in an area called the free store (or heap), operator new allocates the requested object or array, and returns a pointer to (address of ) the memory allocated. Otherwise, the null pointer 0 is returned. The dynamically allocated object exists until the delete operator destroys it. Dynamically Allocated Data char *; = new char; * = B ; cout << *;??? 5

Dynamically Allocated Data char* ; = new char; * = B ; cout << *; Dynamically Allocated Data char* ; = new char; * = B ; cout << *; B Dynamically Allocated Data char* ; = new char; * = B ; cout << *; delete ; Delete deallocates the memory pointed to by The pointer is considered unassigned. The memory is returned to the free store.??? 6

Dynamic Arrays Array limitations Must specify size first May not know until program runs! Must estimate maximum size needed Sometimes OK, sometimes not Wastes memory Dynamic arrays Can grow and shrink as needed Dynamic Array Allocation char *; // is a pointer variable that can hold //the address of a char??? = new char[5]; //dynamically, during run time, allocates memory for 5 //characters and places into the contents of their //beginning address Dynamic Array Allocation char *; = new char[5]; strcpy(, Bye ); B y e \0 [1] = u ; B u e \0 7

Deleting Dynamic Arrays Allocated dynamically at run-time So should be destroyed at run-time Use delete [] ; De-allocates all memory for dynamic array Brackets indicate array is there Memory Leak A memory leak occurs when dynamic memory (that was created using operator new) has been left without a pointer to it by the programmer, and so is inaccessible. Causing a Memory Leak int * = new int; * = 8; int *2 = new int; *2 = -5; 8 2-5 = 2; 8 2-5 8

A Dangling Pointer Occurs when multiple pointers point to the same object and delete is applied to one of them. Leaving a Dangling Pointer int * = new int; * = 8; int *2 = new int; *2 = -5; = 2; 8 2-5 delete 2; 2 = NULL; 8 2 NULL Dynamic Array Example Sort an array of float numbers from standard input. The first element is the size of the array. For example: 5 3.4 2.6 5.7 100.4 43.5 sortfrominput( ) { float *array; int size, idx; cin >> size; array = new float[size]; for (idx=0; idx<size; idx++) cin >> array[idx]; Sort(array, size); OutputSortedArray(array, size); delete[] array; } array array array array idx=3 3.4 2.6 5.7 9