Skip to main content
Contents
Dark Mode Prev Up Next Profile
\(
\newcommand{\lt}{<}
\newcommand{\gt}{>}
\newcommand{\amp}{&}
\definecolor{fillinmathshade}{gray}{0.9}
\newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}}
\)
Section 1.6 Homework 6 -- Pointers
Exercises Exercises
1.
When you declare a variable, the amount of memory that will be allocated for that variable is determined when?
When the program is being compiled.
During the linking phase of the build process.
When the program starts running.
2.
Your program needs more memory during execution time. From where can you get this additional memory?
3.
4.
5.
6.
Suppose that we have already allocated storage on the heap for a dynamic array of Person objects:
Person* arr = new Person[10];
To
deallocate the entire dynamic array, which of the following is correct?
delete [] arr;
delete arr[];
7.
the value of a primitive data type (e.g., an integer or a bool)
8.
Which of the following functions DOES NOT have a memory leak?
void func(string word)
{
int* ptr = NULL;
int len = word.size();
ptr = &len
cout << "Length of " << word << " is " << *len << endl;
}
void func(string word)
{
int* ptr = new int;
*ptr = word.size();
}
void func(string word)
{
int* ptr = new int[2];
ptr[0] = word.size();
delete ptr;
}
9.
Which of the following memory drawings accurately depicts what memory will look like after the following lines of code execute? HINT: Pay attention to the difference between "stack" and "heap".
int x = 23;
int* p = &x;
int** m = &p;
10.
Which of the following memory drawings accurately depicts what memory will look like after the following lines of code execute?
Person* ptr = NULL;
ptr = new Person[2];
ptr[0] = Person(βCleoβ, βWilsonβ, 7);
ptr[1] = Person(βMr.β, βGrayβ, 10);
Person* Q = ptr;
11.
Which of the following memory drawings accurately depicts what memory will look like after the following lines of code execute?
char* p_array = new char[3];
char** p_p_array = &p_array;
p_array[0] = 'A';
p_array[1] = 'B';
p_array[2] = 'C';
delete p_array;
12.
When the following code executes, what is the
output ?
string s1 = "Ruthie";
string s2 = "Skye";
string* my_ptr = &s1;
string* other_ptr = &s2;
other_ptr = my_ptr;
*other_ptr = "Ziggy";
cout << *my_ptr;
13.
When the following code executes, what is the
output ?
string s1 = "Ruthie";
string s2 = "Skye";
string* my_ptr = &s1;
string* other_ptr = &s2;
*other_ptr = *my_ptr;
*other_ptr = "Ziggy";
cout << *my_ptr;
14.
Which of the following statements will be true after the following code has executed?
string mystr = "cool";
string* sptr1 = &mystr;
string* sptr2 = new string;
sptr1 = sptr2;
mystr = "cats";
The value of *sptr1 will be "cool", and the value of *sptr2 will be "cats".
The value of *sptr1 will be "cats", and the value of *sptr2 will be "cool".
The value of *sptr1 will be "cool", and the value of *sptr2 will be "".
The value of *sptr1 will be "", and the value of *sptr2 will be "".
15.
16.
You have attempted
of
activities on this page.