primitive arrays v. vectors (1)

Similar documents
Arrays and Pointers in C. Alan L. Cox

Course organization. Course introduction ( Week 1)

C-String Library Functions

Memory, Data, & Addressing II CSE 351 Spring

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

a data type is Types

Common Misunderstandings from Exam 1 Material

Hardware: Logical View

CMPE-013/L. Introduction to C Programming

Computer Programming

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Bernhard Boser & Randy Katz

Agenda. Components of a Computer. Computer Memory Type Name Addr Value. Pointer Type. Pointers. CS 61C: Great Ideas in Computer Architecture

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

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Krste Asanović & Randy Katz

Object-Oriented Programming, Iouliia Skliarova

Communication With the Outside World

DYNAMIC ARRAYS; FUNCTIONS & POINTERS; SHALLOW VS DEEP COPY

COMP 2355 Introduction to Systems Programming

CSCB09: Software Tools and Systems Programming. Announcement. The address space. Memory model

BOOLEAN EXPRESSIONS CONTROL FLOW (IF-ELSE) INPUT/OUTPUT. Problem Solving with Computers-I

Strings and Stream I/O

Array Initialization

Memory Corruption 101 From Primitives to Exploit

Question 1. [15 marks]

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

At this time we have all the pieces necessary to allocate memory for an array dynamically. Following our example, we allocate N integers as follows:

2 2

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

CPSC 427: Object-Oriented Programming

Pointers. Pointers. Pointers (cont) CS 217

Chapter 7 Array. Array. C++, How to Program

Memory, Data, & Addressing II

Pointers. Pointer References

Pointers II. Class 31

Understanding Pointers

Function Terminology

CPSC 213. Introduction to Computer Systems. Static Scalars and Arrays. Unit 1b

C++ for Java Programmers

Algorithm Design and Analysis. What is an Algorithm? Do Algorithms Matter?

C programming for beginners

Linked List using a Sentinel

LAB #8. GDB can do four main kinds of things (plus other things in support of these) to help you catch bugs in the act:

Memory, Arrays & Pointers

Overview (1A) Young Won Lim 9/9/17

Homework 5. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

Exam 3 Chapters 7 & 9

Sixth lecture; classes, objects, reference operator.

CS C Primer. Tyler Szepesi. January 16, 2013

Introduction to C++ 2. A Simple C++ Program. A C++ program consists of: a set of data & function definitions, and the main function (or driver)

CPSC 427: Object-Oriented Programming

Lecture 2, September 4

Memory. What is memory? How is memory organized? Storage for variables, data, code etc. Text (Code) Data (Constants) BSS (Global and static variables)

Programming with Arrays Intro to Pointers CS 16: Solving Problems with Computers I Lecture #11

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides

Microcontroller Systems. ELET 3232 Topic 8: Structures, Arrays, & Pointers

C++ Programming Fundamentals

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

LAB #8. Last Survey, I promise!!! Please fill out this really quick survey about paired programming and information about your declared major and CS.

Arrays and Memory Management

Course "Data Processing" Name: Master-1: Nuclear Energy Session /2018 Examen - Part A Page 1

So far, system calls have had easy syntax. Integer, character string, and structure arguments.

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

Chapter 2. Procedural Programming

Lab 8. Follow along with your TA as they demo GDB. Make sure you understand all of the commands, how and when to use them.

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

UNDEFINED BEHAVIOR IS AWESOME

aggregate data types member functions cis15 advanced programming techniques, using c++ lecture # II.1

Pointers and References

Overloading Functions & Command Line Use in C++ CS 16: Solving Problems with Computers I Lecture #6

Array. Prepared By - Rifat Shahriyar

Chapter Overview. Pointers and Dynamic Arrays. Pointers. Pointers. Declaring Pointers. Pointers Tell Where To Find A Variable. 9.

Physics 234: Computational Physics

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 8: Dynamic Memory Allocation

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 14

ECE Fall 20l2, Second Exam

