CSCI 2610
Answers to practice questions for quiz 5

  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;
        }
    
        *p = 25
        *q = 25
        *r = 25
        *q = 25
    

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

    When the function returns.

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

    When it is explicitly deleted using the delete operator.

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

    The physical size is the amount of memory allocated. The logical size is the amount that is in use, which might be smaller than the physical size.

  5. What is a dangling pointer?

    A dangling pointer is a pointer to memory that has either been destroyed (either automatically or by using delete) or that has never been allocated at all.

  6. What is a memory leak?

    A memory leak occurs when a program loses all pointers to memory that it allocated using new, without deleting that memory. The memory becomes inaccessible, but cannot be reused.

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

    The operating system kills the program when *p is used. (The program gets some kind of memory fault. On a Unix system, it gets a segmentation fault, which is a kind of memory fault. On a Windows system, it gets a general protection fault.)

  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?

    Yes, an array can be allocated using new. Write

        p = new char[20]
    
    (assuming that variable p has already been declared to have type char*).

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

    It will compile. But the cin object tries to read a word and store it at the address in variable s. Since no memory address has been put into s, variable s holds a junk address. The results are unpredictable. It might cause a memory fault, or it might do a variety of other bad things.

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

    The pointer has type Giraffe*.

  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.

    You can compare two null-terminated strings using strcmp.

       if(strcmp(s,t) == 0) x = 1;
    

  12. How can you copy the null-terminated string in array s into array d?
        strcpy(d,s);