CSCI 261 - Programming Concepts - Spring 2021

Lab XC - Pointers & Dynamic Memory

This lab is due by Wednesday, May 05, 2021, 11:59 PM.
As with all labs you may, and are encouraged, to pair program a solution to this lab. If you choose to pair program a solution, be sure that you individually understand how to generate the correct solution.

The goal of this lab is to gain familiarity with using the concepts learned on pointers, addresses, and dynamic memory.


Pointer Instructions


Create a new folder SetXC and a new project LXC with main.cpp that does the following:

  1. Declare an integer named iNum with initial value 9.
  2. Declare a double named dNum with initial value 14.25.
  3. Declare two pointers to integers named iPtr1 and iPtr2.
  4. Declare a pointer to a double named dPtr.
  5. Assign the address of iNum to iPtr1.
  6. Output the address of iNum and be sure to identify to the user what you are displaying. There are two ways you can do this; you should do both, to convince yourself they are the same.
  7. Use iPtr1 to display the contents of iNum.
  8. Try to assign the address of dNum to iPtr1. What error message do you see? Comment out this bad line of code, but include the error message as a comment directly below.
  9. Assign the address of dNum to dPtr.
  10. Output the address and then the contents of dNum (using dPtr).
  11. Directly change the value of iNum to 7.
  12. Use iPtr1 to output the contents of iNum.
  13. Assign iPtr2 to have the same value as iPtr1. Do not reference iNum; instead use the address stored in iPtr1.
  14. Output the address in iPtr2.  This should be the same as displayed in step 6.
  15. Output the value pointed to by iPtr2.
  16. Using iPtr2, change the contents of iNum to 12.
  17. Output the contents of iNum three times, first using iPtr1, then using iPtr2, then iNum directly. In each case, identify what the user is seeing appropriately.

Dynamic Memory Instructions


The next steps will illustrate the use of dynamic memory.  You can continue adding to the same main.cpp .

  1. Declare another pointer to an integer named iPtr3.
  2. Declare an integer named num. Have the user input the value for num, which represents the number of items in an array.
  3. Using the new operator, give iPtr3 enough memory to contain num integers.
  4. Write a loop that prompts the user for num values and stores the values entered in the iPtr3 array.
  5. Write a loop that sums the values in the array.
  6. Display the sum of the values.
  7. Using the delete operator, return the memory in iPtr3 back to the available memory heap.

Lab Submission



You will submit your solution to this lab with the rest of SetXC. Detailed instructions for doing this are posted in Assignment XC.


This lab is due by Wednesday, May 05, 2021, 11:59 PM.
As with all labs you may, and are encouraged, to pair program a solution to this lab. If you choose to pair program a solution, be sure that you individually understand how to generate the correct solution.