Lab Assignment #3 Solution



(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

// I’m pretty sure that most of you have already done this

  1. Go into that directory
    cd cs2401

  2. Once you are in there make a directory for your labs and inside that one a directory for lab3.

  3. Open a new file in your favorite editor, #include<iostream> and put in your
    using statement.

  4. Type in this little function:

void pretty( ){
int x = 0;


for(int i = 0; i < x; ++i){ cout<<’*’;}



  1. And write a main that has a loop that calls your function six times.

  2. On your answer sheet write the output that you see after you have compiled and run this program.

  3. Now change the first line in the function pretty so it looks like this:

static int x = 0;

  1. Recompile and run this program.

  2. Write the output for this function on your answer sheet.

  3. What was the effect of using static? (Write on answer sheet.)

  4. If you use the word auto instead of static what is the effect? (Write on answer sheet.)

Part Two: (Dynamic Variables.)

  1. Start a new program – again starting out with the #include<iostream> and the
    using namespace std; – this time you will only need a main

  2. Declare a pointer capable of pointing at an int. (int * ptr; )

  3. Make the pointer point at a new integer. (ptr = new int; )

  4. 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; )

  5. 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; )

  6. Now, using the * operator, set the value of your integer to 2401. (*ptr = 2401; )

  7. 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;

  8. On your answer sheet write down first and last line that appear here.

  9. 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.

  10. 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.

  11. On your answer sheet write down what the last two lines of your output looks like.

  12. 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:

    1. g++ -g

    2. gdb a.out

    3. run

    4. When the program crashes ask it

    5. where

  13. On your answer sheet write down what gdb just told you.

  14. 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.

    1. gdb a.out // note that you don’t have to recompile here since we’re not changing anything

    2. break main // you could also have set up your break point right above the loop by doing break — or whatever line number is right above your loop

    3. run

    4. Now single-step through your program by hitting

    5. If you want to see the value of the pointer you can always “peek” at the variable:
      p variable_name

  15. 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.