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.