Managing Memory. (and low level Data Structures) Lectures 22, 23. Hartmut Kaiser.

Essential Metrics Project Manager (EPM) Changed, Added and Deleted SLOC

Chapter 9. Pointers and Dynamic Arrays

CS201- Introduction to Programming Current Quizzes

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

CAAM 420 Daily Note. Scriber: Qijia Jiang. Date: Oct.16. Project 3 Due Wed 23.Oct. Two parts: debug code and library exercise.

The output: The address of i is 0xbf85416c. The address of main is 0x80483e4. arrays.c. 1 #include <stdio.h> 3 int main(int argc, char **argv) 4 {

CSCI 104 Memory Allocation. Mark Redekopp David Kempe

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

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

CSE 333. Lecture 10 - references, const, classes. Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington

Agenda / Learning Objectives: 1. Map out a plan to study for mid-term Review the C++ operators up to logical operators. 3. Read about the tips

THE NAME OF THE CONSTRUCTOR AND DESTRUCTOR(HAVING (~) BEFORE ITS NAME) FUNCTION MUST BE SAME AS THE NAME OF THE CLASS IN WHICH THEY ARE DECLARED.

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

CS 211 Winter 2004 Sample Final Exam (Solutions)

Layers of Abstraction CS 3330: C. Compilation Steps. What s in those files? Higher-level language: C. Assembly: X86-64.

Review. Outline. Array Pointer Object-Oriented Programming. Fall 2017 CISC2200 Yanjun Li 1. Fall 2017 CISC2200 Yanjun Li 2

Summer May 18, 2010

CSE 374 Programming Concepts & Tools

Chapter 16. Pointers and Arrays. Address vs. Value. Another Need for Addresses

C++ Lab 04 - File I/O, Arrays and Vectors

UEE1303(1070) S 12 Object-Oriented Programming in C++


Topic 8: Lazy Evaluation

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

Transcription:

Arrays 1

primitive arrays v. vectors (1) 2 int a[10]; allocate new, 10 elements vector<int> v(10); // or: vector<int> v; v.resize(10);

primitive arrays v. vectors (1) 2 int a[10]; allocate new, 10 elements vector<int> v(10); // or: vector<int> v; v.resize(10); access (no bounds checking) int foo = a[3]; a[4] = 17; int foo = v[3]; v[4] = 17;

primitive arrays v. vectors (1) 2 int a[10]; allocate new, 10 elements vector<int> v(10); // or: vector<int> v; v.resize(10); access (no bounds checking) int foo = a[3]; a[4] = 17; int foo = v[3]; v[4] = 17; access (with bounds checking) /* no equivalent */ int foo = v.at(3); v.at(4) = 17;

primitive arrays v. vectors (2) 3 copy int a[10]; int b[10];... // a = b does NOT work for (int i = 0; i < 10; ++i) a[i] = b[i]; vector<int> a; vector<int> b;... a = b;

primitive arrays v. vectors (3) 4 equality int a[10]; int b[10];... // a == b does NOT work // instead: checks if // a, b are same array // (not same values) vector<int> a; vector<int> b;... bool isequal = (a == b); bool isequal = true; for (int i = 0; i < 10; ++i) isequal = (isequal && a[i] == b[j]);

arrays, pointers, and memory (1) int someints[3] = {2, 4, 6}; int *pointer = someints; address value 0x10000 2 someints[0] 0x10004 4 someints[1] 0x10008 6 someints[2]??? memory 0x10000 pointer 5

arrays, pointers, and memory (1) int someints[3] = {2, 4, 6}; int *pointer = someints; address value 0x10000 2 someints[0] 0x10004 4 someints[1] 0x10008 6 someints[2]??? memory 0x10000 pointer 5

6 arrays, pointers, and memory (2) int someints[3] = {2, 4, 6}; cout << "someints is: " << someints << endl; cout << "&someints[0] is: " << &someints[0] << endl; cout << "&someints[1] is: " << &someints[1] << endl; cout << "someints[1] is: " << someints[1] << endl; example output: someints is: 0x7ffda5455b44 &someints[0] is: 0x7ffda5455b44 &someints[1] is: 0x7ffda5455b48 someints[1] is: 4

