Pointers and Terminal Control

Similar documents
Understanding Pointers

Review and Recursion

Pointers II. Class 31

Pointers, Dynamic Data, and Reference Types

Homework #3 CS2255 Fall 2012

C10: Garbage Collection and Constructors

Vector and Free Store (Pointers and Memory Allocation)

Dynamic Data Structures. CSCI 112: Programming in C

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

In Java we have the keyword null, which is the value of an uninitialized reference type

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

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

Declaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples:

Inheritance and Polymorphism

StackVsHeap SPL/2010 SPL/20

Inheritance and Polymorphism

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors

CPSC 427: Object-Oriented Programming

Variables, Memory and Pointers

Vector and Free Store (Vectors and Arrays)

Chapter 17 vector and Free Store

G Programming Languages - Fall 2012

Pointers and Memory 1

Chapter 1: Object-Oriented Programming Using C++

Implementing Abstractions

CS201- Introduction to Programming Current Quizzes

内存管理. Memory management

Object Reference and Memory Allocation. Questions:

Lecture 2, September 4

Documentation. Programming / Documentation Slide 42

C11: Garbage Collection and Constructors

Exam 3 Chapters 7 & 9

Intermediate Programming, Spring 2017*

Pointers and Dynamic Memory Allocation

Chapter 10. Pointers and Dynamic Arrays. Copyright 2016 Pearson, Inc. All rights reserved.

First of all, it is a variable, just like other variables you studied

Object-Oriented Principles and Practice / C++

Pointers. Addresses in Memory. Exam 1 on July 18, :00-11:40am

CS162 - POINTERS. Lecture: Pointers and Dynamic Memory

In this chapter, you will learn about: Pointers. Dynamic Arrays. Introduction Computer Science 1 CS 23021


Dynamic Allocation of Memory

CS 161 Exam II Winter 2018 FORM 1

CPSC 427: Object-Oriented Programming

Lab 3. Pointers Programming Lab (Using C) XU Silei

Polymorphism. Zimmer CSCI 330

Chapter 17 vector and Free Store

Discussion 1E. Jie(Jay) Wang Week 10 Dec.2

Chapter 10 Pointers and Dynamic Arrays. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo


Chapter 9. Pointers and Dynamic Arrays

The issues. Programming in C++ Common storage modes. Static storage in C++ Session 8 Memory Management


Midterm Review. PIC 10B Spring 2018

Pointers. Variable Declaration. Chapter 10

Intermediate Programming & Design (C++) Classes in C++

377 Student Guide to C++

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

vector and Free Store

CS201 Some Important Definitions

C++ Primer for CS175

Chapter 17 vector and Free Store. Bjarne Stroustrup

CSC 211 Intermediate Programming. Arrays & Pointers

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

C++ Basic Syntax. Constructors and destructors. Wojciech Frohmberg / OOP Laboratory. Poznan University of Technology

Dynamic Allocation of Memory

Short Notes of CS201

Pointers. Developed By Ms. K.M.Sanghavi

CA341 - Comparative Programming Languages

Pointers, Arrays and C-Strings

Inheritance, Polymorphism and the Object Memory Model

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

Linked Memory. Pointers Linked Lists. January 19, 2018 Cinda Heeren / Geoffrey Tien 1

CS201 - Introduction to Programming Glossary By

Lecture 7: Binding Time and Storage

Memory management COSC346

Pointers. Memory. void foo() { }//return

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

Objects Managing a Resource

Memory and C++ Pointers

11. a b c d e. 12. a b c d e. 13. a b c d e. 14. a b c d e. 15. a b c d e

CSCI-1200 Data Structures Spring 2016 Lecture 6 Pointers & Dynamic Memory

Dynamic Memory Allocation

04-17 Discussion Notes

We have written lots of code so far It has all been inside of the main() method What about a big program? The main() method is going to get really

Intermediate Programming, Spring 2017*

C++ PROGRAMMING LANGUAGE: DYNAMIC MEMORY ALLOCATION AND EXCEPTION IN C++. CAAM 519, CHAPTER 15

Pointers! Arizona State University 1

cout << "How many numbers would you like to type? "; cin >> memsize; p = new int[memsize];

Arrays. Comp Sci 1570 Introduction to C++ Array basics. arrays. Arrays as parameters to functions. Sorting arrays. Random stuff

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

CSE 303: Concepts and Tools for Software Development

Week 9 Part 1. Kyle Dewey. Tuesday, August 28, 12

C Programming Basics II

COMP6771 Advanced C++ Programming

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

CSC1322 Object-Oriented Programming Concepts

Fast Introduction to Object Oriented Programming and C++

Topics so far. Review. scanf/fscanf. How data is read 1/20/2011. All code handin sare at /afs/andrew/course/15/123/handin

What the CPU Sees Basic Flow Control Conditional Flow Control Structured Flow Control Functions and Scope. C Flow Control.

Transcription:

Division of Mathematics and Computer Science Maryville College

Outline 1 2 3

Outline 1 2 3

A Primer on Computer Memory Memory is a large list. Typically, each BYTE of memory has an address. Memory can be read or written. In most computers, program code and data are in the same memory.

