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

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

Homework #3 CS2255 Fall 2012

Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Exercise 1.1 Hello world

CS2255 HOMEWORK #1 Fall 2012

Exam 1. CSI 201: Computer Science 1 Fall 2018 Professors: Shaun Ramsey

VARIABLES & ASSIGNMENTS

Exam 3 Chapters 7 & 9

C++ For Science and Engineering Lecture 15

CS31 Discussion 1E Spring 17 : week 08

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

REVIEW. The C++ Programming Language. CS 151 Review #2

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

Chapter 1: Object-Oriented Programming Using C++

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.

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

Week 3: Pointers (Part 2)

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

Lab Instructor : Jean Lai

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy

C++ for Java Programmers

What is an algorithm?

Computer Programming

12. Pointers Address-of operator (&)

Unit 14. Passing Arrays & C++ Strings

Pointers and Strings Chapters 10, Pointers and Arrays (10.3) 3.2 Pointers and Arrays (10.3) An array of ints can be declared as

Goals of this Lecture

Exam 2. CSI 201: Computer Science 1 Fall 2016 Professors: Shaun Ramsey and Kyle Wilson. Question Points Score Total: 80

Lab#5 Due Wednesday, February 25, at the start of class. Purpose: To develop familiarity with C++ pointer variables

Homework Assignment #2 (revised)

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

Introduction to Computer Science Midterm 3 Fall, Points

Getting started with C++ (Part 2)

CMSC 202 Midterm Exam 1 Fall 2015

Lecture 23: Pointer Arithmetic

Dynamic Memory Allocation

BENG (HONS) ELECTRONIC ENGINEERING BENG (HONS) TELECOMMUNICATIONS. Cohort: BEE/10A/FT, BEE/10B/FT &BTEL/10B/FT

a data type is Types

Ch 6. Functions. Example: function calls function

Implementing an ADT with a Class

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

pointers & references

CSCE 206: Structured Programming in C++

CSCE 206: Structured Programming in C++

True or False (12 Points)

Today USING POINTERS. Functions: parameters and arguments. Todaywewilllookattopicsrelatingtotheuseofpointers

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

(5-1) Object-Oriented Programming (OOP) and C++ Instructor - Andrew S. O Fallon CptS 122 (February 4, 2019) Washington State University

SYSC 2006 C Winter 2012

CS2141 Software Development using C/C++ C++ Basics

ECE250: Algorithms and Data Structures C++ Language Tutorial

Here, type declares the base type of the array, which is the type of each element in the array size defines how many elements the array will hold

CS 103 Lab - Party Like A Char Star

Review of Important Topics in CS1600. Functions Arrays C-strings

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

A First Program - Greeting.cpp

