Lecture 01a: C++ review Topics: Setting up projects, main program Memory Diagrams Variables / Types (some of) the many-types-of-const's Input / Output.

Slides:



Advertisements
Similar presentations
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Advertisements

Kernighan/Ritchie: Kelley/Pohl:
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
CMSC 2021 C++ I/O and Other Topics. CMSC 2022 Using C++ Stream I/O Default input stream is called cin Default output stream is called cout Use the extraction.
Informática II Prof. Dr. Gustavo Patiño MJ
Engineering Problem Solving With C++ An Object Based Approach Chapter 9 Pointers and Creating Data Structures.
Pointers and dynamic objects COMP171 Fall Pointers and dynamic objects/ Slide 2 Topics * Pointers n Memory addresses n Declaration n Dereferencing.
Lecture 1 The Basics (Review of Familiar Topics).
Pointers. Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location.
1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays.
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.
Computer Science 1620 C++ - Basics. #include using namespace std; int main() { return 0; } A very basic C++ Program. When writing your first programs,
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Basic Elements of C++ Chapter 2.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
Input & Output: Console
CS352-Week 2. Topics Heap allocation References Pointers.
Chapter 6 Pointers C Programming © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
February 11, 2005 More Pointers Dynamic Memory Allocation.
C++ Tutorial Hany Samuel and Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Copyright © 2006 by Douglas.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
C++ Streams Lecture-2.
CIS-165 C++ Programming I CIS-165 C++ Programming I Bergen Community College Prof. Faisal Aljamal.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 4 Pointers and Dynamic Arrays Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Dynamic memory allocation and Pointers Lecture 4.
Copyright © Curt Hill Structured Data What this course is about.
Looping and Counting Lecture 3 Hartmut Kaiser
Lecture 3: Getting Started & Input / Output (I/O) “TRON” Copyright 1982 (Walt Disney Productions)
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Introduction to Programming
Fundamentals of C++ Yingcai Xiao 09/03/08. Outline Class Definition IO Template vector C Pointer Dynamic Memory Allocation.
Week 2. Functions: int max3(int num1, int num2, int num3) {int result; result = max(max(num1,num2),num3); return result; } //max3.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 3 – August 28, 2001.
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
Review 1 List Data Structure List operations List Implementation Array Linked List.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++
C++ Programming Lecture 3 C++ Basics – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
Lecture 01c: C++ review Topics: static arrays array / pointer connection dynamic arrays (and pointers) 2D arrays.
Variables and memory addresses
Pointers & References. Pointers Pointer arithmetic Pointers and arrays Pointer-related typedef’s Pointers and const References.
Gramming An Introduction to C Programming (assuming that you already know Java; this is not an introduction to C++)
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Slide 1 of 36. Attributes Associated with Every Variable Data type Data type Actual value stored in variable Actual value stored in variable Address of.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Lecture 01d: C++ review Topics: functions scope / lifetime preprocessor directives header files C structures ("simple classes")
14-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
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.
Lecture 3: Getting Started & Input / Output (I/O)
C++ Lesson 1.
Pointers What is the data type of pointer variables?
Intro to ETEC Java.
Pointers and Memory Overview
Pointers and Pointer-Based Strings
Lecture 6 C++ Programming
Andy Wang Object Oriented Programming in C++ COP 3330
Variables with Memory Diagram
Pointers, Dynamic Data, and Reference Types
Pointers and Pointer-Based Strings
Pointers, Dynamic Data, and Reference Types
Presentation transcript:

Lecture 01a: C++ review Topics: Setting up projects, main program Memory Diagrams Variables / Types (some of) the many-types-of-const's Input / Output (console and file) Operators / Expressions Basic pointers

