21.11.  Review Questions

[ fromfile: memoryaccess-questions.xml id: memoryaccess-questions ]

  1. What is defined in the following statement?

    int* p, q;
    

      p is an int*, while q is an int.

  2. What is a memory leak? How does it happen?

  3. Compare the way that +, -, ++, and -- operators work on pointers with the way that they work on int or double.

      With pointers, those operators can only be used if the pointer holds the address of the first element of an array and provided that the results stay within the context of the array. The second operand of + and - must be an int n and the result of the operation will be an address equal to the original address + or - n times the size of the type of the "pointed-to" thing. The unary increment/decrement operators work similarly, but with a built-in second operand of 1.

  4. What happens if delete is applied to a pointer that has just been deleted?

      The results are undefined - heap corruption is likely.

  5. When an array is passed to a function as a parameter, what is copied onto the stack?

      The address to the first element in the array.

  6. What is dynamic memory? How do you obtain it in C++?