a data type is Types

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

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

Pointers, Dynamic Data, and Reference Types

Memory and C++ Pointers

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

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors

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

Exam 3 Chapters 7 & 9

CMSC 202 Midterm Exam 1 Fall 2015

[0569] p 0318 garbage

CSC1322 Object-Oriented Programming Concepts

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

CS31 Discussion 1E Spring 17 : week 08

Lecture 2, September 4

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

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

Variables, Memory and Pointers

CS242 COMPUTER PROGRAMMING

Array Elements as Function Parameters

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

Kurt Schmidt. October 30, 2018

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

C++ Review. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

WHAT POINTERS REALLY ARE

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

CMSC 341 Lecture 2 Dynamic Memory and Pointers

Chapter 9: Pointers Co C pyr py igh i t gh Pear ea so s n n E ducat ca io i n, n Inc. n c.

Short Notes of CS201

Common Misunderstandings from Exam 1 Material

CS 61C: Great Ideas in Computer Architecture C Pointers. Instructors: Vladimir Stojanovic & Nicholas Weaver

Final exam. Final exam will be 12 problems, drop any 2. Cumulative up to and including week 14 (emphasis on weeks 9-14: classes & pointers)

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

CSCI 2212: Intermediate Programming / C Review, Chapters 10 and 11

CS201 - Introduction to Programming Glossary By

Separate Compilation Model

Dynamic memory. EECS 211 Winter 2019

CS 161 Exam II Winter 2018 FORM 1

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

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

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

Dynamic Allocation in C

Chapter 9: Pointers. Copyright 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.

Chapter 9: Getting the Address of a Variable. Something Like Pointers: Arrays. Pointer Variables 8/23/2014. Getting the Address of a Variable

Introduction to C++ with content from

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

EMBEDDED SYSTEMS PROGRAMMING Language Basics

Motivation was to facilitate development of systems software, especially OS development.

CMSC202 Computer Science II for Majors

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

Structured Data. CIS 15 : Spring 2007

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 6: Pointers

Scientific Computing

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.

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

377 Student Guide to C++

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

Learning Objectives. Introduction to Arrays. Arrays in Functions. Programming with Arrays. Multidimensional Arrays

primitive arrays v. vectors (1)

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

Syntax and Variables

3/22/2016. Pointer Basics. What is a pointer? C Language III. CMSC 313 Sections 01, 02. pointer = memory address + type

Introduction to Programming Using Java (98-388)

10/20/2015. Midterm Topic Review. Pointer Basics. C Language III. CMSC 313 Sections 01, 02. Adapted from Richard Chang, CMSC 313 Spring 2013

Memory Management: The Details

CS201- Introduction to Programming Current Quizzes

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

Heap Arrays and Linked Lists. Steven R. Bagley

Purpose of Review. Review some basic C++ Familiarize us with Weiss s style Introduce specific constructs useful for implementing data structures

377 Student Guide to C++

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

Variables. Data Types.

dynamically allocated memory char* x = new char; int* x = new int[n]; ???...?

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

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

Pointers Pointer Variables. Pointer Variables Getting the Address of a Variable. Each variable in program is stored at a

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, SPRING 2013

BITG 1113: POINTER LECTURE 12

LAB 4.1 Relational Operators and the if Statement

Object oriented programming C++

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

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

C:\Temp\Templates. Download This PDF From The Web Site

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

C++ for Java Programmers

Understand Execution of a Program

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

Outline. 1 Function calls and parameter passing. 2 Pointers, arrays, and references. 5 Declarations, scope, and lifetimes 6 I/O

Understanding Pointers

9.2 Pointer Variables. Pointer Variables CS Pointer Variables. Pointer Variables. 9.1 Getting the Address of a. Variable

Control Structures. A program can proceed: Sequentially Selectively (branch) - making a choice Repetitively (iteratively) - looping

04-24/26 Discussion Notes

Advanced Systems Programming

