Starting from:

$30

Lab 3: Dynamic Memory

CS 2401 
Lab 3: Dynamic Memory

(You can type your answers on the answer sheet so that it can be submitted electronically.)
Part One: (Static and automatic variables.)
1. If you have not already done so, make a directory for your CS2401 stuff
mkdir cs2401
2. Go into that directory
cd cs2401
3. Once you are in there make a directory for your labs and inside that one a directory for lab4.
4. Open a new file in your favorite editor, type #include <iostream> and put in your
using statement.
5. Type in this little function:
void pretty( ){
 int x = 0;
x++;
for(int i = 0; i < x; ++i){
cout << ’*’;
}
cout << endl;
}
6. Write a main that has a loop that calls your function six times.
7. On your answer sheet write the output that you see after you have compiled and run this
program.
8. Now change the first line in the function pretty so it looks like this:
static int x = 0;
9. Recompile and run this program.
10. Write the output for this function on your answer sheet.
11. What was the effect of using static? (Write on answer sheet.)
12. If you remove static what is the effect? (Write on answer sheet.)
Part Two: (Dynamic Variables.)
13. Start a new program – again starting out with the #include <iostream> and
using namespace std; - this time you will only need a main
14. Declare a pointer capable of pointing at an int.
15. Make the pointer point at a new integer.
16. Print out the address of the new integer. (On your answer sheet write how you did this as well
as the address that shows up.)
17. Print out the address of that pointer. (On your answer sheet write how you did this as well as
the address that shows up.)
18. Now, using the * operator, set the value of your integer to 2401.
19. Write a loop with an integer counter that counts to 10 and in the body of the loop does this:
++(*ptr);
cout << *ptr << “is stored at “ << ptr << endl;
20. On your answer sheet write down the first and last lines that appear here.
21. Now change the body of your loop to this:
{
++(ptr); // notice I took out the *
cout << *ptr << “is stored at “ << ptr << endl;
}
The difference here is that you were moving the pointer instead of changing the value being
pointed at.
22. On your answer sheet write down what the last two lines of your output looks like.
Part Three (A dynamic array)
23. Now let’s make a dynamic array with the pointer that you have been using.
24. Begin by declaring a couple of size_t variables for capacity and used.
25. Initialize capacity to five and used to zero.
26. Declare an extra int* tmpptr, which we will use in resizing.
27. Type this code:
ptr = new int[capacity];
for(size_t i=0; i<25; ++i){
ptr[used] = rand(); // you will need #include<cstdlib>
// at the top for this to work
cout << ptr[used];
used++;
 if(used == capacity){
cout<<”Sorry no room left.\n”;
break;
}
}
28. Note how many numbers you see in the output.
29. Now replace the cout and the break for a full array with a resizing which will do this:
a. Set the tmpptr to a new integer array of capacity +5 size
b. Copy the data from the original array to the new one using a loop or the copy function
which we talked about in class
c. Adjust the capacity variable so it accurately reflects the size of the new array
d. Delete the original array, remembering to use the [ ]’s
e. Assign ptr to tmpptr, so these pointers will both point at the newer array
f. Do a cout << ”Resized\n”; to report that this was done.
30. Now run your program – what do you see?
31. Finally, comment highlighted code “cout << ptr[used];” in question 15, and then at the
end of this loop that has filled and output the array, put this
g. tmpptr[2] = 0;
h. Write a loop that will output all the numbers in the ptr array.
32. What do you see?
33. Submit the finished code for each of the three parts and your answer sheet to Blackboard,
making sure that your name appears on both your code and the answer sheet.

More products