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

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

Dynamic Allocation of Memory

Pointers, Dynamic Data, and Reference Types

[0569] p 0318 garbage

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

Pointers and References

CS201- Introduction to Programming Current Quizzes

CS113: Lecture 5. Topics: Pointers. Pointers and Activation Records

References and pointers

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

Memory and C++ Pointers

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

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

Pointers II. Class 31

377 Student Guide to C++

Intermediate Programming, Spring 2017*

What have we learned about when we learned about function parameters? 1-1

Pointers review. int a = 5; int *ptr = &a; cout << *ptr;

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

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Lecture 2, September 4

Consider the above code. This code compiles and runs, but has an error. Can you tell what the error is?

Dynamic Allocation of Memory

Week 9 Part 1. Kyle Dewey. Tuesday, August 28, 12

CS 11 C track: lecture 5

CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays

a data type is Types

CSE 307: Principles of Programming Languages

Pointers. A pointer value is the address of the first byte of the pointed object in the memory. A pointer does not know how many bytes it points to.

SYSC 2006 C Winter 2012

Variables, Memory and Pointers

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

Pointers. Memory. void foo() { }//return

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

Memory and Addresses. Pointers in C. Memory is just a sequence of byte-sized storage devices.

Dynamic Memory Allocation

Linked lists Tutorial 5b

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

Memory Management in C (Dynamic Strings) Personal Software Engineering

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

ECE 2400 Computer Systems Programming Fall 2018 Topic 7: Concrete Data Types

THE GOOD, BAD AND UGLY ABOUT POINTERS. Problem Solving with Computers-I

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

CS201 Latest Solved MCQs

CMSC 341 Lecture 2 Dynamic Memory and Pointers

377 Student Guide to C++

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

REFERENCES, POINTERS AND STRUCTS

Pointers and Terminal Control

CS162 - POINTERS. Lecture: Pointers and Dynamic Memory

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

C Introduction. Comparison w/ Java, Memory Model, and Pointers

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

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

Introducing C++ to Java Programmers

Introduction to C. Zhiyuan Teo. Cornell CS 4411, August 26, Geared toward programmers

What the CPU Sees Basic Flow Control Conditional Flow Control Structured Flow Control Functions and Scope. C Flow Control.

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

CSC 211 Intermediate Programming. Arrays & Pointers

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

School of Science and Technology

Ch. 11: References & the Copy-Constructor. - continued -

What goes inside when you declare a variable?

Intro to C: Pointers and Arrays

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

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

Pointers, Arrays and Parameters

Contents. 8-1 Copyright (c) N. Afshartous

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

Consider the program...

ECE 15B COMPUTER ORGANIZATION

Pointers and Dynamic Memory Allocation

CS 161 Exam II Winter 2018 FORM 1

First of all, it is a variable, just like other variables you studied

C++ Programming. Pointers and Memory Management. M1 Math Michail Lampis

Lecture Notes on Memory Layout

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

CS113: Lecture 9. Topics: Dynamic Allocation. Dynamic Data Structures

Limitations of the stack

Chapter 9. Pointers and Dynamic Arrays

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

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

CS61C : Machine Structures

Run Time Environment

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

CSCE 2014 Final Exam Spring Version A

Lecture 9. Pointer, linked node, linked list TDDD86: DALP. Content. Contents Pointer and linked node. 1.1 Introduction. 1.

Object Reference and Memory Allocation. Questions:

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

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

Class Information ANNOUCEMENTS

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

The Stack, Free Store, and Global Namespace

CSC 1600 Memory Layout for Unix Processes"

Heap Arrays and Linked Lists. Steven R. Bagley

Data, memory. Pointers and Dynamic Variables. Example. Pointers Variables (or Pointers) Fall 2018, CS2

Memory Leak. C++: Memory Problems. Memory Leak. Memory Leak. Pointer Ownership. Memory Leak

Introduction to C. Sean Ogden. Cornell CS 4411, August 30, Geared toward programmers

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

Transcription:

CA31-1K DIS Pointers TA: You Lu

Pointers Recall that while we think of variables by their names like: int numbers; Computer likes to think of variables by their memory address: 0012FED4 A pointer is a variable that stores the address of another variable.

Memory address of a variable Lets say we have an int variable x: 42 x 0x22ff40 The variables name is x and that is how programmer refers to it in his program. The variable is stored in memory location 0x22ff40. We say that x has address 0x22ff40. The variable is storing the value 42.

Memory address of a variable x 42 0x22ff40 If I want to get an address of a variable I can use the address operator &. &x is the address of the variable x. Where have we seen this before? cout << x; cout << &x; What is the output?