Preface Recall: C++ is made of – ANSI-C ("older") – Preprocessor directives (#xxx) – C++ extensions (classes, STL, …) – Templates In this class, I'm going to mainly use – C++ (e.g. cout instead of printf) – I won't use an C++11 (C++0X) Most compilers don't fully support it…yet – C++ is intentionally multi-paradigm I'm assuming you understood ~90% of the material in ETEC1101 – This should all be review…so we'll move quickly – If not, it's your responsibility to get up to speed…fast!

Part I: Setting-Up [Set up a project in VC++ and Netbeans and Code::Blocks] Skeleton code Note: this is what a console application looks like. – Other types of applications (e.g..DLL,.LIB, Windows- EXE) will look different #include using namespace std; int main() { cout << "Hello, World!" << endl; return 0; }

Part II: Memory Diagrams A tool I use frequently to model current state of RAM Mimics most executable formats, but isn't modeled after a specific architecture / OS Four main areas: – Stack: Contains static variables, saved registers, call stack – Heap: Contains dynamically allocated memory chunks – Globals: Contains global and static variables. – Machine Code [not usually shown]

Overview, cont. In reality, the physical addresses of variables are determined by: – the architecture (x86, ARM, …) – the OS (OSX, Windows, …) the other processes currently running – the compiler (MinGW, VC++, …) …I'll just use relative addresses – The CPU and OS together resolve this to a physical address at run-time.

General layout 0 …HEAP ? ?+1 …STACK 10,000 10,001 …GLOBALS 19,999 20,000 CODE The HEAP and STACK share the same memory. Items are added to the stack in reverse order.

Part III: C++ variables / types C++ variables can be made from these types: – integer types (char, short, int, long int) – floating-point types (float, double) – booleans (hold a true or false – really just an integer) – pointers (to ___; an address) – [homogeneous] arrays – classes user-defined (e.g. Spaceship) STL-defined (standard template library) (e.g. vector, string, etc) From a library (e.g. SDL_Surface)

Variable Declaration C/C++ uses explicit declaration int x = 6;// declaration (and init) cout << x;// use Compare that to Python/MathPiper/etc which use implicit declaration x = 6# implicit declaration print(x)# use

Variables and memory When you include a declaration – compiler generates code which will allocate memory at the appropriate time – The architecture / OS target determines the amount allocated e.g. on a 32-bit system, int's might be 4 bytes, but on a 64-bit system they could be 8. sizeof function / operator

Part IV: const One of the most confusing syntaxes in C/C++ Performs exactly like the non-const version… …but the compiler won't allow changes. Good for: – list sizes – things like pi –…–… int x = 5; const int y = 5; x = 6; y = 6;// compile error

Part V: Input / Output Just focusing on cin / cout here (although you can still use printf / scanf) Both are considered C++ streams – as are fstream's (for files), and stringstream's (for building up strings) – Input streams (cin and file-reading) use the >> operator – Output streams (cout, file-writing, and stringstreams) use the << operator – The data passed can be anything which has overloaded the > operators (most built-in types have this)

Example: cin/cout #include using namespace std; int main() { int age; string name; cout << "Enter your name: "; cin >> name;// no spaces allowed! cout << "Enter your age: "; cin >> age; cout << "\"" << name << "\", in 5 years, you'll be"; cout << age + 5 << endl; }

Example: fstream #include using namespace std; int main() { fstream fp; float avg, temp; // Seed the random generator srand(time(NULL)); // Create (or replace) the contents of test.txt fp.open("test.txt", ios::out); if (fp.is_open()) { fp << rand() % 100 << endl; fp.close(); }

Example: fstream // Read data in from the file and compute the average fp.open("test.txt", ios::in); if (fp.is_open()) { fp >> avg; fp >> temp; avg += temp; fp.close(); cout << "The average is " << avg / 3.0 << endl; } }

Part VI: Operators, Expressions No surprises here (high precedence => low): Reference: pexpressionsr_7.htm pexpressionsr_7.htm LevelOperator 1() grouping post-fix 3! ~ 3+ - unary 3& address-of 3* de-reference 4* / % > bit-shift 7 = == != 8& ^ | bit-wise 9&& || 10= *= += /= %= ~= &= |= >= 11?: ternary conditional 12,

Part VII: Basic Pointers Pointers themselves are just integers – Regardless of what they point to! – They hold an address – The type of the pointer is important When working with arrays (later) When working with pointers to objects (later) When de-referencing a pointer

Simple example #include using namesapce std; int main() { char * cptr; char c = 'a'; // 97 double * dptr = NULL; double d = 3.17, e = 9.3; int x; x = sizeof(c); x = sizeof(cptr); x = sizeof(dptr); cptr = &c; *cptr = 'b'; dptr = &d; *d = 4.2; dptr = &e; *dptr = *dptr * 2.0; } [Do the memory diagram on the board]

const and pointers From: /cpp/cpp_mfc/general/article.p hp/c6967/Constant-Pointers- and-Pointers-to-Constants.htm /cpp/cpp_mfc/general/article.p hp/c6967/Constant-Pointers- and-Pointers-to-Constants.htm char a = 'A', b = 'B'; char * ptr1 = &a;// Non-const const char * ptr2 = &a;// Can't change char pointed to char * const ptr3 = &a;// Can't change address of ptr const char * const ptr4 = &a;// Can't change either *ptrX = 'c';ptrX = &b ptr1 ptr2  ptr3  ptr4 