[Solved] One Important Uses Pointers Dynamic Allocation Memory C Commands Let User Request Chunk Me Q37227911
One of the most important uses of pointers is for dynamicallocation of memory. In C++ there are commands that let the userrequest a chunk of memory from the operating system, and use thismemory to store data. This enables the program to use exactly theright amount of memory. This is particularly important as weprocess very large amounts of data. In this lab we will learn moreabout dynamic memory allocation in C++.
In order to request memory for dynamic variables, we need to dothe following:
- Declare a pointer variable of the desired data type
- Use the “new” command to request memory
- Make use of the memory in the program
- Use the “delete” command to return memory
- Set the pointer variable to NULL
Instructions:
Consider the following C++ program. It declares an array ofintegers called “data” and uses a for loop to initialize the arrayto random values between 0..99.
// Include statements#include <cstdlib>#include <iostream>using namespace std; // Main functionint main(){ // Declare variables int data[100]; int number = 0; // Get user input cout << “Enter number of values:n”; cin >> number; // Process data for (int i = 0; i < number; i++) { data[i] = random() % 100; cout << data[i] << ” “; } cout << endl; return 0 ;}
Step 1: Copy this program into your C++ programeditor, and compile it. Hopefully you will not get any errormessages. Run the program with a variety of input values to seewhat it prints. What happens if you type in a number between1..100? What happens if you type a number less than 1? What happensif you type in a number greater than 100?
Step 2: In your experiments above, you may havenoticed that entering a number like 111 or 222 output the samenumber of data values. How can this be possible? To find out whatis going on, add the line “cout << “number = ” <<number << endl;” before and after the for loop. Recompile theprogram, and run it again with inputs 111 and 222. This time youwill see that the variable number has changed. It was clobbered inyour for loop when you went out of the array bounds of “data”.
Step 3: Your next step is to replace yourstatic array data with a dynamic array. To to this, edit yourprogram, and remove the “int data[100];” declaration and replace itwith “int * data;”. Now data is a pointer variable, and we need toinitialize it somehow. We need to request a chunk of memory that isjust the right size for this execution. To do this, add the line”data = new int[number];” just after the cin line and just beforethe for loop. Compile and run the program to see what happens.
Step 4: Test your new program with numbervalues between 1..100. Now try number values greater than 100. Forfun, see what you get with 1000000. Congratulations, you have justmade a program with a dynamic array that resizes based on userinput.
Step 5: Now test your program with a numbervalue less than 0. Hmmm, it looks like the new command really doesnot like negative sized memory requests. To fix your program, youshould add some error checking code after the cin statement thatchanges any negative number value into zero instead. This way theprogram will not die with horrible error messages.
Step 6: It is very important to return memoryto the operating system when you are finished working with it.Otherwise, your program may have a “memory leak” and borrow moreand more memory until the system runs out of memory and yourprogram will die with strange and wonderful error messages. In C++the command to return memory to the O/S is “delete”. Edit yourprogram and add “delete [] data;” just above the “return 0;” linein your program. When you recompile and run your program, it shouldwork as before, but you have avoided a memory leak.
Step 7: Test your program one last time with avariety of inputs to make sure it is working correctly. Then uploadyour final program into the auto grader by following theinstructions below.
Expert Answer
Answer to One of the most important uses of pointers is for dynamic allocation of memory. In C++ there are commands that let the u… . . .
OR