Pointers Lets say we have an int pointer p and a regular int variable x. p x 0x22ff40 42 0x22ff40 Here the pointer p is storing the address of the variable x. We say that p points to x. We draw an arrow to indicate that p points to x. p 0x22ff40 42 x 0x22ff40

Declaring pointers Syntax for declaring a pointer: type * name; Example: int * p; // Declares an integer pointer, but it is // uninitialized. int a=2; p = &a; // p now points to a. Draw a picture

Dereferencing operator * We can access the memory location that a pointer points to by using a dereferencing operator. We can use the dereferencing to output the contents of the memory location: cout << *p; // Outputs the value 2.

Dereferencing operator * exf41000 2 p x exf41000 We can even use the dereferencing operator to change the value stored at the memory location where p points to!! cout << x; *p = 5; cout << *p; cout << x; exf41000 5 p x exf41000 So *p acts exactly like the variable x that p points to.

Pointer fun int a=2; int *p; // Do not confuse this * with dereferencing p = &a; // p points to a What is the output of the following lines? cout << *p; cout << p; cout << &a; cout << &p; cout << *&a;

Pointers to different kinds of variables double *b; string *MyString; Product *ProductPointer; Remember that the pointer type has to match the variable type. I can not do: double a; int *p; p=&a; // p can only point to int variable!

Example int a; int *p; p=&a; a=23; cout << *p; a=56; cout << *p; *p=5; cout <<a;

Example It is very important to initialize pointers before using them. int *q; cout << *q; // crashes the program if q is not // pointing to anything.

Understanding difference between addresses and reference Be careful when declaring multiple ptrs you must put * on every one of them : int *a, b; // a is int*, but b is an int int * p1, *p2; *p1=7; *p2=8; This is wrong why? This even compiles but remember uninitialized pointers are just like unintialized variables. The pointers point to some random memory. It's not a good idea to change what is stored in random memory location...your own program might be stored there!!

Understanding difference between addresses and reference int * p1, *p2; int a,b; p1=&a; p2=&b; *p1=7; *p2=8; Lets draw a picture

Example int *p1, *p2; int a=7,b=8; p1=&a; p2=&b; Draw a picture p1=p2; Draw a picture p1=&a; p2=&b; *p1 = *p2; Draw a picture

Example int *p1, *p2; int a=7,b=8; p1=&a; p2=&b; p1=p2; *p1 = 10; cout << *p2 << " " << b;

Example int *p1, *p2; int a=7,b=8; p1=&a; p2=&b; int *temp; temp=p1; p1=p2; p2=temp; cout << a << " " << b << " " << *p1 << " " << *p2;

new If I write: int *p; I have a pointer but it does not yet point to anything. p = new int; This declares some free memory to which p now points to. *p = 17; // This new memory now holds the value 17. Note this new memory has no name. It is only accessible through p. Through p we have created a variable.

new Often when you are programming you do not know how many objects you will need. We can create these objects as we go using dynamic memory allocation. For example we can write: Product * p; p = new Product; You might say why not just write: Product my_product;

Heap vs stack memory Difference is that objects created with a new command live in heap memory and objects created with ordinary declarations live in the stack memory. When a variable is stored in stack memory it lives in a storage area that is associated with a function in which the variable was declared: void foo() { Product my_product;... } When computer exits foo, my_product automatically dies.

Heap vs stack memory However, when we write Product * p; p = new Product; This memory allocated for the Product is now in heap memory. This Product can only be accessed through the pointer p, but it will stay alive until the programmer explicitly deallocates the memory. So one reason to use pointers is to be able to create objects with longer life span.

Delete To free this memory for other applications I should do: delete p; Important: p is not deleted. Just the memory where p pointed to has been released for other applications. We can still use the pointer p: int x = 3; p = &x; When p is no longer pointing to anything useful set it to NULL. delete p; p=null; NULL is defined in <cstdlib>

Memory management A program will crash if memory runs out! The delete function allows us to free up space in memory, so we don t run over the memory limit. Suppose we have a class Database that stores a lot of data and takes up a lot of memory. Database *p = new Database;... Code... delete p; Now the memory is freed.

Dangling pointers A common run-time error is to use a pointer that doesn't point anywhere. Two dangling pointer errors: Using a pointer that was not initialized. Using a pointer that was deleted. int *p; cout << *p; p = new int; delete p; cout << *p; In both cases, we do not get a compile error. Here p just points to a random spot, usually containing a garbage value.

Example with pointers void fun (int *p, int *&q) { *p = 2; q = p; int c=7; p=&c; return; } int main() { int x=5, y=10; int *a = &x; int *b = &y; fun(a, b); return 0; }