Starting from:

$25

CSCI1730 - Breakout - Lab 04 - Solved

Memory Maps & Pointers, Oh My!

Problem / Exercise
Pointers are an integral part of C++. While they may be the cause of many headaches, they allow for a great deal of control and flexibility. Because they are low-level constructs, they require a solid understanding of the memory model behind it all.

Not every language has pointers, but what you learn about memory management will apply to almost every language. The goals for this lab are:

•    Understand and use pointers and pointer operations.

•    Discover the relationship between arrays and pointers.

•    Explore passing pointers as function parameters and the const modifier with pointers.

This lab is structured differently from previous labs. You’ll be asked to observe some code and comment on what its output should be.

1           Submission Instructions
Make sure your work is in a plain text file named ‘lab04.txt’ in a directory called LastName-FirstName-lab04 on your odin account. From within the parent directory, execute the following command:

$ submit LastName-FirstName-lab04 csci-1730

2           Warmup Activity
1. Compile and run the following C++ program:

#include <iostream> #include <cstdlib>

using std::cout; using std::endl; int main() {

int integer1, * p1, ** p2; integer1 = 10; // line 11 p1 = &integer1; // line 12 p2 = &p1;            // line 13

cout << "integer1 = " << integer1 <<endl; // line 15 cout << "p1 = " << p1 << endl;       // line 16 cout << "p2 = " << p2 << endl;         // line 17

return EXIT_SUCCESS;

} // main

Suppose after the declaration of variables integer1, p1, and p2, we have the following memory map:

Symbol Name
Type
Memory Address
Value
integer1
int
80
 
p1
int *
84
 
p2
int **
92
 
(a)    Fill in the “Value” column in the in the memory map to reflect the changes that are caused by lines 11, 12, and

13.

(b)    If we substitute lines 15–17 with the the following two statements, then what will be the output of the program?

(*p1)++;

cout << "integer1 = " << *p1 << endl;

(c)    Will the output of the program be the same if we substitute the above two lines with the following two statements?

integer1++;

cout << "integer1 = " << *p1 << endl;

(d)   Will the output of the program be the same if we substitute the above two lines with the following two statements?

*p2++;

cout << "integer1 = " << integer1 << endl;

(e)    Explain why the outputs of parts (c) and (d) are the same or different from each other.

2. Consider the following program and answer the questions below:

#include <iostream> #include <cstdlib>

int main() { int * p1; int ** p2; p2 = p1;

return EXIT_SUCCESS;

} // main

(a)    What does the variable p2 represent?

(b)    Will this program compile? Why or why note? (Try it!)

3           Arrays and Pointers Activity
Now let’s look at the relationship between pointer notation and array notation (Hint: They both accomplish the exact the same thing!).

1. Compile and run the following program, then answer the corresponding questions:

#include <iostream> #include <cstdlib>

using std::cout; using std::endl; int main() {

int iarray [10], * p1, * p2; p1 = iarray; p2 = &iarray[0];

for (int i = 0; i < 10; ++i) iarray[i] = i; for (int i = 0; i < 10; ++i) cout << *(p2 + i) << endl;

return EXIT_SUCCESS;

} // main

(a)    What is the type of the iarray variable (Hint: It’s some kind of pointer.)?

(b)    What is the value of iarray?

(c)    If you dereference iarray, what do you get?

(d)   What is the output of the program?

(e)    Will the output change if we replace the first for-loop with the following? Why or why not?

for (int i = 0; i < 10; ++i) *(p1 + i) = i;

(f)     Will the output change if we replace the second for-loop with the following? Why or why not?

for (int i = 0; i < 10; ++i) cout << *(p1 + i) << endl;

(g)    Will the output change if we replace the first for-loop with the following? Why or why not?

for (int i = 0; i < 10; ++i) *(p1++) = i;

4           Functions, Pointers, and Tricky Declarations Activity
In this part, you will get to play with pointers as function parameters and practice how to simulate pass-by-reference by passing pointers by value. Also, you will become familiar with complicated mixed representations of pointers, arrays, and functions.

1. What is the output of the following code (assume proper includes, etc)? Why? Which variable in the code is “passedby-reference”?

void divBy2(int & n) { n = n / 2;

} // divBy2

void divBy2(int * np) { *np = *np / 2; } // divBy2

int main() { int x = 44;

divBy2(x);

cout << x << endl;

divBy2(&x); // line 16 cout << x << endl;

return EXIT_SUCCESS;

} // main

2. In the code presented in the previous question, why is &x used for the function parameter on line 16?

5           Const Pointers Activity
1. Consider the following code (assume proper includes, etc):

int main() {

int x = 44; int y = 22;

// part (a) int * p1 = &x; *p1 = 11;

// part (b) const int * p2 = &x; *p2 = 11; p2 = &y;

// part (c) const int * const p3 = &x;

*p3 = 11; p3 = &y;

return EXIT_SUCCESS;

} // main

(a)    Which statements of the code marked for part (a) are valid, and which statements are invalid? If a statement is invalid, please explain why.

(b)    Which statements of the code marked for part (b) are valid, and which statements are invalid? If a statement is invalid, please explain why.

(c)    Which statements of the code marked for part (c) are valid, and which statements are invalid? If a statement is invalid, please explain why.

2. Consider the following code (assume proper includes, etc):

int a [2] = {1, 2};

const int foo() { return 2; } // foo

const int * bar() { return a; } // bar

int * const baz() { return a; } // baz int main() {

// part (a) int x = foo();

x++; foo()++;

// part (b) const int * p1 = bar();

p1++ (*p1)++; bar()++; (*bar())++;

// part (c) const int * p2 = baz(); p2++ (*p2)++; baz()++; (*baz())++; return EXIT_SUCCESS;

} // main

(a)    Which statements of the code marked for part (a) are valid, and which statements are invalid? If a statement is invalid, please explain why.

(b)    Which statements of the code marked for part (b) are valid, and which statements are invalid? If a statement is invalid, please explain why.

(c)    Which statements of the code marked for part (c) are valid, and which statements are invalid? If a statement is invalid, please explain why.

More products