Sample Final Exam. 1) (24 points) Show what is printed by the following segments of code (assume all appropriate header files, etc.

CS31 Discussion. Jie(Jay) Wang Week8 Nov.18

True or False (15 Points)

Operator overloading

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7.

EINDHOVEN UNIVERSITY OF TECHNOLOGY Department of Mathematics and Computer Science

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

CSCE121: Introduction to Program Design and Concepts Practice Questions for Midterm 1

CS24 Week 3 Lecture 1

CSCE 206: Structured Programming in C++

BITG 1113: POINTER LECTURE 12

Machine Language Instructions Introduction. Instructions Words of a language understood by machine. Instruction set Vocabulary of the machine

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

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

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

C++ Structures Programming Workshop 2 (CSCI 1061U)

Pointers and References

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

Note 12/1/ Review of Inheritance Practice: Please write down 10 most important facts you know about inheritance...

Reference operator (&)

CS31 Discussion. Jie(Jay) Wang Week6 Nov.4

Pointers II. Class 31

Memory and Pointers written by Cathy Saxton

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

Pointers. Variable Declaration. Chapter 10

Integer Data Types. Data Type. Data Types. int, short int, long int

Introduction to Programming using C++

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

Pointers, Arrays and C-Strings

Operators. The Arrow Operator. The sizeof Operator

Pointers in C/C++ 1 Memory Addresses 2

W3101: Programming Languages C++ Ramana Isukapalli

CS 430 Computer Architecture. C/Assembler Arithmetic and Memory Access William J. Taffe. David Patterson

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

Array Elements as Function Parameters

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors

C++ ARRAYS POINTERS POINTER ARITHMETIC. Problem Solving with Computers-I

Computer Programming : C++

8. The C++ language, 1. Programming and Algorithms II Degree in Bioinformatics Fall 2017

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

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

Tutorial 12 Craps Game Application: Introducing Random Number Generation and Enumerations

Functions, Arrays & Structs

Transcription:

pointers + memory computer have memory to store data. every program gets a piece of it to use as we create and use more variables, more space is allocated to a program memory int x; double x; string a; main overhead int y; program 2 main overhead program 1 overhead is just some extra memory the computer needs to use to manage everything CS31 Fall 2014 Discussion 7: pointers

recall function spaces remember how functions create spaces to do operations and store data code memory int x 3 2 int fxn2() { int x = 10; return 5; int fxn1() { return fxn2(); 4 int n 1 fxn1() int n 2 fxn2() fxn1() int n 3 1 int { int n = 5; fxn1(); 5 fxn1() int n int n 5 4

memory addresses every location in memory has it memory int x 0 variable at address 0 is x value is 10 fxn2() 1 fxn1() int n 2 3 4 variable at address 3 is n value is 5..

pointers variables that can point to memory addresses pointer operations: int* x; type name declares a pointer to an integer x pointer int y 10 name value x = &y; assigns pointer to address of y x int y 10 pointer name value a = *x; a = x int y 10 dereferences pointer for access to value x points to pointer name value *x = 5; x int y 5 dereferences pointer to set the value x points to pointer name value

pointer basics code #include <iostream> using namespace std; output int { int x, y; int *px; x = 2; y = -5; declares a pointer to an integer px = &x; cout << px: << px << endl; cout << *px: << *px << endl; px = &y; cout << px: << px << endl; cout << *px: << *px << endl; assign s x s memory address to px assign s y s memory address to px px: 0012AA3C *px: 2 px: 0012AA4C *px: -5

pointers practice int x = 5; int* px = &x; int y = 15; int* py = &y; int a = -4; int* pa = &a; int* py2 = &y; some statements (changes persist) cout << x + y cout << *px + *py y = *px; cout << y; px = py; cout << *px; what s the output? py = pa; *pa = y; cout << *py; 5 20 20 5 5 px = *x; cout << *px; cout << *py2 + *py + *px + *pa; error 20

pointers + arrays array variables are actually pointers that can t point to anything else (pointee is fixed) x pointer 0 1 4 9 16 25 36 49 64 81 0 1 2 3 4 5 6 7 8 9 x points to the first element in the array. Note that in memory, the elements of arrays are laid out consecutively. Thus, if we know the location of the first element, then we can get to any element int x[10]; cout << x; cout << x[0]; cout << *x; cout << &x[0]; cout << &x[1]; cout << x + 1; declares a pointer to an integer prints the array s address prints the value of the first element also prints the value of the first element prints the first element s address, which is also the array s address prints the second element s address also prints the second element s address

pointers arithmetic when you print memory addresses, they are not consecutive, why is that? 0A1400 0A1404 0A1408 0A140C 0A1410 x[0] x[1] x[2] x[3] x[4] because memory addresses are byte-aligned, and an integer takes 4 bytes cout << x; cout << x + 1; cout << x + 2; prints the array s address: 0A1400 prints the 2nd element s address: 0A1404 prints the 3rd element s address: 0A1408 because x is pointing to an integer, it automatically knows to increment x by 4 to get to the address of the next integer. now let s dereference x *x dereferences x, same as x[0]; *(x + 1) gets x s 2nd element, same as x[1]; *(x + 2) *(x + 5). gets x s 3rd element, same as gets x s 6th element, same as x[2]; x[5];.

pointer arithmetic practice x 1 0 2 10 3 5 4 y 5-4 3 2 4 0 1 0 0 4 0 1 2 3 4 5 6 0 1 2 3 4 5 6 7 8 9 some statements *x + *y *(x + 9) + *(y + 4) what s the output? 6 error y[9] + *(x + 4) 7 *(y + *x) -4 y[*(x + 1) + *(x + 6)] 4 *(x + y[9]) 3 *(x + *(y + *(x + *(y + 9)))) 2 *(x + *(y + *x)) error

pointers + array snippets convert these code snippets to use pointer access instead of array access const int N = 7; double x[n] = {5, 10, 20, -10, 20, 100, -20; double* px = &x; sum += *(x + i); double sum = 0.0; for (int i = 0; i < N; i++) { reports the sum of the array: 125 sum += x[i]; cout << sum: << sum << endl; const int N = 7; double x[n] = {5, 10, 20, -10, 20, 100, -20; double* px = &x; double sum = 0.0; for (int i = 0; i < N; i++) { sum += x[i]; x[i] = sum; cout << sum: << sum << endl; const int N = 7; double x[n] = {5, 10, 20, -10, 20, 100, -20; double* px = &x; for (int i = 1; i < N; i++) { x[i] = x[i] + x[i-1]; cout << sum: << x[n-1] << endl; sum += *(x + i); *(x + i) = sum; reports the sum of the array: 125 sets x to the following: 5 15 35 25 45 145 125 0 1 2 3 4 5 6 *(x + i) += *(x + i - 1)... << *(x + N - 1) <<... also reports the sum of the array: 85 also sets x to the following: 5 15 35 25 45 145 125 0 1 2 3 4 5 6

pointers + functions example function header for sum function with some pointer input int sum(int* a, int n) why the n? again arrays or pointers to the beginning of arrays don t know their own size. pointers as input arguments: 1. you can change a in the function and when it finishes a will keep those changes 2. to protect a from being changed, define the input argument as const (i.e. const int* a ) if a is declared as const, cout << *(a + 5); *(a + 5) = 10; okay compile error to call the function: use the pointer name (no extra asterisks): int sum = sum(x, 10); int sum = sum(*x, 10); compile error, since *x is an integer and not an integer pointer can my function return a pointer? YES! well sort of. sum can return a pointer to the array, just do int* sum(int* a, int n)

sum example convert to using pointers return type function name input array input size int sum(int x[], int n) { int sum = 0; for (int i = 0; i < n; i++) { sum += x[i]; return sum;

sum example int *x and int x[] are pretty much equivalent for parameter declarations return type function name input array input size int sum(int *x, int n) { int sum = 0; for (int i = 0; i < n; i++) { sum += *(x + i); return sum;

sum example v2 int *x and int x[] are pretty much equivalent for parameter declarations return type function name input array input size int sum(int *x, int n) { int sum = 0; for (int i = 0; i < n; i++) { sum += *x; x++; return sum;

deleting duplicates in arrays with pointer access If there s a duplicate, return it. Else, return -1 int find_duplicate(int* x, int n) { for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (*(x + i) == *(x + j)) { return *(x + i); return -1; Cool, but let s go and delete that duplicate from the array, so let s return the position of the first instance, so we can remove it int find_duplicate(int* x, int n) { for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (*(x + i) == *(x + j)) { return i; return -1; Now how do we delete it? We can t just set it to 0, that s another value. We ll replace it with the last element, and then say the array has shrank. int remove_dup(int* x, int n, int index) { *(x + index) = *(x + n - 1); return n - 1; Shoots, what if there s more duplicates? How do we remove them all? Just do it over and over again. int remove_duplicates(int* x, int n) { int dup_index = find_duplicate(x, n); while (dup_index!= -1) { n = remove_dup(x, n, dup_index); return n; Lastly, put it to the test! int { int x[9] = {1, 2, 1, 0, 2, 1, 2, 5, 1; int* px = &x, py = &x; int s1 = remove_duplicates(px + 3, 8); int s2 = remove_duplicates(px, s1); int s3 = remove_duplicates(py, 8); How s x look after each call? what s s1, s2, s3?