CS 228 meeting -*- Outline -*- * introduction to pointers and dynamic data (HR 7) ** preview compare HR 7.5 ----------------------------- POINTERS AND DYNAMIC DATA (HR 7) Problem: Amount of input data varies between uses Some solutions: - Allocate the maximum amount - Ask user how much to allocate - Allocate space at run-time (dynamically) ----------------------------- first alternative is inefficient, imposes a limit in practice second makes user do something computers are better at having 2 passes (count and save data, then allocate) is inefficient - anyway, also requires dynamic allocation Q: what is done in a typical Scheme program? allocated dynamically, using cons but to do the second and esp. the third need to solve: ----------------------------- Problem: - how the name dynamically allocated storage? ----------------------------- Q: how is that done in Scheme? using variables that name lists (cons cells) in C++ use pointers ** memory model each cell has an address (name) and contents that the program can interpret according to its type --------------------------- MODEL OF MEMORY hardware C++ Memory Program Address Cell Identifier Type 3000 [ 12000 ] intPtr int * 3004 [ 12004 ] lintPtr long int * ... 12000 [ ] int 12004 [ ] long int ABSTRACT PICTURE // pointerIntro.C #include int main() { int * intPtr; long int * lintPtr; intPtr = new int; *intPtr = 20; lintPtr = new long int; *lintPtr = 30; cout << *intPtr + *lintPtr << endl; } --------------------------- fill in the above Q: Does the star (*) above mean multiplication? --------------------------- SUMMARY C++ presents the memory of a computer as cells (or objects), each of which has: - a - a - a The may change during execution. ---------------------------- (unless it is declared to be a const) ... - a unique address - a type - a value (or contents) The value can change Q: What ADT is memory like? it's random access, like a record, but with numbers vs. names cells may overlap But it's very helpful to think of memory as a big array (or several, one for each type of value) Q: If you think of memory as a big array, what is a pointer like? the array index ----------------------------------- def: a *pointer* is an address with a type The type tells ------------------------------------- ... how to interpret the contents of the cell at that address, and also how to do pointer arithmetic -------------------------------------- def: a *variable* is ------------------------------------- ... a named cell whose value can be changed by assignment (a box) Q: What's the difference between a variable and a constant? Q: What's the difference between a constant and a value? Q: What is a pointer? a value? a constant? or a variable? so best to distinguish cells (objects) from variables might want to always say "pointer value" or and "pointer variable" Q: What are the operations on variables? assign, fetch value Q: What is an integer variable? ------------------------------------- def: a *pointer variable* is a variable that Its type is called a ------------------------------------ ... holds a pointer. ... pointer type. Give a picture of a pointer value and a pointer variable