CS 352 Week 2 Topics Heap allocation References










![Arrays //Declares/Allocates 6 consecutive integers //init depends upon scope int pos[6]; pos variable is Arrays //Declares/Allocates 6 consecutive integers //init depends upon scope int pos[6]; pos variable is](https://slidetodoc.com/presentation_image_h/3205029bcbcb33d357353adf97c246b0/image-11.jpg)










- Slides: 21

CS 352 -Week 2

Topics • Heap allocation • References • Pointers

Previous knowledge • vi/vim information http: //troll. cs. ua. edu/cs 150/ • Concept of an array allocating the data in a sequence that is all the same type • Functions - http: //troll. cs. ua. edu/cs 150/book/index_9. html – Understanding of passing read only parameters to a function – Understanding of returning a value from a function – Denote type of variable in formal parameter list

Objectives • Dynamically allocate an array of any native type • Understand how/where local variables, global variables and dynamically allocated variables are stored • Multiple ways to refer to an array member and iterate over an array • Understanding of pass by reference vs pass by value • Motivation for using references instead of pointers • Motivation for using references to reduce processing

Function to add values //add. cc #include

Program to swap values //swap 1. cc #define

Call by value Native types are passed to a function using call by value int m=5, n=10; swap(m, n) m 20 0101 special code to save place in calling function and get ready to execute function swap(int i, int j) i j n 21 1010 0101 The If i =we j statement only affects the memoryinthat send the address of the variables memory, holdswe the copy, not the original values can change Values are copied to stack (special memory section to store function parms)

Call by value Native types are passed to a function using call by value int m=5, n=10; swap(&m, &n) m 20 0101 21 1010 Addresses are copied to stack special code to save place in calling function and get ready to execute function swap(int * i, int * j) n i j 20 21 Must let function know that we passed addresses not values

C style program to swap values //swap 2. cc #define

C++ style program to swap values //swap 3. cc #define
![Arrays //Declares/Allocates 6 consecutive integers //init depends upon scope int pos[6]; pos variable is Arrays //Declares/Allocates 6 consecutive integers //init depends upon scope int pos[6]; pos variable is](https://slidetodoc.com/presentation_image_h/3205029bcbcb33d357353adf97c246b0/image-11.jpg)
Arrays //Declares/Allocates 6 consecutive integers //init depends upon scope int pos[6]; pos variable is a pointer 1320 //Declares/Allocates 6 consecutive integers and initializes them with given values int pos={0, 2, 4, 6, 8, 10}; pos[0]=16; cout<

Arrays //arrays 1. cc #include

Array Allocation, Declaration, and Initialization //ADI array pointer and AD(I) array of 10 ints int array 2[10]; //Arrays can only be allocated in global/local //scope is if the size is known int array 1[]; //why not allocate the array? **Sometimes we do not know the size of array we will need

Dynamic allocation • Many array sizes cannot be anticipated at compile time • Unknown array sizes have to be allocated at runtime • Space is allocated from heap (free store) which is separate from the storage for other variables • Allocated space can be used globally *if* there is a global pointer to it • Allocated space with a local pointer will cause the pointer to be deallocated when it is out of scope *but* the space it pointed to still exists, called memory leak • Once allocated, the space stays allocated until it is explicitly freed

Variable ADI Where and how matters You writing a program where you will generate a random number of integers and store them in an array //dynamic 1. cc #include

Object-oriented Programming • Data and functions are linked together • Attributes are the data fields in a class • Methods can be functions that act on the data fields • Methods can be functions that return data state to the caller • Classes are a type definition – No memory is allocated in a class definition • Classes usually follow natural relationships and actions

Smart Car Vehicle Attributes door. Count=2 is. Driver. Door. Closed=T Methods get. Door. Count(){ return door. Count; } is. Driver. Door. Closed() { return is. Driver. Door. Closed; } open. Driver. Door() { driver. Door. Is. Closed=F; } Contain both static and dynamic state Accessorreturn state; read only Mutatorchanges state

Class definition class Vehicle { } public: get. Door. Count(); is. Driver. Door. Closed(); open. Driver. Door(); private: door. Count=2; is. Driver. Door. Closed=T;

Upcoming preparation • • • Class definitions class scope classes vs object declaration, allocation and initialization constructors and destructors I will talk about syntax and semantics, not object oriented analysis and design (determining appropriate classes, methods, attributes and interactions) Resources • Bjarne Stroustrup, Grady Booch • Object-Oriented Analysis and Design with Applications • Design patterns: Elements of Reusable Code

Recap • Using references to allow functions to mutate values • Reference types contain native types and other reference types • Array declaration, allocation and initialization • Array variable holds address of first element • Dynamic array allocation of native type arrays • Pointer arithmetic

• I will not be here (either Dr Hong or a graduate student will be here) • Thursday will be an in class assignment • 3 programs to be due at the end of class • Review the techniques and concepts so that you can complete the assignment in 50 minutes