References and Pointers

Slides:



Advertisements
Similar presentations
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Advertisements

A pointer is the memory address of a variable. A memory address is a physical location within a system’s memory space. A pointer variable is variable used.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
CSC1016 Coursework Clarification Derek Mortimer March 2010.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Pointers.
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.
ARRAYS In this Lecture, we will try to develop understanding of some of the relatively complex concepts. The following are explained in this lecture with.
Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.
Dynamic memory allocation and Pointers Lecture 4.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Spring 2005, Gülcihan Özdemir Dağ Lecture 6, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 6 Outline 6.1Introduction.
Pointers Class #9 – Pointers Pointers Pointers are among C++ ’ s most powerful, yet most difficult concepts to master. We ’ ve seen how we can use references.
Lecture 7 Pointers & Refrence 1. Background 1.1 Variables and Memory  When you declare a variable, the computer associates the variable name with a particular.
C Programming Lecture 16 Pointers. Pointers b A pointer is simply a variable that, like other variables, provides a name for a location (address) in memory.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Computer Programming Lecture 12 Pointers Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical & Electronics Engineering
Pointers 1. Introduction Declaring pointer variables Pointer operators Pointer arithmetic 2 Topics to be Covered.
CS 31 Discussion, Week 7 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Pointers  * symbol and & symbol  Pointer operations  Pointer.
Pointers. What Is Pointer l every variable has memory address char c=’y’; int i=2; address of variable i is 0022 l address can used to refer to this variable.
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.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
Intro to Pointers in C CSSE 332 Operating Systems
EGR 2261 Unit 11 Pointers and Dynamic Variables
User Interaction and Variables
Chapter 7 Pointers and C-Strings
Variables Mr. Crone.
© 2016 Pearson Education, Ltd. All rights reserved.
Motivation and Overview
COM S 326X Deep C Programming for the 21st Century Prof. Rozier
Pointers.
Learning Objectives Pointers Pointer in function call
Pointers.
Dynamic Memory CSCE 121 J. Michael Moore.
Lecture 6 C++ Programming
Pointers Psst… over there.
Andy Wang Object Oriented Programming in C++ COP 3330
Pointers and References
Pointers Psst… over there.
CSC 253 Lecture 8.
Introduction to the C Language
Introduction to the C Language
Variables with Memory Diagram
Object Oriented Programming COP3330 / CGS5409
CSC 253 Lecture 8.
CMSC202 Computer Science II for Majors Lecture 04 – Pointers
Pointers, Dynamic Data, and Reference Types
Built-In (a.k.a. Native) Types in C++
Programming Funamental slides
Exceptions CSCE 121 J. Michael Moore
Local Variables, Global Variables and Variable Scope
Pointers Lecture 1 Thu, Jan 15, 2004.
Reference semantics, variables and names
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
Suggested self-checks: Section 7.11 #1-11
ECE 103 Engineering Programming Chapter 35 C Pointers, Part 1
POINTER CONCEPT 4/15/2019.
Data Structures and Algorithms Introduction to Pointers
Pointers and dynamic objects
CS31 Discussion 1H Fall18: week 7
Pointers and References
Dynamic Memory CSCE 121.
Pointers, Dynamic Data, and Reference Types
POINTER CONCEPT 8/3/2019.
Classes and Objects Object Creation
SPL – PS2 C++ Memory Handling.
Presentation transcript:

References and Pointers CSCE 121 Modified from slides created by Carlos Soto

You’ve already seen some datatypes: int long long float double char string *you’ll usually want to stick to the ones in bold If you’ve been keeping up with your reading..

We can create a variable in our code by specifying a datatype and a variable identifier or name. For example: int count; string name;

We can create a variable in our code by specifying a datatype and a variable identifier or name. For example: int count; string name; Datatype is “int”

We can also initialize our variables We can also initialize our variables. For example: int count = 1; string name = "Adam";

Our variables hold values, and we can use those values directly Our variables hold values, and we can use those values directly. For example: int total = count + 10; cout << "My name is " << name;

We can also access values indirectly with two important programming language constructs: Pointers and References Pointers and references are complex topics, and we are only covering them lightly for now. It may not seem apparent yet why they are useful, but they become very important later on.

Pointers are variables whose value is the address in memory of another variable. References are special variables that act as aliases to another variable. (But also have a value that is the address in memory of another variable.) So the value of a pointer is always a memory address. That is, a number. Making sure that this number is a valid address that your program has access to becomes important later for programming safety. References are basically nick-names (or second names) for variables you already have in your program, so you can use them just like you’d use the original variable. The difference is that references have the special property that references work across diference scopes, as we’ll see later. I’m going to show you guys what it looks like to use pointers and references, but don’t worry too much about them for now.

Using pointers: int count = 1; int. countPtr = &count; int Using pointers: int count = 1; int *countPtr = &count; int *countPtr2 = countPtr; cout << *countPtr; This would also work for char, string, etc. countPtr and countPtr2 are both storing the address of count.

Using pointers: int count = 1; int *countPtr = &count; Datatype is “pointer to an int”

Using pointers: int count = 1; int* countPtr = &count; This is equivalent, so the * can go with the datatype or with the identifier. Datatype is “pointer to an int”

Using pointers: int count = 1; int. countPtr = &count; int Using pointers: int count = 1; int *countPtr = &count; int *countPtr2 = countPtr; cout << *countPtr; “address of” operator If we instead assigned the value of count, we’d have an invalid address.

Using pointers: int count = 1; int. countPtr = &count; cout << Using pointers: int count = 1; int *countPtr = &count; cout << *countPtr; “dereferences” the pointer What would happen if we printed countPtr without dereferencing.

Pointers are considered dangerous because it is easy to lose track of the addresses you’re pointing to, and try to dereference memory you don’t “own”. That’s a big no no. So C++ also has References, which some people think of as safer pointers.

Using references int count = 1; int& countRef = count; int& countRef2 = count; int& countRef2 = countRef; cout << countRef;

Using references int count = 1; int& countRef = count; Datatype is “reference to an int”

Using references int count = 1; int& countRef = count; cout << countRef; No “address of” needed No dereferencing needed

Takeaway messages for now: C++ has two special datatypes called Pointers and References. They let you access values indirectly. You need to at least know they exist for now References are safer, usually better than pointers The symbols you’ll see when using pointers and references are: & and *