-1

The following function must allocate memory for a database of objects, assigning a pointer to the array of objects.

A pointer will then loop down the array using pointer arithmetic and initialize each object to the correct values, following user input.

Here is the code that doesn't work:

//**********************************************************************
//*                        Get Database Records                        *
//**********************************************************************
record* get_records_database(int quantity)
{
   record *p_database; // Pointer to the records database
   record *p_record;   // Pointer to each object in the database
   int    index = 1; // Number of objects in the database

   // Allocate a database of object records
   try
   {
      p_database = new record[quantity];
   }
   catch (bad_alloc xa)
   {
      fatal_error(ALLOC_ERR, "get_records_database", 
                             "object records database");
   }

   // Loop processing object records until the database is filled
   // --- //

   // Test:
   p_database->set_amount(400);
   p_database->get_amount();

   return p_database;
}

The problem I face is fixing the following compiler error in VisualStudio: error C4703: potentially uninitialized local pointer variable 'p_employee_database' used.

This is a project; using new, try, catch, and pointers, is required; the structure of the function is required (not all written at this point); the return value of a pointer to a class is required. My teacher is extremely strict in following his requirements precisely.

Any help to the resolution of this error is greatly appreciated. Thank you ;)

Denis G. Labrecque
  • 1,023
  • 13
  • 29

1 Answers1

3

There are at least two issues with your code here:

   try
   {
      p_employee_database = new employee_bonus_record[employee_quantity];
   }
   catch (bad_alloc xa)
   {
      fatal_error(EMPLOYEE_ALLOC_ERR, "get_employee_records_database", 
                                      "employee bonus records database");
   }
   //.. rest of code, assuming p_employee_database is ok.

If the exception is thrown, the p_employee_database is uninitialized, however you failed to return from the function. Instead your logic proceeds using p_employee_database as if nothing is wrong, thus the compiler warning.

Even as you stated, fatal_error calls exit(), the compiler doesn't see this. It only looks at that block of code and gives the warning. If you want to suppress the warning, you can return nullptr.

   try
   {
      p_employee_database = new employee_bonus_record[employee_quantity];
   }
   catch (const bad_alloc& xa)
   {
      fatal_error(EMPLOYEE_ALLOC_ERR, "get_employee_records_database", 
                                      "employee bonus records database");
      return nullptr;
   }

The second thing wrong with the code is that you should catch the std::bad_alloc by const reference, not by value. See this article.

Community
  • 1
  • 1
PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45
  • Note that referencing `xa` gives a warning **warning C4101: 'xa': unreferenced local variable.** I'll have to look more into the general form of the `try/catch` method to see why this matters. – Denis G. Labrecque Mar 31 '17 at 04:34
  • You can eliminate the parameter, i.e. `(const std::bad_alloc&)`. But instead of that, you should really record what `xa.what()` is telling you. It is not a good habit to get into by totally ignoring the system error strings and creating your own interpretation of the error. One big reason is that system error strings can be researched (web search) to find out more info, since the system error strings are consistent and encountered by thousands of programmers. – PaulMcKenzie Mar 31 '17 at 04:38
  • Just wanted to thank you for your suggestions on this code. At the time, it was a night before an assignment was due, and your aid was invaluable. The teacher noticed that his requirement was incorrect and gave us the solution the next day, but you allowed me to turn this in on time. Thank you. – Denis G. Labrecque Jan 05 '20 at 00:43