Download Memory Management and Dynamic Structures: Pointers, Dynamic Variables, and Arrays - Prof. and more Study notes Electrical and Electronics Engineering in PDF only on Docsity! Pointers and Dynamic Structures Overview of Memory Model Pointer Variables Using Dynamic Variables and Arrays Using Dynamic Structures and Arrays of Structures Reference Parameters Revisited Class Constructors Copy Constructors Class Destructors Overview of Memory Model Computer memory can be viewed as an "array" of storage locations On a PC this array may be limited to size of physical memory On a workstation "virtual" memory size is almost unlimited Memory at run time typically contains the following: O/S Code O/S Data Program Code Program Data The Stack Free Memory The Heap 1 - operating system code The O/S code is loaded into memory when machine "boots" This code is executed when O/S functions are called 2 - operating system data Provides space for all global variables of the O/S On some systems this data may be on the stack 3 - program code The program code is loaded when the program is "invoked" The code is executed when program functions are called 4 - program data Provides space for all global variables of the program On some systems this data may be on the stack Using Dynamic Variables and Arrays Pointers can also contain addresses of space requested from the heap Must specify amount of space needed (number & types of variables) Different data types are different sizes (char=1, int=4, float=4, etc.) This mechanism gives us a way to create variable sized arrays The new command creates space, the delete command releases space Use *data or data[i] to access dynamic variables or arrays Use delete or delete [] to return memory to the heap // Allocate a dynamic variable int *value = NULL; value = new int; // Using a dynamic variable *value = 13; delete value; int *john = new int; cout << *john; // Allocate a dynamic array int size; cout << "Enter size of array:"; cin >> size; float *data = new float[size]; // Use the dynamic array for (int i=0; i < size; i++) data[i] = i * 3.14159 / size; delete []data; Using Dynamic Structures and Arrays of Structures Can also request space for one or more structures on the heap This is used for creating linked lists, trees, and graphs Must define structure and use new and delete as before Need to use data->Part or (*data).Part to refer to subparts // Allocate a dynamic structure struct student { char Name[20] int Year; float GPA; }; student *temp = new student; student junk; junk.Year = 2006; // Using a dynamic structure strcpy(temp->Name, "Tom Jones"); temp->Year = 2006; temp->GPA = 3.0; // Allocate a dynamic array of structures int size; cout << "Enter size of array:"; cin >> size; student *data = new student[size]; // Initialize dynamic array of structures for (int i=0; i < size; i++) { strcpy(data[i].Name, ""); data[i].Year = 2006; data[i].GPA = 0.0; } cin >> size; Point *p = new Point[size]; for (int i=0; i<size; i++) { p[i].setX( rand() % 100 ); p[i].setY( rand() % 100 ); } // Use dynamic array of structures student *ptr = NULL; for (int i=0; i < size; i++) if (strcmp(data[i].Name, "John Smith") == 0) ptr = data[i]; if (ptr != NULL) ptr->GPA = 3.5; // Release memory Class Constructors The constructor function is called when a class variable is declared This function has the same name as the class itself Constructor initializes private data members of the class Dynamic size objects can be created using the new command // Array class definition class Array { public: Array(int size); // constructor Array(const & Array); // copy constructor ~Array(); // destructor void ReadArray(char filename[]); void ProcessArray(int param); void WriteArray(char filename[]); private: int Length; int *Data; }; // Constructor function Array::Array(const int size) { Length = size; Data = new int[Length]; } // Write array data to file void Array::WriteArray(char filename[]) { ofstream dout(filename); for (int i=0; i<Length; i++) dout << Data[i] << " "; dout << endl; dout.close(); } // Main program int main() { int size; cin >> size; Array a(size); a.ReadArray("infile.txt"); a.ProcessArray(42); a.WriteArray("outfile.txt"); // magic call to “a” destructor } Copy Constructors The copy constructor is used to create copies of objects It is also used when objects are passed by value into functions Important to define when dynamic memory is allocated in constructor 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::Array(const Array & a) { // Allocate space and make copy Length = a.Length; Data = new int[Length]; for (int i = 0; i < Length; i++) Data[i] = a.Data[i]; } // Usage example Array a(100); a.ReadArray("infile.txt"); Array b(a);