6 arrays, pointers, and memory (2) int someints[3] = {2, 4, 6}; cout << "someints is: " << someints << endl; cout << "&someints[0] is: " << &someints[0] << endl; cout << "&someints[1] is: " << &someints[1] << endl; cout << "someints[1] is: " << someints[1] << endl; example output: array implicitly converted to pointer to first element someints is: 0x7ffda5455b44 &someints[0] is: 0x7ffda5455b44 &someints[1] is: 0x7ffda5455b48 someints[1] is: 4

arrays, pointers, and memory (2) 6 int someints[3] = {2, 4, 6}; cout << "someints is: " << someints << endl; cout << "&someints[0] is: " << &someints[0] << endl; cout << "&someints[1] is: " << &someints[1] << endl; cout << "someints[1] is: " << someints[1] << endl; arrays elements always at adjacent addresses example output: (4 bytes apart = ints are 4 bytes) someints is: 0x7ffda5455b44 &someints[0] is: 0x7ffda5455b44 &someints[1] is: 0x7ffda5455b48 someints[1] is: 4

arrays, pointers, and memory (2) 6 int someints[3] = {2, 4, 6}; cout << "someints is: " << someints << endl; cout << "&someints[0] is: " << &someints[0] << endl; cout << "&someints[1] is: " << &someints[1] << endl; cout << "someints[1] is: " << someints[1] << endl; general rule: example output: (long) &array[i] == (long) array addr + sizeof(array elem) * i someints is: 0x7ffda5455b44 &someints[0] is: 0x7ffda5455b44 &someints[1] is: 0x7ffda5455b48 someints[1] is: 4

7 arrays as function parameters void somefunc(int ptrtoarray[], int size) { /* code */ } int main() { int someints[3]; somefunc(someints, 3); return 0; } is exactly equivalent to: void somefunc(int *ptrtoarray, int size) { /* code */ } int main() { int someints[3]; somefunc(someints, 3); return 0; }

arrays as function parameters 7 void somefunc(int ptrtoarray[], int size) { /* code */ } int main() { int someints[3]; somefunc(someints, ptrtoarray is always 3); a pointer return 0; example: sizeof(ptrtoarray) == sizeof(int*) } (even though sizeof(someints) == 3 * sizeof(int)) is exactly equivalent to: void somefunc(int *ptrtoarray, int size) { /* code */ } int main() { int someints[3]; somefunc(someints, 3); return 0; }

arrays of arrays 8 AKA multidimensional arrays int m[2][3]; int n[2][3] = { {1,2,3}, {4,5,6} };

arrays of arrays 8 AKA multidimensional arrays int m[2][3]; int n[2][3] = { {1,2,3}, {4,5,6} }; // "row" 1 n[0][0] == 1 n[0][1] == 2 n[0][2] == 3 // "row" 2 n[1][0] == 4 n[1][1] == 5 n[1][2] == 6

array of array storage 9 address value 0x1000 1 n[0][0] 0x1004 2 n[0][1] 0x1008 3 n[0][2] 0x100C 4 n[1][0] 0x1010 5 n[1][1] 0x1014 6 n[1][2] row-major order

array of array storage 9 address value 0x1000 1 n[0][0] 0x1004 2 n[0][1] 0x1008 3 n[0][2] 0x100C 4 n[1][0] 0x1010 5 n[1][1] 0x1014 6 n[1][2] row-major order

array of array storage 9 address value 0x1000 1 n[0][0] 0x1004 2 n[0][1] 0x1008 3 n[0][2] 0x100C 4 n[1][0] 0x1010 5 n[1][1] 0x1014 6 n[1][2] row-major order 0 1 2 0 1

