COMP 11 Class 17 Outline Topics: Dynamic Arrays and Memory 2 Approach: Main Ideas: Discussion, Explanation, Discussion Memory allocation and pointers 1. Admin a) Sentegy Study Announcment b) Project 2A hand out c) Midterm exam grading issues d) Reading assignment, chapters 9.1 and 9.2 2. Recap on previous lecture and the lab a) Dynamic arrays i. Addressing the problems of (a) exact memory size, (b) persistent memory, (c) changing memory requirement, (d) allocating from the heap instead of the stack b) The concept of hermit crab i. Find a larger shell ii. Move all the stuff iii. Delete the old shell c) Syntax: new and delete i. Example using new and delete (new.cpp) ii. See comparison chart 3. Today s problem: dynamically allocated memory in general a) Remember: For every new, there must be a delete!!! b) A visual explanation of dynamic array (new.cpp) c) The concept of memory leaking i. Using valgrind to see what happens without the delete (new.cpp) d) Some advantages of using dynamic array: i. Allocate just enough memory to fit your need (efficiency) ii. Returning memory from function (new2.cpp) 1. Pointer diagram iii. Arrays that can change size! (new3.cpp) 1. Introducing NULL (memory address of: 0x00) 2. Passing pointers to change values 3. Returning new memory that is allocated from a function 4. Who is responsible for deleting? 5. Pointer diagram iv. Discussion: efficiency of new and delete is terrible 1. How can we make new3.cpp better? (new3-b.cpp) 2. Pointer diagram 4. New/delete with basic variable types and structs (new4.cpp) a) Difference between delete and delete [] 5. Exercise: Allocating a two-dimensional array a) Deleting a two dimensional array b) Pointer diagram c) How about a three-dimensional array? 6. Stack vs. Heap
:::::::::::::: new.cpp ::::::::::::: int size = 5; int* tarray = new int [size]; int sarray[size]; tarray[i] = i; sarray[i] = i; cout << "tarray[" << i << "]: " << tarray[i] << endl; cout << "sarray[" << i << "]: " << sarray[i] << endl; cout << "size of the dynamic array: " << sizeof(tarray) << endl; cout << "size of the static array: " << sizeof(sarray) << endl; cout << "size of an integer: " << sizeof(int) << endl; cout << "size of an integer pointer: " << sizeof(int*) << endl; delete [] tarray; //delete [] sarray; :::::::::::::: new2.cpp ::::::::::::: int* allocatesomememory(int); int size = 5; int* returnedarray = allocatesomememory(size); cout << "array[" << i << "]: " << returnedarray[i] << endl; delete [] returnedarray; int* allocatesomememory(int size) { int* array = new int [size]; array[i] = i; return array;
:::::::::::::: new3.cpp ::::::::::::: void printarray (int*, int); int* growarraybyone (int*, int); int input; int* array = NULL; int arraysize = 0; char print = 'n'; //initializing the pointer to point to NULL while (1) { cout << "give me a positive integer, type -1 to exit: "; cin >> input; if (input < 0) { break; else { array = growarraybyone (array, arraysize); arraysize++; array[arraysize-1] = input; cout << "print the array? "; cin >> print; if (print == 'y') { printarray(array, arraysize); if (array!= NULL) { delete [] array; void printarray (int* array, int size) { cout << "array[" << i << "]: " << array[i] << endl; int* growarraybyone (int* currentarray, int size) { int* temparray = new int [size+1]; temparray[i] = currentarray[i]; if (currentarray!= NULL) { delete [] currentarray;
return temparray; :::::::::::::: new4.cpp ::::::::::::: struct Course { int coursenum; int numstudents; ; int* myint = new int; float* myfloat = new float; Course* mycourse = new Course; cout << "type in an int: "; cin >> *myint; cout << "type in a float: "; cin >> *myfloat; cout << "type in course num: "; cin >> (*mycourse).coursenum; cout << "type in num students: "; cin >> mycourse->numstudents; cout << "int: " << *myint << endl; cout << "float: " << *myfloat << endl; cout << "course, coursenum: " << mycourse->coursenum << endl; cout << "course, numstudents: " << (*mycourse).numstudents << endl; delete myint; delete myfloat; delete mycourse; :::::::::::::: new3-b.cpp ::::::::::::: void printarray (int*, int); int* growarrayifnecessary (int*, int*, int); int input; int* array = new int [1]; int arraysize = 1; int curindex = 0; char print = 'n'; while (1) { cout << "give me a positive integer, type -1 to exit: "; cin >> input;
if (input < 0) { break; else { array = growarrayifnecessary (array, &arraysize, curindex); array[curindex] = input; curindex++; cout << "print the array? "; cin >> print; if (print == 'y') { printarray(array, curindex); if (array!= NULL) { delete [] array; void printarray (int* array, int index) { for (i=0; i<index; i++) { cout << "array[" << i << "]: " << array[i] << endl; int* growarrayifnecessary (int* currentarray, int* size, int curindex) { int* temparray = currentarray; if (curindex == *size) { int oldsize = *size; int newsize = *size * 2; cout << "growing array from " << oldsize << " to " << newsize << endl; temparray = new int [newsize]; for (i=0; i<oldsize; i++) { temparray[i] = currentarray[i]; *size = newsize; if (currentarray!= NULL) { delete [] currentarray; return temparray;
POINTER SUMMARY address pointer variable & WHAT ISAPOINTER? TERMS AND OPERATORS Where in memory a variable is stored A variable that holds an address The C/C++ operator that finds the address of a variable example int x, *p; // create an int and a pointer to an int p = &x; // store address of x in pointer variable p * The C/C++ operator that takes an address and gives the variable example *p = 3; // store 3 in int pointed to by p WHAT CAN POINTERS POINT AT? a. Existing Variables b. Dynamically Allocated Variables // existing vars int x; char name[100]; struct Trip t1; int *p4 = new int; char *p5 = new char[100]; struct Trip *p6 = new struct Trip; int *p1 = &x; char *p2 = name; // arrays don t need & struct Trip *p3 = &t1; QUEST:HOW DOIDEREFERENCE POINTERS? ANS:DEPENDS ON TYPE OF POINTEE. Simple Variables Arrays Structs // create storage int x; float f; int *p1; float *p2; int *p3; // assign *p1 = &x; *p2 = &f; *p3 = new double; // use *p1 = 4; *p2 = 1.3923; *p1 += 2; *p3 = *p2 + *p2; // create storage struct Trip sched[10]; int temps[31]; struct Trip *p4; int *p5; float *p6; // assign p4 = sched; p5 = temps; p6 = new float[7]; // use p4[2].orig = "BOS"; p5[23] = 21; p6[2] = 29.23; // create storage struct Trip t1; struct Trip *p7; struct Person *p8; // assign p7 = &t1; p8 = new Person; // use p7->dest = "DFW"; p8->lastname = "Lee"; Mar 23, 2009 13:10