Skip to main content

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?
  • heap (a.k.a. free store)
  • stack
  • static 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 deallocatethe entire dynamic array, which of the following is correct?
  • delete [] arr;
  • delete arr;
  • delete arr[];
  • delete[] arr[];

7.

A pointer stores what?
  • a memory location
  • a variable name
  • 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;
  • Ziggy
  • Skye
  • Ruthie
  • Cleo
  • no output (empty string)

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;
  • Ziggy
  • Skye
  • Ruthie
  • Cleo
  • no output (empty string)

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.