Description
(You can type your answers on the answer sheet so that it can be submitted electronically.)
Part One: (Static and automatic variables.)
-
If you have not already done so, make a directory for your CS2401 stuff
mkdir cs2401
// I’m pretty sure that most of you have already done this
-
Go into that directory
cd cs2401 -
Once you are in there make a directory for your labs and inside that one a directory for lab3.
-
Open a new file in your favorite editor, #include<iostream> and put in your
using statement. -
Type in this little function:
void pretty( ){
int x = 0;
x++;
for(int i = 0; i < x; ++i){ cout<<’*’;}
cout<<endl;
}
-
And write a main that has a loop that calls your function six times.
-
On your answer sheet write the output that you see after you have compiled and run this program.
-
Now change the first line in the function pretty so it looks like this:
static int x = 0;
-
Recompile and run this program.
-
Write the output for this function on your answer sheet.
-
What was the effect of using static? (Write on answer sheet.)
-
If you use the word auto instead of static what is the effect? (Write on answer sheet.)
Part Two: (Dynamic Variables.)
-
Start a new program – again starting out with the #include<iostream> and the
using namespace std; – this time you will only need a main -
Declare a pointer capable of pointing at an int. (int * ptr; )
-
Make the pointer point at a new integer. (ptr = new int; )
-
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.) (cout << ptr <<endl; )
-
Print out the address of that pointer. (On your answer sheet write how you did this as well as the address that shows up.) (cout << &ptr; )
-
Now, using the * operator, set the value of your integer to 2401. (*ptr = 2401; )
-
Write a loop with an integer counter that counts to 110 and in the body of the loop does this:
++(*ptr); cout<<*ptr<< “is stored at “ <<ptr<<endl; -
On your answer sheet write down first and last line that appear here.
-
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.
-
Now change the loop so that it counts to 1,000,000, and have the ptr move backwards by changing the ++(ptr) to a –(ptr). Compile and run this.
-
On your answer sheet write down what the last two lines of your output looks like.
-
It’s not always so easy to tell exactly where the crash is happening, and this is where a debugger can be very useful. Let’s run the debugger on the program you just did:
-
g++ -g parttwo.cc
-
gdb a.out
-
run
-
When the program crashes ask it
-
where
-
-
On your answer sheet write down what gdb just told you.
-
Sometimes it’s good to single-step up through your program. Try this for the first few iterations of the loop. You can always quit by hitting q.
-
gdb a.out // note that you don’t have to recompile here since we’re not changing anything
-
break main // you could also have set up your break point right above the loop by doing break parttwo.cc:12 — or whatever line number is right above your loop
-
run
-
Now single-step through your program by hitting
n -
If you want to see the value of the pointer you can always “peek” at the variable:
p variable_name
-
-
Submit the finished code for each of the two parts and your answer sheet to Blackboard, making sure that your name appears on both your code and the answer sheet.