CS 352 Week 2 Topics Heap allocation References

  • Slides: 21
Download presentation
CS 352 -Week 2

CS 352 -Week 2

Topics • Heap allocation • References • Pointers

Topics • Heap allocation • References • Pointers

Previous knowledge • vi/vim information http: //troll. cs. ua. edu/cs 150/ • Concept of

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

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 <iostream> using namespace std; int add(int i,

Function to add values //add. cc #include using namespace std; int add(int i, int j){ return i+j; } What is the output generated by this code? static_cast is used to convert x and y to integers via truncation int main() { int m = 5, n =10; cout<<"inputs: “<< m <<“ “<

Program to swap values //swap 1. cc #define <iostream> using namespace std; int main()

Program to swap values //swap 1. cc #define using namespace std; int main() { int m = 5, n =10; cout<<"inputs: “<< m <<“ “<

Call by value Native types are passed to a function using call by value

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

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 <iostream> using namespace std;

C style program to swap values //swap 2. cc #define using namespace std; int main() { int m = 5, n =10; This function is a mutator; it changes the original value cout<<"inputs: “<< m <<“ “<

C++ style program to swap values //swap 3. cc #define <iostream> using namespace std;

C++ style program to swap values //swap 3. cc #define using namespace std; int main() { int m = 5, n =10; This function is a mutator; it changes the original value The & lets the compiler know to use the addresses rather than the values cout<<"inputs: “<< m <<“ “<

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 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 <iostream> using namespace std; int main() { int. Array

Arrays //arrays 1. cc #include using namespace std; int main() { int. Array 1[6]; int. Array 2[]={2, 4, 6, 8, 10}; OUTPUT Print uninitialized array pointer to Array 1 0 xffbff 158 pos 0 int. Array 1 is -4198016 pos 1 int. Array 1 is 67896 pos 2 int. Array 1 is -12775520 pos 3 int. Array 1 is 0 pos 4 int. Array 1 is 0 pos 5 int. Array 1 is 0 pointer to Array 2 0 xffbff 140 cout<<"pointer to Array 1 "<

Array Allocation, Declaration, and Initialization //ADI array pointer and AD(I) array of 10 ints

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

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

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 #include #include using namespace std; //reference-ADI; array-nothing int * integer. List; //don’t know the random number of integers; can’t use [] count i int L 2[4]; //reference ADI; array-ADI in data int main() { srand(time(NULL)); int count=rand()%10; int * integer. List = new int[count]; //dynamic allocation of space-”operator new” } for (int i=0; i

Object-oriented Programming • Data and functions are linked together • Attributes are the data

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(){

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();

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

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

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

• 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