The sizeof operator Syntax sizeof(type) sizeof expression Each data type has a different size. sizeof returns the size of the type or expression which follows it. Example: sizeof(int) returns 4 on our system. sizeof(double) returns 8 on our system.

How Variables are Stored in Memory Variables reside in memory. The compiler remembers the beginning and size of each variable. Generated machine code refers to variables by their memory address. A variable is fully specified as: A Memory Address Size Data

Pointers A Pointer is a variable. The value stored in a pointer is an address. Pointers provide raw access to memory. There are also some OOP considerations for pointers.

Pointer Syntax Declaration type *name int *ptr; Declares a pointer to a variable of given type. Address Of &variable &x Returns the address of the variable. Dereference *name *ptr Gets to the value at the address stored in the pointer.

Pointer Example int x; //integer declaration int *ptr; //integer pointer declaration ptr = &x; //the "address of" operator *ptr = 5; //dereferencing cout << ptr << endl; //prints the address cout << *ptr << endl; //prints the value

The new and delete Operators Syntax new type delete pointer new allocates a new instance of the specified type. new returns a pointer to the first byte of the newly allocated memory. delete deallocates the memory starting at the pointer. delete only works on pointers returned by the new operator.

Life of an Integer int *ptr; //create an integer ptr = new int; //do stuff with it *ptr = 5; cout << *ptr << endl; //destroy it delete ptr;

Memory Leaks A very common bug! Memory leaks are when we have no reference to allocated memory. Allocated memory is effectively lost and becomes unusable. Wastes resources. In a loop, it can bring a system to its knees! Every new should have a corresponding delete int *ptr; ptr = new int; ptr = nullptr; //we lost an int!

Memory Organization Programs are usually divide into segments. A typical arrangement is based on the Unix Memory Model. Program Text Writable Data Block Program Stack

Dynamic Memory Static Variables Variables declared normally. Dynamic Variables Variables created with new. A typical C++ arrangement: Static variables live on the stack. Dynamic variables live on the heap. Static variables exist only while functions are running. Dynamic variables exist until deallocated.

Arrays Arrays are blocks of contiguous memory. They are a primitive list. Declared with type and size: int ar[10]; They are indexed, like vectors: ar[0] thru ar[9] Array bounds are immutable. Arrays are not objects! Typically, in C++, we use vectors. Arrays just aren t as powerful!

Arrays and Pointers Like a pointer, an array name refers to a memory address. An array name is the address of the first item in the list. An array name can be assigned to a pointer! int *ptr = ar; You can even index a pointer like an array (assuming it contains the address of the beginning of an array) ptr[1] Unlike pointers, array names are immutable. Arrays cannot be returned from functions while pointers can. More on the magic of pointers and arrays will be covered in your operating system class!

Pointers and Objects An object can be referenced by a pointer. Point *p; You can create objects using new: p = new Point; //invokes no-arg constructor p = new Point(); //also no-arg p = new Point(5,2); //arguments! Members can be referenced using the -> operator. p->setx(42.0); Using delete on an object invokes its destructor. delete p;

Advantages of Object Pointers Pro Tip It is a very good idea to use pointers or references when dealing with objects! Statically defined objects can be trouble! Explicit control of instantiation and destruction. Delay instantiation until some values have been computed. Pointers allow you to use nullptr to indicate an object which has not yet been created. In C++, polymorphism only works with pointers and references!

References Variables Reference variables are declared with an &. Point p; Point &pref=p; References bind the name to a memory location. References are immutable, and must be assigned at instantiation. A Useful Idiom/Standard Use pointers for creation and storage of objects. Use references for parameters and non-dynamic member variables.

Outline 1 2 3

A Brief Introduction to TTY Interfaces Physical Hardware Terminals Linked to Machine via Serial Cables Video Terminals Type Writer (Teletype) Terminals

Round Trip to Your Terminal

Codes Control uses Escape Sequences These begin with the escape character (octal 033) Sequences control color, cursor placement, etc. A terminal emulator responds to these sequences of characters. Image Source: wikipedia.org

termmanip.h A small library written to play with terminals. It remembers the ANSI sequences so you don t have to! Works kind of like iomanip. For instance example: cout «bold «red «"Hello, World" «endl; Allows most common text changes and cursor movements.

Outline 1 2 3

Cellular Automata A simulation based on how cells reproduce. First CA s were proposed by John Von Neumann in the 1940 s Many CA s are capable of Universal Computation! Useful in simulation problems, especially for optimization. Active area of research.

The Rules of Proposed by John Conway in 1970 Played on a 2D grid. Each cell is either alive or dead. 4 Simple Rules: 1 Starvation A living cell with fewer than 2 living neighbors dies. 2 Stasis A living cell with 2-3 living neighbors lives. 3 Overpopulation A living cell with more than 3 living neighbors dies. 4 Reproduction A dead cell with exactly two living neighbors comes to life. Capable of Universal Computation!

Some General Tips Decompose the problem into classes. Remember your parts of speech! Each cell should basically just be aware of whether it is alive or dead as well as whether it will be alive or dead in the next generation. A 2d grid can be represented as a vector of vectors: vector<vector<cell> > grid(24, vector<cell>(80)); grid[0][0]; //the upper left cell grid[23][79]; //the lower right cell The grid will need some way to count living neighbors.