For example a program to calculate the area of a rectangle will need three

For example a program to calculate the area of a

This preview shows page 18 - 21 out of 34 pages.

program, you can de ne those variables up front. For example, a program to calculate the area of a rectangle will need three variables: one for the rectangle s length, one for the rectangle s width, and one to hold the area. But what about those times when you don’ t know how many variables you need? For example, a program to calculate sum of an array of its size determined with a user by entering a value from keyboard. Quite simply, you allow the program to create its own variables on the y. This is called dynamic memory allocation, and is only possible through the use of pointers. To dynamically allocate memory means that a program, while running, asks the computer to set aside a chunk of unused memory large enough to hold a variable of a specific data type. The way a C++ program requests dynamically allocated memory is through the new operator. Assume a program has a pointer to an int defined as int *iptr; Here is an example of how this pointer may be used with the new operator: iptr = new int; This statement is requesting that the computer allocate enough memory for a new int variable. The operand of the new operator is the data type of the variable being created. Once the statement executes, iptr will contain the address of the newly allocated memory.
Image of page 18
19 A value may be stored in this new variable by dereferencing the pointer: *iptr = 25; Any other operation may be performed on the new variable by simply using the dereferenced pointer. Here are some example statements: cout << *iptr; // Display the contents of the new variable. cin >> *iptr; // Let the user input a value. total += *iptr; // Use the new variable in a computation. Although the statements above illustrate the use of the new operator, there’s little purpose in dynamically allocating a single variable. A more practical use of the new operator is to dynamically create an array. Here is an example of how a 100-element array of integers may be allocated: iptr = new int[100]; Once the array is created, the pointer may be used with subscript notation to access it. for (int count = 0; count < 100; count++) cin>>iptr[count] ; But what if there isn’t enough free memory to accommodate the request? What if the program asks for a chunk large enough to hold a 100,000-element array of floats, and that much memory isn’t available? When memory cannot be dynamically allocated, C++ throws an exception and terminates the program. Under older compilers, the new operator returns the address 0, or NULL when it fails to allocate the requested amount of memory. When a program is finished using a dynamically allocated chunk of memory, it should release it for future use. The delete operator is used to free memory that was allocated with new. Here is an example of how delete is used to free a single variable, pointed to by iptr:
Image of page 19
20 delete iptr; If iptr points to a dynamically allocated array, the [] symbol must be placed between delete and iptr : delete [] iptr; Write a program to calculate sum of array elements and average value of the sum. The size of the array should be allocated dynamically.
Image of page 20
Image of page 21

You've reached the end of your free preview.

Want to read all 34 pages?

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes