CS31 Discussion 1E Spring 17 : week 08
|
|
- Ethelbert Jennings
- 5 years ago
- Views:
Transcription
1 CS31 Discussion 1E Spring 17 : week 08 TA: Bo-Jhang Ho bojhang@cs.ucla.edu Credit to former TA Chelsea Ju
2 Project 5 - Map cipher to crib Approach 1: For each pair of positions, check two letters in cipher and crib are both identical or different For each pair of positions pos1 and pos2, cipher[pos1] == cipher[pos2] should equal to crib[pos1] == crib[pos2] Approach 2: Get the positions of the letter and compare For each position pos, indexes1 = getallpositions(cipher, pos, cipher[pos]) indexes2 = getallpositions(crib, pos, crib[pos]) indexes1 should equal to indexes2
3 Project 5 - Map cipher to crib Approach 3: Generate the mapping We first have a mapping array char cipher2crib[128] = { \0 ; Then whenever we attempt to map lettera in cipher to letterb in crib, we first check whether it violates the previous setup: Is cipher2crib[lettera]!= \0? // implies lettera has been used Then cipher2crib[lettera] should equal to letterb If no violation happens, cipher2crib[lettera] = letterb;
4 Road map of CS31 String Array Function Class Variable If / else Loops Pointer!!
5 Cheat sheet Data type int *a; a is a variable, whose data type is int * a stores an address of some integer Use as verbs & - address-of operator &b means I want to get the memory address of variable b * - dereference operator *c means I want to retrieve the value in address c
6 Memory model byte
7 Memory model byte = 8 bits off on A bit has 2 states
8 Memory model byte = 8 bits off on A bit has 2 states A byte has 256 (=2 8 ) states
9 Memory model byte = 8 bits off on A bit has 2 states A byte has 256 (=2 8 ) states A char takes 1 byte An int takes 4 bytes A double takes 8 bytes
10 Data type review Basic data types Type Bytes Bits Value range char to 127 short ,768 to 32,767 int ,147,483,648 to 2,147,483,647 long long * 10^18 to 9 * 10^18 float * 10^38 to 3.4 * 10^38 double * 10^308 to 1.7 * 10^308
11 Memory model byte = 8 bits off on A bit has 2 states A byte has 256 (=2 8 ) states My has 16GB ram
12 Memory model byte = 8 bits off on A bit has 2 states A byte has 256 (=2 8 ) states My has 16GB ram = 16,000,000,000 bytes!
13 What s going on in the memory int a = 5; int b = 3; double c = 3.5; int d = b - a;
14 What s going on in the memory a 5 int a = 5; int b = 3; double c = 3.5; int d = b - a;
15 What s going on in the memory a 5 int a = 5; int b = 3; double c = 3.5; int d = b - a; b
16 What s going on in the memory a 5 int a = 5; int b = 3; double c = 3.5; int d = b - a; b 3 c
17 What s going on in the memory a 5 int a = 5; int b = 3; double c = 3.5; int d = b - a; b 3 c 3.5 d 2
18 What s going on in the memory int a = 5; int *b = &a; a++; *b += 2; cout << a << endl; cout << *b << endl;
19 What s going on in the memory a 5 int a = 5; int *b = &a; a++; *b += 2; cout << a << endl; cout << *b << endl;
20 What s going on in the memory a 5 int a = 5; int *b = &a; a++; *b += 2; cout << a << endl; cout << *b << endl;
21 What s going on in the memory a 5 int a = 5; int *b; b = &a; // declare a pointer var // set address a++; *b += 2; cout << a << endl; cout << *b << endl;
22 What s going on in the memory a 5 int a = 5; int *b; b = &a; // declare a pointer var // set address a++; *b += 2; b?? (address) cout << a << endl; cout << *b << endl;
23 What s going on in the memory a 5 int a = 5; int *b; b = &a; // declare a pointer var // set address a++; *b += 2; b (address) cout << a << endl; cout << *b << endl;
24 What s going on in the memory a 5 à 6 int a = 5; int *b; b = &a; // declare a pointer var // set address a++; *b += 2; b (address) cout << a << endl; cout << *b << endl;
25 What s going on in the memory a 6 à 8 int a = 5; int *b; b = &a; // declare a pointer var // set address a++; *b += 2; b (address) cout << a << endl; cout << *b << endl;
26 What s going on in the memory a 8 int a = 5; int *b; b = &a; // declare a pointer var // set address a++; *b += 2; b (address) cout << a << endl; cout << *b << endl; Output 8
27 What s going on in the memory a 8 int a = 5; int *b; b = &a; // declare a pointer var // set address a++; *b += 2; b (address) cout << a << endl; cout << *b << endl; Output 8 8
28 Using uninitialized variables is always dangerous int a; int b = a + 3; int *c; *c = 27;
29 Using uninitialized variables is always dangerous a?? int a; int b = a + 3; int *c; *c = 27;
30 Using uninitialized variables is always dangerous a?? int a; int b = a + 3; int *c; b?? *c = 27;
31 Using uninitialized variables is always dangerous a?? int a; int b = a + 3; int *c; b?? *c = 27; c?? (address)
32 Using uninitialized variables is always dangerous a?? int a; int b = a + 3; int *c; b?? *c = 27; c?? (address) May manipulate a piece of memory not belonging to this program!!
33 Array v.s. Pointer Array is a special case of pointer Pointer can be treated as an array
34 Array in memory int a = 3; int b[5] = {10, 20, 30, 40, 50; int *c = &b[1]; *c = 100;
35 Array in memory a int a = 3; int b[5] = {10, 20, 30, 40, 50; int *c = &b[1]; 1010 *c = 100;
36 Array in memory a b[0] 3 10 int a = 3; int b[5] = {10, 20, 30, 40, 50; int *c = &b[1]; b[1] 20 *c = 100; b b[2] b[3] b[4]
37 Array in memory a b[0] 3 10 int a = 3; int b[5] = {10, 20, 30, 40, 50; int *c = &b[1]; b[1] 20 *c = 100; b b[2] b[3] b[4]
38 Array in memory b a b[0] b[1] b[2] b[3] int a = 3; int b[5] = {10, 20, 30, 40, 50; int *c; // declare a pointer c = &b[1]; // get address *c = 100; b[4]
39 Array in memory b a b[0] b[1] b[2] b[3] int a = 3; int b[5] = {10, 20, 30, 40, 50; int *c; // declare a pointer c = &b[1]; // get address *c = 100; b[4] c 1024?? (address)
40 Array in memory b a b[0] b[1] b[2] b[3] int a = 3; int b[5] = {10, 20, 30, 40, 50; int *c; // declare a pointer c = &b[1]; // get address *c = 100; b[4] c 1024 (address)
41 Array in memory b a b[0] b[1] b[2] b[3] 20 à int a = 3; int b[5] = {10, 20, 30, 40, 50; int *c; // declare a pointer c = &b[1]; // get address *c = 100; b[4] c 1024 (address)
42 Time and Delta analogy Time + Delta = Time Delta + Delta = Delta Delta + Time = Time Time + Time = Doesn t make sense!
43 Time and Delta analogy + = + = + = + = Doesn t make sense!
44 Time and Delta analogy Pointer + Int = Pointer Int + Int = Int Int + Pointer = Pointer Pointer + Pointer = Doesn t make sense!
45 Time and Delta analogy Time - Delta = Time Delta - Delta = Delta Delta - Time = Doesn t make sense! Time - Time =??
46 Time and Delta analogy Time - Delta = Time Delta - Delta = Delta Delta - Time = Doesn t make sense! Time - Time = Delta
47 Time and Delta analogy - = = = = Doesn t make sense!
48 Time and Delta analogy Pointer - Int = Pointer Int - Int = Int Int - Pointer = Doesn t make sense! Pointer - Pointer = Int
49 Array in memory a b[0] 3 10 int a = 3; int b[5] = {10, 20, 30, 40, 50; int *c = &b[1]; b b[1] b[2] c = c + 1; // Or, // c += 1; // c++; b[3] b[4] *c = 27; c 1024 (address)
50 Array in memory a b[0] 3 10 int a = 3; int b[5] = {10, 20, 30, 40, 50; int *c = &b[1]; b b[1] b[2] c = c + 1; // Or, // c += 1; // c++; b[3] b[4] *c = 27; c 1024 à (address) What? + 1 =?
51 Array in memory a b[0] 3 10 int a = 3; int b[5] = {10, 20, 30, 40, 50; int *c = &b[1]; b b[1] b[2] c = c + 1; // Or, // c += 1; // c++; b[3] 40 *c = 27; b[4] c à (address) Pointer arithmetic We should interpret as adding the memory size of 1 integer
52 Array in memory a b[0] 3 10 int a = 3; int b[5] = {10, 20, 30, 40, 50; int *c = b; b b[1] b[2] b[3] c += 2; *c = 27; b[4] Array name is a pointer c 1024 (address) b can be treated as an int*
53 Array in memory a b[0] 3 10 int a = 3; int b[5] = {10, 20, 30, 40, 50; int *c = b; b b[1] b[2] b[3] c += 2; *c = 27; b[4] Array name is a pointer c 1024 à (address) b can be treated as an int*
54 Array in memory a b[0] 3 10 int a = 3; int b[5] = {10, 20, 30, 40, 50; int *c = b; b b[1] b[2] b[3] 30 à c += 2; *c = 27; b[4] Array name is a pointer c 1024 (address) b can be treated as an int*
55 More about array Array name can be considered as start point Technically, it s the base address The index can be considered as the offset
56 More about array a 3 b[-4] b[-3] b[-2] b[-1] b[0] 10 b[0] b[1] 20 b[1] b b[2] 27 b[2] b[3] 40 b[3] b[4] c (address) b[4] b[5] b[6] b[7] b[8] int a = 3; int b[5] = {10, 20, 30, 40, 50; int *c = b; cout << b[-1] << endl; b[9]
57 Use pointer like an array If x is a pointer, you can treat x as an array Meaning, you can have something like x[3]
58 Treat pointer as an array a 3 b[0] 10 b[1] 20 b b[2] b[3] b[4] int a = 3; int b[5] = {10, 20, 30, 40, 50; int *c = b + 2; c 1024 (address) cout << c[-2] << endl;
59 Treat pointer as an array a 3 b[0] 10 b[1] 20 b b[2] b[3] b[4] c[0] int a = 3; int b[5] = {10, 20, 30, 40, 50; int *c = b + 2; c 1024 (address) cout << c[-2] << endl;
60 Treat pointer as an array a 3 b[0] 10 b[1] 20 b b[2] b[3] b[4] c[0] c[1] c[2] int a = 3; int b[5] = {10, 20, 30, 40, 50; int *c = b + 2; c 1024 (address) c[3] c[4] cout << c[-2] << endl; c[5] c[6] c[7]
61 Treat pointer as an array a c[-4] 3 c[-6] c[-3] b[0] 10 c[-2] b[1] 20 c[-1] b b[2] b[3] b[4] c[0] c[1] c[2] int a = 3; int b[5] = {10, 20, 30, 40, 50; int *c = b + 2; c 1024 (address) c[3] c[4] cout << c[-2] << endl; c[5] c[6] c[7]
62 Treat pointer as an array a c[-4] 3 c[-6] c[-3] b[0] 10 c[-2] b[1] 20 c[-1] b b[2] b[3] b[4] c[0] c[1] c[2] int a = 3; int b[5] = {10, 20, 30, 40, 50; int *c = b + 2; c 1024 (address) c[3] c[4] cout << c[-2] << endl; c[5] c[6] It is actually a valid memory access c[7]
63 Summary Array name is a pointer Pointer arithmetic Let s say x is a pointer, n is an integer x + n is a pointer (memory address) after n elements of x x - n is a pointer before n elements of x Treat a pointer as an array Again, let s say x is a pointer, n is an integer x[n] means to access n th element counted from x x[n] equivalent to *(x + n)
64 Remind a previous example 01 - Pass an array to a function From caller: In the function: int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10; foo(arr); or or void foo(int params[10]) { void foo(int params[]) { void foo(int *params) {
65 Remind a previous example 02 - Mapping of cipher and crib in project 5 Approach 3: Generate the mapping We first have a mapping array char cipher2crib[128] = { \0 ; Whenever we attempt to map lettera in cipher to letterb in crib: cipher2crib[lettera] = letterb; But there are only 26 letters. Why do we want to have an array with 128 elements? We use ascii as a key (a.k.a index) in the array We don t need to worry about the offset (i.e., x - a ) Is there any solution to reduce the size to 26?
66 Remind a previous example 02 - Mapping of cipher and crib in project 5 bool check(const char cipher[], const char crib[]) { // assume cipher and crib have the same length char cipher2crib[128] = {'\0'; for (int i = 0; cipher[i]!= '\0'; i++) { char lettera = cipher[i]; char letterb = crib[i]; if (cipher2crib[lettera]!= '\0 && cipher2crib[lettera]!= letterb) return false; cipher2crib[lettera] = letter return true;
67 Remind a previous example 02 - Mapping of cipher and crib in project 5 bool check(const char cipher[], const char crib[]) { // assume cipher and crib have the same length char truearray[26] = {'\0'; char *cipher2crib = truearray 'a'; for (int i = 0; cipher[i]!= '\0'; i++) { char lettera = cipher[i]; char letterb = crib[i]; if (cipher2crib[lettera]!= '\0 && cipher2crib[lettera]!= letterb) return false; cipher2crib[lettera] = letter return true; cipher2crib 97 elements truearray 26 elements Memory footprint
68 Question 01 If we declare int *ptr; and int val;, the following two usages are valid: ptr = &val; *ptr = val; What is &ptr?
69 Question 01 If we declare int *ptr; and int val;, the following two usages are valid: ptr = &val; *ptr = val; What is &ptr? &ptr means the address of ptr The type of &ptr is int** (we call it double pointer) For example, int** strongptr = &ptr;
70 Double pointer a int a = 3; int *b = &a; int c[5] = {10, 20, 30, 40, 50; int** d = &b;
71 Double pointer a b 3 (address) int a = 3; int *b = &a; int c[5] = {10, 20, 30, 40, 50; int** d = &b;
72 Double pointer a b 3 (address) int a = 3; int *b = &a; int c[5] = {10, 20, 30, 40, 50; int** d = &b; c[0] 10 c[1] 20 c c[2] c[3] c[4]
73 Double pointer a b 3 (address) int a = 3; int *b = &a; int c[5] = {10, 20, 30, 40, 50; int** d = &b; c[0] 10 c[1] 20 c c[2] c[3] c[4] d 1032 (address)
74 Question 02 If we declare int *ptr; and int val;, the following two usages are valid: ptr = &val; *ptr = val; What is *val?
75 Question 02 If we declare int *ptr; and int val;, the following two usages are valid: ptr = &val; *ptr = val; What is *val? It won t compile * operator (dereference) implies that what it stores is a memory address Only pointer variables store memory address
76 Different levels of pointers If we have Then they have the following relations int val; int* ptr1; int** ptr2; int*** ptr3; int**** ptr4; int***** ptr5; ptr5 = &ptr4; ptr4 = &ptr3; ptr3 = &ptr2; ptr4 = *ptr5; ptr3 = *ptr4; ptr2 = *ptr3; ptr2 = &ptr1; ptr1 = *ptr2; ptr1 = &val; of val = *ptr1; Deference
77 Question 03 If we declare int *ptr;, can we hardcode an address and assign to ptr? For example, ptr = 1234; No, it won t compile For security issue It doesn t make sense that we can get an address beforehand The same variable can reside in different parts of memory in different executions
78 Question 03 int a; cout << &a << endl;
79 Question 04 If we declare int *ptr; and double val;, is the following code valid? ptr = &val;
80 Question 04 If we declare int *ptr; and double val;, is the following code valid? ptr = &val; No, it won t compile Pointers are type-aware We can cast the type: ptr = (int*) &val; However, that means we use the way we interpret integer to intepret a piece of memory which stores a double
81 Array in memory a 3.5 double a = 3.5; int *b = (int*) &a; b (address) cout << *b << endl;
82 Array in memory a 3.5 double a = 3.5; int *b = (int*) &a; b (address) cout << *b << endl;
83 Question 05 If we declare int *ptri; and double *ptrd;, can we have the following assignment? ptri = ptrd; No, it won t compile Pointer type doesn t match Though both store memory addresses, how they interpret the memory content are different We can cast the type: ptri = (int*) ptrd;
84 Checkpoint Looping over the array Return a pointer double* findfirstnegativeptr(double a[], int n) { for (double* p = a; p < a + n; p++) { if (*p < 0) return p; return nullptr; Return an index int findfirstnegativeidx(double a[], int n) { for (int i = 0; i < n; i++) { if (a[i] < 0) return i; return -1;
85 Project 6 Problem 1b probably is the most tricky question.
86 Road map of CS31 String Array Function Class Variable If / else Loops Pointer!!
87 Class Define a data structure A data structure groups different variables together For example, when we describe a 2d coordinate, naturally we use 2 numbers to represent it We can also say we declare a new data type
88 Example // create a new data type class Point { public: double x; double y; ; // how we use it Point p; p.x = 1.1; p.y = 2.2; Point r = p; r.y = 3.3; cout << "Point 1: " << p.x << " " << p.y << endl; cout << "Point 2: " << r.x << " " << r.y << endl;
CS31 Discussion. Jie(Jay) Wang Week8 Nov.18
CS31 Discussion Jie(Jay) Wang Week8 Nov.18 Outline Pointer Struct Memory Management When the program gets executed, it gets some amount of memory allocated for use. memory Program 1 Program 2 Memory Management
More informationa data type is Types
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
More informationpointers + memory double x; string a; int x; main overhead int y; main overhead
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;
More informationPointers, Dynamic Data, and Reference Types
Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation The new operator The delete operator Dynamic Memory Allocation for Arrays 1 C++ Data Types simple
More informationAgenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011
CS 61C: Great Ideas in Computer Architecture (Machine Structures) Introduction to C (Part II) Instructors: Randy H. Katz David A. Patterson http://inst.eecs.berkeley.edu/~cs61c/sp11 Spring 2011 -- Lecture
More informationPointer Arithmetic. Lecture 4 Chapter 10. Robb T. Koether. Hampden-Sydney College. Wed, Jan 25, 2017
Pointer Arithmetic Lecture 4 Chapter 10 Robb T. Koether Hampden-Sydney College Wed, Jan 25, 2017 Robb T. Koether (Hampden-Sydney College) Pointer Arithmetic Wed, Jan 25, 2017 1 / 36 1 Pointer Arithmetic
More informationHomework #3 CS2255 Fall 2012
Homework #3 CS2255 Fall 2012 MULTIPLE CHOICE 1. The, also known as the address operator, returns the memory address of a variable. a. asterisk ( * ) b. ampersand ( & ) c. percent sign (%) d. exclamation
More informationLecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?
Lecture 14 No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions? Friday, February 11 CS 215 Fundamentals of Programming II - Lecture 14 1 Outline Static
More informationPointer Basics. Lecture 13 COP 3014 Spring March 28, 2018
Pointer Basics Lecture 13 COP 3014 Spring 2018 March 28, 2018 What is a Pointer? A pointer is a variable that stores a memory address. Pointers are used to store the addresses of other variables or memory
More informationCSCI-1200 Data Structures Fall 2017 Lecture 5 Pointers, Arrays, & Pointer Arithmetic
CSCI-1200 Data Structures Fall 2017 Lecture 5 Pointers, Arrays, & Pointer Arithmetic Review from Letctures 3 & 4 C++ class syntax, designing classes, classes vs. structs; Passing comparison functions to
More informationPIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics
PIC 10A Pointers, Arrays, and Dynamic Memory Allocation Ernest Ryu UCLA Mathematics Pointers A variable is stored somewhere in memory. The address-of operator & returns the memory address of the variable.
More informationCS201- Introduction to Programming Current Quizzes
CS201- Introduction to Programming Current Quizzes Q.1 char name [] = Hello World ; In the above statement, a memory of characters will be allocated 13 11 12 (Ans) Q.2 A function is a block of statements
More informationStructured Data. CIS 15 : Spring 2007
Structured Data CIS 15 : Spring 2007 Functionalia HW4 Part A due this SUNDAY April 1st: 11:59pm Reminder: I do NOT accept LATE HOMEWORK. Today: Dynamic Memory Allocation Allocating Arrays Returning Pointers
More informationPointers. Lecture 1 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 14, 2015
Pointers Lecture 1 Sections 10.1-10.2 Robb T. Koether Hampden-Sydney College Wed, Jan 14, 2015 Robb T. Koether (Hampden-Sydney College) Pointers Wed, Jan 14, 2015 1 / 23 1 Pointers 2 Pointer Initialization
More informationCS2351 Data Structures. Lecture 7: A Brief Review of Pointers in C
CS2351 Data Structures Lecture 7: A Brief Review of Pointers in C 1 About this lecture Pointer is a useful object that allows us to access different places in our memory We will review the basic use of
More informationGoals of this Lecture
C Pointers Goals of this Lecture Help you learn about: Pointers and application Pointer variables Operators & relation to arrays 2 Pointer Variables The first step in understanding pointers is visualizing
More information2. First Program Stuff
CSE 232 First Midterm, Overview: 1. Getting Started 1. we got started 2. First Program Stuff 1. Compiler vs. Intepreter a. do you know all the steps to create an executable? 2. Variables are declared a.
More informationDiscussion 1E. Jie(Jay) Wang Week 10 Dec.2
Discussion 1E Jie(Jay) Wang Week 10 Dec.2 Outline Dynamic memory allocation Class Final Review Dynamic Allocation of Memory Recall int len = 100; double arr[len]; // error! What if I need to compute the
More informationPointers. Memory. void foo() { }//return
Pointers Pointers Every location in memory has a unique number assigned to it called it s address A pointer is a variable that holds a memory address A pointer can be used to store an object or variable
More informationReview: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements
Review: Exam 1 9/20/06 CS150 Introduction to Computer Science 1 1 Your First C++ Program 1 //*********************************************************** 2 // File name: hello.cpp 3 // Author: Shereen Khoja
More informationPointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology
Introduction to C++ Massachusetts Institute of Technology ocw.mit.edu 6.096 Pointers 1 Background 1.1 Variables and Memory When you declare a variable, the computer associates the variable name with a
More informationCS113: Lecture 5. Topics: Pointers. Pointers and Activation Records
CS113: Lecture 5 Topics: Pointers Pointers and Activation Records 1 From Last Time: A Useless Function #include void get_age( int age ); int age; get_age( age ); printf( "Your age is: %d\n",
More informationCS 61C: Great Ideas in Computer Architecture. C Arrays, Strings, More Pointers
CS 61C: Great Ideas in Computer Architecture C Arrays, Strings, More Pointers Instructor: Justin Hsia 6/20/2012 Summer 2012 Lecture #3 1 Review of Last Lecture C Basics Variables, Functions, Flow Control,
More informationExam 1 Practice CSE 232 Summer 2018 (1) DO NOT OPEN YOUR EXAM BOOKLET UNTIL YOU HAVE BEEN TOLD TO BEGIN.
Name: Section: INSTRUCTIONS: (1) DO NOT OPEN YOUR EXAM BOOKLET UNTIL YOU HAVE BEEN TOLD TO BEGIN. (2) The total for the exam is 100 points (3) There are 8 pages with 32 problem; 15 multiple-choice, 15
More informationMemory, Data, & Addressing II CSE 351 Spring
Memory, Data, & Addressing II CSE 351 Spring 2018 http://xkcd.com/138/ Review Questions 1) If the word size of a machine is 64-bits, which of the following is usually true? (pick all that apply) a) 64
More information9.2 Pointer Variables. Pointer Variables CS Pointer Variables. Pointer Variables. 9.1 Getting the Address of a. Variable
CS 1400 Chapter 9 9.1 Getting the Address of a Variable A variable has: Name Value Location in a memory Type The location in memory is an address Use address operator & to get address of a variable: int
More informationExam 3 Chapters 7 & 9
Exam 3 Chapters 7 & 9 CSC 2100-002/003 29 Mar 2017 Read through the entire test first BEFORE starting Put your name at the TOP of every page The test has 4 sections worth a total of 100 points o True/False
More informationAgenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures
The main body and cout Agenda 1 Fundamental data types Declarations and definitions Control structures References, pass-by-value vs pass-by-references The main body and cout 2 C++ IS AN OO EXTENSION OF
More informationPointers, Arrays and C-Strings
Pointers, Arrays and C-Strings Data Processing Course, I. Hrivnacova, IPN Orsay Variable in Memory Pointers Nullptr Pointers vs References C-Arrays C-Strings Problems with C-Arrays, C-Strings 1 Variables
More informationC++ ARRAYS POINTERS POINTER ARITHMETIC. Problem Solving with Computers-I
C++ ARRAYS POINTERS POINTER ARITHMETIC Problem Solving with Computers-I General model of memory Sequence of adjacent cells Each cell has 1-byte stored in it Each cell has an address (memory location) Memory
More informationCS 31 Review Sheet. Tau Beta Pi - Boelter Basics 2. 2 Working with Decimals 2. 4 Operators 3. 6 Constants 3.
CS 31 Review Sheet Tau Beta Pi - Boelter 6266 Contents 1 Basics 2 2 Working with Decimals 2 3 Handling Strings and Numbers with stdin/stdout 3 4 Operators 3 5 Common Mistakes/Misconceptions 3 6 Constants
More informationCSCI 2212: Intermediate Programming / C Review, Chapters 10 and 11
... 1/16 CSCI 2212: Intermediate Programming / C Review, Chapters 10 and 11 Alice E. Fischer February 3, 2016 ... 2/16 Outline Basic Types and Diagrams ... 3/16 Basic Types and Diagrams Types in C C has
More informationCS31 Discussion. Jie(Jay) Wang Week6 Nov.4
CS31 Discussion Jie(Jay) Wang Week6 Nov.4 Outline Project 4 Array C Strings Project 4 DDL: Monday, November 7 Read the spec & FAQ carefully Incremental development You can call your function in other functions
More informationCSCI-1200 Data Structures Spring 2014 Lecture 5 Pointers, Arrays, Pointer Arithmetic
CSCI-1200 Data Structures Spring 2014 Lecture 5 Pointers, Arrays, Pointer Arithmetic Announcements: Test 1 Information Test 1 will be held Monday, February 10th, 2014 from 6-7:50pm, Lab sections 1-5 and
More informationCSCI 262 Data Structures. Arrays and Pointers. Arrays. Arrays and Pointers 2/6/2018 POINTER ARITHMETIC
CSCI 262 Data Structures 9 Dynamically Allocated Memory POINTERS AND ARRAYS 2 Arrays Arrays are just sequential chunks of memory: Arrays and Pointers Array variables are secretly pointers: x19 x18 x17
More informationCS2141 Software Development using C/C++ C++ Basics
CS2141 Software Development using C/C++ C++ Basics Integers Basic Types Can be short, long, or just plain int C++ does not define the size of them other than short
More informationLecture 2, September 4
Lecture 2, September 4 Intro to C/C++ Instructor: Prashant Shenoy, TA: Shashi Singh 1 Introduction C++ is an object-oriented language and is one of the most frequently used languages for development due
More informationCSCI-1200 Data Structures Spring 2017 Lecture 5 Pointers, Arrays, Pointer Arithmetic
CSCI-1200 Data Structures Spring 2017 Lecture 5 Pointers, Arrays, Pointer Arithmetic Announcements Submitty iclicker registration is still open. Even if you already registered on the iclicker website,
More informationExercise: Using Numbers
Exercise: Using Numbers Problem: You are a spy going into an evil party to find the super-secret code phrase (made up of letters and spaces), which you will immediately send via text message to your team
More informationCS31 Discussion 1E Spring 17 : week 05
CS31 Discussion 1E Spring 17 : week 05 TA: Bo-Jhang Ho bojhang@cs.ucla.edu Credit to former TA Chelsea Ju Road map of CS31 String Array Function Class Variable If / else Loops Pointer Today s Agenda }
More informationLecture 8: Pointer Arithmetic (review) Endianness Functions and pointers
CSE 30: Computer Organization and Systems Programming Lecture 8: Pointer Arithmetic (review) Endianness Functions and pointers Diba Mirza University of California, San Diego 1 Q: Which of the assignment
More informationIntro to C: Pointers and Arrays
Lecture 4 Computer Science 61C Spring 2017 January 25th, 2017 Intro to C: Pointers and Arrays 1 Administrivia Teaching Assistants: Let s try that again. Lectures are recorded. Waitlist/Concurrent Enrollment
More informationTHE INTEGER DATA TYPES. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)
THE INTEGER DATA TYPES STORAGE OF INTEGER TYPES IN MEMORY All data types are stored in binary in memory. The type that you give a value indicates to the machine what encoding to use to store the data in
More informationREFERENCES, POINTERS AND STRUCTS
REFERENCES, POINTERS AND STRUCTS Problem Solving with Computers-I https://ucsb-cs16-sp17.github.io/ Pointer assignment 2 int *p1, *p2, x; p1 = &x; p2 = p1; Q: Which of the following pointer diagrams best
More informationDiscussion 1H Notes (Week 2, 4/8) TA: Brian Choi Section Webpage:
Discussion 1H Notes (Week 2, 4/8) TA: Brian Choi (schoi@cs.ucla.edu) Section Webpage: http://www.cs.ucla.edu/~schoi/cs31 Variables You have to instruct your computer every little thing it needs to do even
More informationCS 61C: Great Ideas in Computer Architecture C Pointers. Instructors: Vladimir Stojanovic & Nicholas Weaver
CS 61C: Great Ideas in Computer Architecture C Pointers Instructors: Vladimir Stojanovic & Nicholas Weaver http://inst.eecs.berkeley.edu/~cs61c/sp16 1 Agenda Pointers Arrays in C 2 Address vs. Value Consider
More informationBITG 1113: POINTER LECTURE 12
BITG 1113: POINTER LECTURE 12 1 LEARNING OUTCOMES At the end of this lecture, you should be able to: 1. Describe the concept of pointer. 2. Write declaration and initialization of a pointer. 3. Do arithmetic
More informationMemory and Pointers written by Cathy Saxton
Memory and Pointers written by Cathy Saxton Basic Memory Layout When a program is running, there are three main chunks of memory that it is using: A program code area where the program itself is loaded.
More informationIntermediate Programming, Spring 2017*
600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general
More informationFORM 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):
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): 1. If a function has default arguments, they can be located anywhere
More informationPOINTERS. Pointer is a memory variable which can store address of an object of specified data type. For example:
POINTERS Pointer is a memory variable which can store address of an object of specified data type For example: #include int x=5; int *a;//here a is a pointer to int which can store address of
More informationCENG 447/547 Embedded and Real-Time Systems. Review of C coding and Soft Eng Concepts
CENG 447/547 Embedded and Real-Time Systems Review of C coding and Soft Eng Concepts Recall (C-style coding syntax) ; - Indicate the end of an expression {} - Used to delineate when a sequence of elements
More informationCSCI-1200 Data Structures Fall 2012 Lecture 5 Pointers, Arrays, Pointer Arithmetic
CSCI-1200 Data Structures Fall 2012 Lecture 5 Pointers, Arrays, Pointer Arithmetic Announcements: Test 1 Information Test 1 will be held Tuesday, September 18th, 2012 from 2-3:50pm in West Hall Auditorium.
More informationCS 103 Lab - Party Like A Char Star
1 Introduction In this lab you will implement a "hangman" game where the user is shown blanks representing letter of a word and then tries to guess and fill in the letters with a limited number of guesses.
More informationC++ Data Types. 1 Simple C++ Data Types 2. 3 Numeric Types Integers (whole numbers) Decimal Numbers... 5
C++ Data Types Contents 1 Simple C++ Data Types 2 2 Quick Note About Representations 3 3 Numeric Types 4 3.1 Integers (whole numbers)............................................ 4 3.2 Decimal Numbers.................................................
More informationRelational Operators and if. Class 10
Relational Operators and if Class 10 Data Type a data type consists of two things: Data Type a data type consists of two things: a set of values Data Type a data type consists of two things: a set of values
More informationProgramming with Arrays Intro to Pointers CS 16: Solving Problems with Computers I Lecture #11
Programming with Arrays Intro to Pointers CS 16: Solving Problems with Computers I Lecture #11 Ziad Matni Dept. of Computer Science, UCSB Thursday, 5/17 in this classroom Starts at 2:00 PM **SHARP** Please
More informationWhat is an algorithm?
Announcements CS 142 C++ Pointers Reminder Program 6 due Sunday, Nov. 9 th by 11:55pm 11/3/2014 2 Pointers and the Address Operator Pointer Variables Each variable in a program is stored at a unique address
More informationFunctions, Pointers, and the Basics of C++ Classes
Functions, Pointers, and the Basics of C++ Classes William E. Skeith III Functions in C++ Vocabulary You should be familiar with all of the following terms already, but if not, you will be after today.
More informationPOINTERS - Pointer is a variable that holds a memory address of another variable of same type. - It supports dynamic allocation routines. - It can improve the efficiency of certain routines. C++ Memory
More informationBasic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable
Basic C++ Overview C++ is a version of the older C programming language. This is a language that is used for a wide variety of applications and which has a mature base of compilers and libraries. C++ is
More informationFORM 2 (Please put your name and form # on the scantron!!!!)
CS 161 Exam 2: FORM 2 (Please put your name and form # on the scantron!!!!) True (A)/False(B) (2 pts each): 1. Recursive algorithms tend to be less efficient than iterative algorithms. 2. A recursive function
More informationChapter 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.
Chapter 9: Pointers 9.1 Getting the Address of a Variable C++ Variables [ not in book ] A Variable has all of the following attributes: 1. name 2. type 3. size 4. value 5. storage class static or automatic
More informationChapter 9: Pointers. Copyright 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.
Chapter 9: Pointers 9.1 Getting the Address of a Variable Getting the Address of a Variable Each variable in program is stored at a unique address Use address operator & to get address of a variable: int
More informationChapter 9: Getting the Address of a Variable. Something Like Pointers: Arrays. Pointer Variables 8/23/2014. Getting the Address of a Variable
Chapter 9: Pointers 9.1 Getting the Address of a Variable Getting the Address of a Variable Each variable in program is stored at a unique address Use address operator & to get address of a variable: int
More informationGetting started with C++ (Part 2)
Getting started with C++ (Part 2) CS427: Elements of Software Engineering Lecture 2.2 11am, 16 Jan 2012 CS427 Getting started with C++ (Part 2) 1/22 Outline 1 Recall from last week... 2 Recall: Output
More informationPointers Pointer Variables. Pointer Variables Getting the Address of a Variable. Each variable in program is stored at a
3.1. Getting the Address of a Variable Pointers (read Chapter 9. Starting Out with C++: From Control Structures through Objects, Tony Gaddis.) Le Thanh Huong School of Information and Communication Technology
More informationMore about BOOLEAN issues
More about BOOLEAN issues Every boolean test is an implicit comparison against zero (0). However, zero is not a simple concept. It represents: the integer zero for all integral types the floating point
More informationUNDEFINED BEHAVIOR IS AWESOME
UNDEFINED BEHAVIOR IS AWESOME Piotr Padlewski piotr.padlewski@gmail.com, @PiotrPadlewski ABOUT MYSELF Currently working in IIIT developing C++ tooling like clang-tidy and studying on University of Warsaw.
More informationPhysics 2660: Fundamentals of Scientific Computing. Lecture 3 Instructor: Prof. Chris Neu
Physics 2660: Fundamentals of Scientific Computing Lecture 3 Instructor: Prof. Chris Neu (chris.neu@virginia.edu) Announcements Weekly readings will be assigned and available through the class wiki home
More informationCSCI-1200 Data Structures Fall 2018 Lecture 5 Pointers, Arrays, & Pointer Arithmetic
CSCI-1200 Data Structures Fall 2018 Lecture 5 Pointers, Arrays, & Pointer Arithmetic Announcements: Test 1 Information Test 1 will be held Thursday, Sept 20th, 2018 from 6-7:50pm Students will be randomly
More informationCS1500 Algorithms and Data Structures for Engineering, FALL Virgil Pavlu, Jose Annunziato,
CS1500 Algorithms and Data Structures for Engineering, FALL 2012 Virgil Pavlu, vip@ccs.neu.edu Jose Annunziato, jannunzi@gmail.com Rohan Garg Morteza Dilgir Huadong Li cs1500hw@gmail.com http://www.ccs.neu.edu/home/vip/teach/cpp_eng/
More informationBinghamton University. CS-211 Fall Pointers
Pointers 1 What is a pointer? Says I m not important what s important is over there Points AT or TO something else 2 Memory Array of bytes Each element has a value 0 1 2 3 xffff fffd xffff fffe xffff ffff
More informationAlgorithms & Data Structures
GATE- 2016-17 Postal Correspondence 1 Algorithms & Data Structures Computer Science & Information Technology (CS) 20 Rank under AIR 100 Postal Correspondence Examination Oriented Theory, Practice Set Key
More informationLecture 04 Introduction to pointers
Lecture 04 Introduction to pointers A pointer is an address in the memory. One of the unique advantages of using C is that it provides direct access to a memory location through its address. A variable
More informationReview of Important Topics in CS1600. Functions Arrays C-strings
Review of Important Topics in CS1600 Functions Arrays C-strings Array Basics Arrays An array is used to process a collection of data of the same type Examples: A list of names A list of temperatures Why
More informationCS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 6: Pointers
CS101: Fundamentals of Computer Programming Dr. Tejada stejada@usc.edu www-bcf.usc.edu/~stejada Week 6: Pointers Pointers Pointers are references to other things Pointers are the address of some other
More informationFinal exam. Final exam will be 12 problems, drop any 2. Cumulative up to and including week 14 (emphasis on weeks 9-14: classes & pointers)
Review 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) 2 hours exam time, so 12 min per problem (midterm 2 had
More informationcalling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!
Chapter 6 - Functions return type void or a valid data type ( int, double, char, etc) name parameter list void or a list of parameters separated by commas body return keyword required if function returns
More informationReview for COSC 120 8/31/2017. Review for COSC 120 Computer Systems. Review for COSC 120 Computer Structure
Computer Systems Computer System Computer Structure C++ Environment Imperative vs. object-oriented programming in C++ Input / Output Primitive data types Software Banking System Compiler Music Player Text
More informationOutput of sample program: Size of a short is 2 Size of a int is 4 Size of a double is 8
Pointers Variables vs. Pointers: A variable in a program is something with a name and a value that can vary. The way the compiler and linker handles this is that it assigns a specific block of memory within
More informationA First Program - Greeting.cpp
C++ Basics A First Program - Greeting.cpp Preprocessor directives Function named main() indicates start of program // Program: Display greetings #include using namespace std; int main() { cout
More informationAnnouncements. Working on requirements this week Work on design, implementation. Types. Lecture 17 CS 169. Outline. Java Types
Announcements Types Working on requirements this week Work on design, implementation Lecture 17 CS 169 Prof. Brewer CS 169 Lecture 16 1 Prof. Brewer CS 169 Lecture 16 2 Outline Type concepts Where do types
More informationHeap Arrays. Steven R. Bagley
Heap Arrays Steven R. Bagley Recap Data is stored in variables Can be accessed by the variable name Or in an array, accessed by name and index a[42] = 35; Variables and arrays have a type int, char, double,
More information1d: tests knowing about bitwise fields and union/struct differences.
Question 1 1a: char ptr[] = Hello World ; char a = ptr[1], b = *(ptr+6); Creates an array of 12 elements, 11 visible letters and a character value 0 at the end. i true ii true iii false iv false v true
More informationEECS 183. Week 3 - Diana Gage. www-personal.umich.edu/ ~drgage
EECS 183 Week 3 - Diana Gage www-personal.umich.edu/ ~drgage Upcoming Deadlines Lab 3 and Assignment 3 due October 2 nd (this Friday) Project 2 will be due October 6 th (a week from Friday) Get started
More informationUEE1302 (1102) F10 Introduction to Computers and Programming (I)
Computational Intelligence on Automation Lab @ NCTU UEE1302 (1102) F10 Introduction to Computers and Programming (I) Programming Lecture 10 Pointers & Dynamic Arrays (I) Learning Objectives Pointers Data
More informationPointers and References
Steven Zeil October 2, 2013 Contents 1 References 2 2 Pointers 8 21 Working with Pointers 8 211 Memory and C++ Programs 11 212 Allocating Data 15 22 Pointers Can Be Dangerous 17 3 The Secret World of Pointers
More informationC Pointers. 6th April 2017 Giulio Picierro
C Pointers 6th April 07 Giulio Picierro Functions Return type Function name Arguments list Function body int sum(int a, int b) { return a + b; } Return statement (return keyword
More informationVariables and numeric types
s s and numeric types Comp Sci 1570 to C++ types Outline s types 1 2 s 3 4 types 5 6 Outline s types 1 2 s 3 4 types 5 6 s types Most programs need to manipulate data: input values, output values, store
More informationPlease refer to the turn-in procedure document on the website for instructions on the turn-in procedure.
1 CSE 131 Winter 2013 Compiler Project #2 -- Code Generation Due Date: Friday, March 15 th 2013 @ 11:59pm Disclaimer This handout is not perfect; corrections may be made. Updates and major clarifications
More informationCSCE 206: Structured Programming in C++
CSCE 206: Structured Programming in C++ 2017 Spring Exam 3 Monday, April 17, 2017 Total - 100 Points B Instructions: Total of 11 pages, including this cover and the last page. Before starting the exam,
More informationCSCE 206: Structured Programming in C++
CSCE 206: Structured Programming in C++ 2017 Spring Exam 3 Monday, April 17, 2017 Total - 100 Points A Instructions: Total of 11 pages, including this cover and the last page. Before starting the exam,
More informationLecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings
Lecture 4: Outline I. Pointers A. Accessing data objects using pointers B. Type casting with pointers C. Difference with Java references D. Pointer pitfalls E. Use case II. Arrays A. Representation in
More informationFundamentals of Programming CS-110. Lecture 2
Fundamentals of Programming CS-110 Lecture 2 Last Lab // Example program #include using namespace std; int main() { cout
More informationC: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21
Department of Computer Science College of Engineering Boise State University September 11, 2017 1/21 Pointers A pointer is a variable that stores the address of another variable. Pointers are similar to
More informationCS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation
CS113: Lecture 3 Topics: Variables Data types Arithmetic and Bitwise Operators Order of Evaluation 1 Variables Names of variables: Composed of letters, digits, and the underscore ( ) character. (NO spaces;
More informationIntro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?
Intro to Programming & C++ Unit 1 Sections 1.1-4 and 2.1-10, 2.12-13, 2.15-17 CS 1428 Spring 2019 Jill Seaman 1.1 Why Program? Computer programmable machine designed to follow instructions Program a set
More informationCS107 Handout 13 Spring 2008 April 18, 2008 Computer Architecture: Take II
CS107 Handout 13 Spring 2008 April 18, 2008 Computer Architecture: Take II Example: Simple variables Handout written by Julie Zelenski and Nick Parlante A variable is a location in memory. When a variable
More informationIntroduction to Scientific Computing and Problem Solving
Introduction to Scientific Computing and Problem Solving Lecture #22 Pointers CS4 - Introduction to Scientific Computing and Problem Solving 2010-22.0 Announcements HW8 due tomorrow at 2:30pm What s left:
More information