CSCI 2610
Practice questions for quiz 5

The focus will be on dynamic memory allocation, plus a little on using arrays.

  1. What does the test() print? Do a careful hand simulation.
        void test()
        {
          int* s;
          int* p = new int;
          int* q = p;
          int* r = new int;
          *p = 8;
          s = p;
          *q = 25;
          *r = *s;
          q = r;
          cout << "*p = " << *p << endl;
          cout << "*q = " << *q << endl;
          cout << "*r = " << *r << endl;
          cout << "*s = " << *s << endl;
        }
    

  2. When is a variable that is local to a function destroyed (or deallocated)?

  3. When is a variable that is allocated using new destroyed (or deallocated)?

  4. What is the difference between the physical size of an array and the logical size of an array?

  5. What is a dangling pointer?

  6. What is a memory leak?

  7. The NULL pointer is memory address 0. What happens if you write execute
        char* p;
        p = NULL;
        *p = 'x';
    

  8. Can an array be allocated using new? If so, how would you allocate an array of 20 characters, and make pointer variable p point to that array?

  9. What happens if you perform the following? Will it compile? Is it a reasonable program fragment?
        char *s;
        cin >> s;
        cout << s;
    

  10. If Giraffe is a type, then expression new Giraffe returns a pointer. What is the type of the pointer?

  11. How can you compare two strings to see if they are the same? Write a statement that sets integer variable x to 1 just when two arrays s and t hold the same string.

  12. How can you copy the null-terminated string in array s into array d?