Pointers and Memory Management |
|
Imagine you write the following program: using namespace System; int main() { typedef double Salary; Salary Monthly; Salary Hourly, Weekly; double WeeklyHours; return 0; } When your program launches, that is, when it loads in the computer memory, the compiler reserves an appropriate amount of space in the heap memory for each declared variable. This technique of providing memory space is called static allocation, the memory space is "allocated" to that variable. Just after the program has launched and memory has been allocated (or assigned) to each variable, each memory space is filled 0. Check it with the following program: using namespace System; int main() { typedef double Salary; Salary Monthly; Salary Hourly, Weekly; double WeeklyHours; Console::WriteLine("Employee Payroll"); Console::Write("Weekly Hours: "); Console::WriteLine(WeeklyHours); Console::Write("Hourly Salary: "); Console::WriteLine(Hourly); Console::Write("Weekly Salary: "); Console::WriteLine(Weekly); Console::Write("Monthly Salary: "); Console::WriteLine(Monthly); return 0; } This would produce the following warnings: warning C4700: uninitialized local variable 'WeeklyHours' used warning C4700: uninitialized local variable 'Hourly' used warning C4700: uninitialized local variable 'Weekly' used warning C4700: uninitialized local variable 'Monthly' used It would display the following: Employee Payroll Weekly Hours: 0 Hourly Salary: 0 Weekly Salary: 0 Monthly Salary: 0 Press any key to continue As you can see, the memory allocated to each variable is filled with a 0 value. If you decide to use a pointer to a variable, you can declare and initialize such a variable. This system of declaring and initializing a pointer also allows the compiler to allocate a memory space for the pointer in the heap. Remember, just like a regular variable, a pointer is also a variable and uses its own memory space. This pointer can also be assigned a value that would be shared with the pointed to variable: using namespace System; int main() { typedef double Salary; Salary Monthly; Salary Hourly, Weekly; double WeeklyHours; double *PHourly; Hourly = 12.55; WeeklyHours = 42.50; PHourly = &Hourly; Weekly = Hourly * WeeklyHours; Console::WriteLine("Payroll Preparation"); Console::WriteLine("Enter Employee's Weekly Hours: "); Console::WriteLine("Employee Payroll"); Console::Write("Weekly Hours: "); Console::WriteLine(WeeklyHours); Console::Write("Hourly Salary: "); Console::WriteLine(*PHourly); Console::Write("Weekly Salary: "); Console::WriteLine(Weekly); Console::Write("Monthly Salary: "); Console::WriteLine(Monthly); return 0; } This would produce: Payroll Preparation Enter Employee's Weekly Hours: Employee Payroll Weekly Hours: 42.5 Hourly Salary: 12.55 Weekly Salary: 533.375 Monthly Salary: 0 Press any key to continue . . . Notice that the Monthly variable is still holding 0 and it has not been used throughout the program.
Instead of letting the compiler provide memory when your program comes up, you can ask it to use memory only when necessary. In that case, the compiler would allocate memory when the program is executing. This is called dynamic allocation. Dynamic allocation allows you to use memory "as needed" and not "as required". To dynamically assign memory, the C++ language provides the new operator. Here is the syntax to use it: PointerName = new DataType; The PointerName must be a declared pointer. You could have declared it previously or you can declare it when performing memory allocation. The assignment operator "=" and the new keyword are required. they let the compiler know that you are about to request memory. The DataType parameter can be any of those we are already familiar with, but it must be the same as the variable it is pointing to. This means that, if PointerName points to an integer variable, the data type must be an integer: using namespace System; int main() { typedef double Salary; Salary Monthly; Salary Hourly, Weekly; double WeeklyHours; double *PHourly; Hourly = 12.55; WeeklyHours = 42.50; PHourly = &Hourly; *PHourly = 14.25; Weekly = Hourly * WeeklyHours; Console::WriteLine("Payroll Preparation"); Console::WriteLine("Employee Payroll"); Console::Write("Weekly Hours: "); Console::WriteLine(WeeklyHours); Console::Write("Hourly Salary: "); Console::WriteLine(*PHourly); Console::Write("Weekly Salary: "); Console::WriteLine(Weekly); Console::Write("Monthly Salary: "); Console::WriteLine(Monthly); PHourly = new double; return 0; } Whenever you (decide to) dynamically allocate memory to a pointer variable, if the pointer had been previously initialized, the value that was stored in that pointer is lost (obsolete). Furthermore, if the pointer was pointing to an already declared variable, the relationship is broken: the pointer would not point to that variable anymore. In other words, both the pointer and the variable it was pointing to do not hold the same value anymore. The pointed to variable keeps its value because it is independent from the pointer (it is the pointer that is pointing to the variable and not vice-versa).
After dynamically allocating memory, you can assign a new value to the pointer for any purpose. Once the memory is not in use anymore, you should reclaim it. This is done with the delete operator. The formula to use it is: delete PointerName; The delete keyword is required. It lets the compiler know that you do not want to use the pointer anymore. The PointerName is the name of the pointer that was dynamically allocated with the new operator. You cannot use the delete operator if you did not previously use the new operator to allocate memory. To use the delete operator, simply type delete followed by the name of the pointer whose memory you want to reclaim. This operation is referred to as deallocating the memory (you are deallocating the memory that was dynamically allocated): using namespace System; int main() { double * PHourly = new double; Console::WriteLine("Stuff..."); delete PHourly; return 0; }
|
|
||
Previous | Copyright © 2006-2016, FunctionX, Inc. | Next |
|