Exercise: Inventing Language

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

Recap: Pointers. int* int& *p &i &*&* ** * * * * IFMP 18, M. Schwerhoff

C++ Final Exam 2017/2018

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

COMP 2355 Introduction to Systems Programming

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Transcription:

Pointers Class 2

a data type is Types

Types a data type is a set of values a set of operations defined on those values in C++ (and most languages) there are two flavors of types primitive or fundamental types programmer-defined aggregations of primitives

Primitive Types the main primitive types in C++ are

Primitive Types the main primitive types in C++ are void bool char short, int, long, long long float, double, long double char and the integer types can also have modifiers signed unsigned

a variable is Variables

Variables a variable is a symbolic way to refer to a memory location when you declare a variable, space is allocated in the current local memory scope to hold one value of the variable s type the type of the variable indicates how to interpret the bits in that location void foo { int x {5}; double y {1.2}; IntCell i {10};... } int double IntCell foo 5 1.2 10 x y i

Immediate Variables variables like this have immediate storage the bits in the memory location are interpreted directly as that type unsigned short 213 counter signed short 43 my value the bits in each location are literally 1101 0101 how they are interpreted depends on the variable s type

Pointer Variables you can also declare a pointer variable using an asterisk we signify an uninitialized value with? foo void foo() { int x; double y {1.2}; IntCell i {10}; int * p; } int double IntCell int*? 1.2 10? x y i p

Pointer Variables pointer variables have indirect storage the bits at that location represent an address at which to find a value of the declared type Student* fred name... banner ID... GPA... type: Student

Variable Types in Java every primitive variable has immediate storage every object variable is a reference that points to someplace else NO EXCEPTIONS! but in C++ a variable may be an immediate primitive or a pointer to a primitive a variable may be an immediate object or a pointer to an object the programmer has to choose, every time a variable is declared

Declaring Variables an immediate variable is declared with a plain declaration (no asterisk) with or without an initialization int foo; double temperature {12.345}; IntCell i {5}; a pointer variable is declared with an asterisk int * foo; IntCell * i; double * temperature; but not double * temperature {12.345};

Giving a Variable a Value the last example brings up the question: how does a pointer variable get its value? (the address to point to?) for that matter, how does any variable get a value?

Giving a Variable a Value the last example brings up the question: how does a pointer variable get its value? (the address to point to?) for that matter, how does any variable get a value? assignment statement (under the hood in an initialization list) x = 5; y = 3.72;

Getting An Address how do we get an address for a pointer variable? in C++ there are two ways to get the address of something to store in a pointer the first way is to use the address-of operator & int i {5}; int * ip; ip = &i; int 5 i or simply int* ip int i {5}; int * ip {&i};

file 0addr vs content Example

Getting An Address the second way is to use the return value of the new operator the new operator allocates a storage space from the heap for a variable and returns its address IntCell * i {new IntCell{10}}; stored value: 10 IntCell* i int get value() const void set value() type: IntCell

Using a Pointer Variable once a pointer variable has a valid value, it can be used the value in the variable itself is an address, usually not directly useful to get at the value it s pointing to, we must dereference it using the dereference operator * 1 int i {5}; 2 int * ip {&i}; 4 i++; 5 *ip += 5; 6 cout << "i is " << i << endl; 7 cout << "i is " << *ip << endl; picture of memory

Using a Pointer to an Object regardless of whether the pointer points to an object or a primitive, it works the same way however, the syntax presents a problem *intcellptr.set value(5); does not work we must use the dereference-then-select operator -> intcellptr->set value(5); example file 1objectpointer.cpp

Memory Management when you allocate memory with new, that RAM is unavailable for anything else in the computer when you re done with it, you must give it back failure to do so results in a memory leak memory allocated with new is released with delete (C uses malloc and free, but not C++; C++ uses new and delete) there must be a 1-1 correspondence between calls to new and calls to delete in every program

Examples code examples 2dereference 3segfault 4doubledelete