array of array storage 9 address value 0x1000 1 n[0][0] 0x1004 2 n[0] n[0][1] 0x1008 3 n[0][2] 0x100C 4 n[1][0] 0x1010 5 n[1] n[1][1] 0x1014 6 n[1][2] row-major order 0 1 2 0 1

pointers to array 10 (we will not test you on this) int m[2][3] = { {1,2,3}, {4,5,6} } // p1 is a "pointer to array of 3 ints" // yes, this syntax is really confusing // and generally not worth using int (*p1)[3]; p1 = m; // p1 contains address of m[0] cout << p1 << endl; // OUTPUT: 0x... --- address of m[0][0] cout << p1[1] << endl; // OUTPUT: 0x... --- address of m[1][0] cout << p1[1][1] <<endl; // OUTPUT: 5

command line parameters 11 int main (int argc, char* argv[]) {... } // same as: int main (int argc, char **argv) {... } argc number of arguments + 1 argv array of pointers to C-style strings argv[0] program name argv[1], argv[2], arguments what about int main() {... }? okay, but can t get arguments

12 int main(int argc, char **argv) {... // argv == 0x10000 } memory address value address value 0x20005 'u' argv[0][5] 0x10000-70x20000 argv[0] 0x20006 't' argv[0][6] 0x20007 '\0' argv[0][7] 0x10008-F0x2003A argv[1] 0x2003A 'a' argv[1][0] 0x20000 '.' argv[0][0] 0x2003B 'r' argv[1][1] 0x20001 '/' argv[0][1] 0x2003C 'g' argv[1][2] 0x20002 'a' argv[0][2] 0x2003C '1' argv[1][3] 0x20003 '.' argv[0][3] 0x2003D '\0' argv[1][4] 0x20004 'o' argv[0][4]

12 int main(int argc, char **argv) {... // argv == 0x10000 } memory address value address value 0x20005 'u' argv[0][5] 0x10000-70x20000 argv[0] 0x20006 't' argv[0][6] 0x20007 '\0' argv[0][7] 0x10008-F0x2003A argv[1] 0x2003A 'a' argv[1][0] 0x20000 '.' argv[0][0] 0x2003B 'r' argv[1][1] 0x20001 '/' argv[0][1] 0x2003C 'g' argv[1][2] 0x20002 'a' argv[0][2] 0x2003C '1' argv[1][3] 0x20003 '.' argv[0][3] 0x2003D '\0' argv[1][4] 0x20004 'o' argv[0][4]

C strings to strings given a char *c_style_string (like argv[i]) output: cout << c_style_string convert to C++-style string called s: string s(c_style_string) string s; s = c_style_string; 13

command line parameters 14 int main (int argc, char* argv[]) { // The 0th command line parameter is the program name. cout << "This program is called '" << argv[0] << "'" << endl; cout << "The following are the command " << "line parameters you specified: " << endl; // for loop starts at 1 to avoid printing // name of program (again) for ( int i = 1; i < argc; i++ ) { // we can convert the C-style strings into // C++-style strings, and then print them: string s(argv[i]); cout << "\t" << s << endl; } return 0; }

command line parameters 14 int main (int argc, char* argv[]) { // The 0th command line parameter is the program name. cout << "This program is called '" << argv[0] << "'" << endl; cout << "The following are the command " << "line parameters you specified: " << endl; // for loop starts at 1 to avoid printing // name of program (again) for ( int i = 1; i < argc; i++ ) { // we can convert the C-style strings into // C++-style strings, and then print them: string s(argv[i]); cout << "\t" << s << endl; } return 0; }

command line parameters 14 int main (int argc, char* argv[]) { // The 0th command line parameter is the program name. cout << "This program is called '" << argv[0] << "'" << endl; cout << "The following are the command " << "line parameters you specified: " << endl; // for loop starts at 1 to avoid printing // name of program (again) for ( int i = 1; i < argc; i++ ) { // we can convert the C-style strings into // C++-style strings, and then print them: string s(argv[i]); cout << "\t" << s << endl; } return 0; }