Class Note for EECS 168 with Professor Haverkamp at KU
Class Note for EECS 168 with Professor Haverkamp at KU
Popular in Course
Popular in Department
This 13 page Class Notes was uploaded by an elite notetaker on Friday February 6, 2015. The Class Notes belongs to a course at Kansas taught by a professor in Fall. Since its upload, it has received 19 views.
Reviews for Class Note for EECS 168 with Professor Haverkamp at KU
Report this Material
What is Karma?
Karma is the currency of StudySoup.
You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!
Date Created: 02/06/15
Pointers and Dynamic Structures 0 Overview of Memory Model 0 Pointer Variables 0 Using Dynamic Variables and Arrays 0 Using Dynamic Structures and Arrays of Structures 0 Reference Parameters Revisited 0 Class Constructors 0 Copy Constructors 0 Class Destructors Overview of Memory Model o Computer memory can be viewed as an quotarrayquot of storage locations o On a PC this array may be limited to size of physical memory o On a workstation quotvirtualquot memory size is almost unlimited o Memory at run time typically contains the following 08 08 Program Program The Free The Code Data Code Data Stack Memory Heap 1 operating system code o The 08 code is loaded into memory when machine quotbootsquot o This code is executed when 0 S functions are called 2 operating system data o Provides space for all global variables of the O S o On some systems this data may be on the stack 3 program code o The program code is loaded when the program is quotinvokedquot o The code is executed when program functions are called 4 program data o Provides space for all global variables of the program o On some systems this data may be on the stack 5 the stack o Provides space for all parameters and local variables in functions o Space is dynamically allocated when a function is called o Space is dynamically released when a function returns o The stack also contains the return address for function calls o The stack is essential for supporting recursive functions 6 the heap Provides space for all dynamically allocated variables Space is specifically requested by programmer Space is specifically released by programmer The heap is essential for supporting advanced data structures such as 0 variable length arrays linked lists trees and graphs 7 free memory o Free memory is located between the stack and the heap o Free memory gives the stack and the heap room to grow 0 quotstack over owquot occurs when too much stack space is requested 0 quotallocation errorquot occurs when too much heap space is requested Pointer Variables o All program variables start at some memory location in the computer o Pointer variables contain the starting addresses of other variables o Can access variables quotindirectlyquot using pointers o Need to be very careful about how and when we use pointers Declarations oat value 42 oat gt temp NULL Pointer initialization temp ampva1ue Direct variable access value 77 cout ltlt value Indirect variable access gt temp 94 cout ltlt gt temp Using Dynamic Variables and Arrays o Pointers can also contain addresses of space requested from the heap o Must specify amount of space needed number amp types of variables o Different data types are different sizes char1 int4 oat4 etc o This mechanism gives us a way to create variable sized arrays o The new command creates space the delete command releases space o Use gt data or datai to access dynamic variables or arrays o Use delete or delete to return memory to the heap Allocate a dynamic variable int gt value NULL value new int Using a dynamic variable gt value 13 delete value int gt john new int cout ltlt gt john Allocate a dynamic array int size cout ltlt quotEnter size of arrayquot cin gtgt size oat gt data new oatsize Use the dynamic array for int i0 i lt size i datai i 314159 size delete data Using Dynamic Structures and Arrays of Structures o Can also request space for one or more structures on the heap o This is used for creating linked lists trees and graphs o Must de ne structure and use new and delete as before o Need to use datagtPart or dataPart to refer to subparts Allocate a dynamic structure struct student char Name20 int Year oat GPA student gt temp new student student junk junkYear 2006 Using a dynamic structure strcpytempgtName quotTom J onesquot tempgtYear 2006 tempgtGPA 3 0 Allocate a dynamic array of structures int size cout ltlt quotEnter size of arrayquot cin gtgt size student gt data new studentsize Initialize dynamic array of structures for int i0 i lt size i strcpydataiName quotquot dataiYear 2006 dataiGPA 00 cin gtgt size Point gt p new Pointsize for int i0 iltsize i pisetX rand lOO pisetY rand 100 Use dynamic array of structures student gt ptr NULL for int i0 i lt size i if strcmpdataiName quotJohn Smithquot 0 ptr datai if ptr NULL ptrgtGPA 35 Release memory delete temp delete data Reference Parameters Revisited o When we pass a variable by reference we really pass its address o When we access a reference parameters are really using indirection o In C this method of parameter passing was explicit o In C reference parameters are simpler to de ne and use C function with reference parameters void fooint ampnum num num 42 Corresponding C function call int val 1101 foo val C function with reference parameters Expects the address of an integer as an argument void barint gtl num gtl num num l7 Corresponding C function call Passes the address of an integer to function int val 1304 bar ampVal Class Constructors o The constructor function is called when a class variable is declared o This function has the same name as the class itself o Constructor initializes private data members of the class o Dynamic size objects can be created using the new command Array class de nition class Array public Arrayint size constructor Arrayconst amp Array copy constructor Array destructor void ReadArraychar f11ename void ProcessArrayint param void WriteArraychar f11ename private int Length int gt Data Constructor function Array Arrayconst int size Length size Data new intLength Write array data to le void Array WriteArraychar lename ofstream doutf11ename for int i0 iltLength i dout ltlt Datai ltlt quot quot dout ltlt endl doutclose Main program int main 1nt Slze cin gtgt size Array asize aReadArrayquotin letxtquot aProcessArray42 aWriteArrayquotout letxtquot magic call to a destructor Copy Constructors 0 The copy constructor is used to create copies of objects o It is also used when objects are passed by value into functions 0 Important to de ne when dynamic memory is allocated in constructor 0 Without a copy constructor dynamic objects should be passed by reference or you will have a shallow copy deep destroy bug Copy constructor function Array Arrayconst Array amp a Allocate space and make copy Length aLength Data new intLength for inti O i lt Length i Datai aDatai Usage example Array alOO aReadArrayquotinfiletxtquot Array ba Class Destructors o The destructor function is automatically called when the object is no longer in scope o This function has the same name as the class with a added in front o Destructor cleans up anything the class initialized or allocated o Must make sure to free space using destroy in a destructor function o Failure to clean up after new will eventually result in heap over ow Destructor function Array Array Length O delete Data
Are you sure you want to buy this material for
You're already Subscribed!
Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'