Docsity

Memory Management and Dynamic Structures: Pointers, Dynamic Variables, and Arrays - Prof. , Study notes of Electrical and Electronics Engineering

Partial preview of the text

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

Pointer Variables  All program variables start at some memory location in the computer  Pointer variables contain the starting addresses of other variables  Can access variables "indirectly" using pointers  Need to be very careful about how and when we use pointers // Declarations float value = 42; float *temp = NULL; // Pointer initialization temp = &value // Direct variable access value = 77; cout << value; // Indirect variable access *temp = 94; cout << *temp;

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;

// 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

delete temp; delete []data;

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 }

Class Destructors  The destructor function is automatically called when the object is no longer in scope  This function has the same name as the class with a ~ added in front  Destructor cleans up anything the class initialized or allocated  Must make sure to free space using destroy in a destructor function  Failure to clean up after new will eventually result in heap overflow // Destructor function Array::~Array() { Length = 0; delete []Data; }