Table Of Contents
Previous Section Next Section

Program 93: No Magic

Something strange was happening to the class info. Your valiant author was assigned the task of figuring out what was happening. After a little playing around, I decided that what was probably happening is that someone had gotten a hold of a bad pointer and was stepping all over the class.

To try and find out where the class was being overwritten, I put a couple of magic numbers at the beginning and end of the data for the class. I expected these magic numbers to get changed when things went wrong. But I was surprised to learn that things went wrong much sooner than expected.

So why does the magic go out of the class?

  1 #include <stdlib.h>
  2 #include <iostream>
  3 #include <cstring>
  4
  5 /************************************************
  6  * info -- A class to hold information.         *
  7  *                                              *
  8  * Note:                                        *
  9  *      Because someone is walking all over our *
 10  *      memory and destroying our data, we      *
 11  *      have put two guards at the beginning    *
 12  *      and end of our class.    If someone     *
 13  *      messes with us these numbers will       *
 14  *      be destroyed.                           *
 15  *                                              *
 16  * Member functions:                            *
 17  *      set_data -- Store a string in our data. *
 18  *      get_data -- Get the data string. *
 19  *      check_magic -- Check the magic numbers. *
 20  ************************************************/
 21 // Magic numbers for the start and end of the
 22 // data in the class info
 23 const int START_MAGIC = 0x11223344;
 24 const int END_MAGIC = 0x5567788;
 25 class info
 26 {
 27     private:
 28         // Magic protection constant
 29         const int start_magic;
 30
 31         // String to be stored
 32         char data[30];
 33
 34         // Magic protection constant
 35         const int end_magic;
 36     public:
 37         info(void):
 38             start_magic(START_MAGIC),
 39             end_magic(END_MAGIC)
 40             {}
 41
 42         // Copy constructor defaults
 43         // Assignment operator defaults
 44         // Destructor defaults
 45
 46         // Store some data in the class
 47         void set_data(
 48             // Data to be stored
 49             const char what[]
 50         )
 51         {
 52             strcpy(data, what);
 53         }
 54
 55         // Get the data from the class
 56         char *get_data(void)
 57         {
 58             return (data);
 59         }
 60
 61         // Verify that the magic
 62         // numbers are correct
 63         void check_magic(void)
 64         {
 65             if ((start_magic != START_MAGIC) ||
 66                 (end_magic != END_MAGIC))
 67             {
 68                 std::cout <<
 69                     "Info has lost its magic\n";
 70             }
 71         }
 72 };
 73
 74 /************************************************
 75  * new_info -- Create a new version of the      *
 76  *      info class.                             *
 77  ************************************************/
 78 struct info *new_info(void)
 79 {
 80     struct info *result; // Newly created result.
 81
 82     result = (struct info *)
 83         malloc(sizeof(struct info));
 84
 85     // Make sure the structure is clear
 86     memset(result, '\0',  sizeof(result));
 87
 88     return (result);
 89 }
 90 int main()
 91 {
 92     // An info class to play with
 93     class info *a_info = new_info();
 94
 95     a_info->set_data("Data");
 96     a_info->check_magic();
 97     return (0);
 98 }

(Next Hint 153. Answer 98.)

Table Of Contents
Previous Section Next Section