0

The project I am working on for class is a delivery scheduling program. The first part of the project had use create static arrays to store all of the information for the deliveries. This part has us replace those static arrays with dynamic ones. The arrays are meant to stay within 50-75% full at all times, and so, I am trying to maintain them around 62-64% full to prevent having to resize at boundaries. The problem I am having comes with calculating and resizing the arrays.

void grow_array(int &array_size, int &number_of_deliveries,
                string name_of_sender[], string shipping_type[],
                double weight[], string id[])
{
   double test_percentage;
   test_percentage = ((100 * number_of_deliveries) / array_size);
   if(number_of_deliveries = 2)
   {
      array_size = 5;
      string *n_t_array = new string[array_size];
      string *s_t_array = new string[array_size];
      string *i_t_array = new string[array_size];
      double *w_t_array = new double[array_size];

      for(int i = 0; i < number_of_deliveries; i++)
      {
         n_t_array[i] = name_of_sender[i];
         s_t_array[i] = shipping_type[i];
         i_t_array[i] = id[i];
         w_t_array[i] = weight[i];
      }

      delete [] name_of_sender;
      delete [] shipping_type;
      delete [] id;
      delete [] weight;

      name_of_sender = n_t_array;
      shipping_type = s_t_array;
      id = i_t_array;
      weight = w_t_array;

      n_t_array = NULL;
      s_t_array = NULL;
      i_t_array = NULL;
      w_t_array = NULL;
   }
   /*if(test_percentage >= 62)
   {
       int new_array_size = ((100 * number_of_deliveries) / 64);
       array_size = new_array_size;
       cout << "INSIDE GROW ARRAY/JUST AFTER CALCULATING NEW SIZE/PRINT NEW ARRAY SIZE: " << new_array_size << endl;
       cout << "INSIDE GROW ARRAY/JUST AFTER CALCULATING NEW SIZE/PRINT ARRAY SIZE: " << array_size << endl;
       string *n_t_array = new string[array_size];
       string *s_t_array = new string[array_size];
       string *i_t_array = new string[array_size];
       double *w_t_array = new double[array_size];

       for(int i = 0; i < number_of_deliveries; i++)
       {
           n_t_array[i] = name_of_sender[i];
           s_t_array[i] = shipping_type[i];
           i_t_array[i] = id[i];
           w_t_array[i] = weight[i];
       }

       delete [] name_of_sender;
       delete [] shipping_type;
       delete [] id;
       delete [] weight;

       name_of_sender = n_t_array;
       shipping_type = s_t_array;
       id = i_t_array;
       weight = w_t_array;

       n_t_array = NULL;
       s_t_array = NULL;
       i_t_array = NULL;
       w_t_array = NULL;
   }*/
}

As you can see, I've tried calculating the new array size and hard coding it. I don't know where I am going wrong with this and what is leading to the seg fault. The arrays start off as size three (#define SIZE 3).

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • 2
    You *do* know how to pass arguments by reference, but you pass references to variables which do not change in the function. Think about that for a while. – Some programmer dude Mar 01 '18 at 07:12
  • 3
    Then a note about your code: Why all the pointers? Why not `std::vector`? And if the "arrays" are related, why not put them in a structure? – Some programmer dude Mar 01 '18 at 07:13
  • `if(number_of_deliveries = 2)` is not right. I am sure you meant `if(number_of_deliveries == 2)`. – R Sahu Mar 01 '18 at 07:16
  • @Someprogrammerdude unfortunately this project requires we use dynamic arrays. Id love to use a vector, but the professor wants to prove a point and give us pointers practice – Harrison Weiss Mar 01 '18 at 07:18
  • Regarding the structure comment: A better solution would be to have a structure containing the name, type, id and weight, and then have an array of that structure. – Some programmer dude Mar 01 '18 at 07:23
  • @RSahu good catch, thank you – Harrison Weiss Mar 01 '18 at 07:24
  • @Someprogrammerdude If a structure is like a class, I've yet to really learn it. I'm only at the beginning of CS II and we've only scratched the surface of using classes. If I knew how to implement it, I would have used a class as it would make a lot more sense – Harrison Weiss Mar 01 '18 at 07:29
  • Then we come back to the big problem (besides the assignment instead of comparison): In the function the variable `name_of_sender` (and the other "array" arguments) are passed by value, so they are *copies* of the original pointer. Changing the copy (with e.g. `name_of_sender = n_t_array`) will not change the original. The variables you do *not* modify in the function are passed by reference, while the variables you do modify are not. – Some programmer dude Mar 01 '18 at 07:32
  • @Someprogrammerdude Aren't all arrays passed by refernece inherently, even when they are dynamically allocated? When passing an array, aren't you really passing the reference of the first part of that array? Unless dynamically allocated arrays are not passed by reference inherently. Also, when I use 'name_of_sender = n_t_array', I am trying to change the pointer of the array 'name_of_sender' to the place in memory where my temporary array is pointing, giving me access to the larger array while being able to call it with the 'name_of_sender' identifier – Harrison Weiss Mar 01 '18 at 07:36
  • Sounds like a great time to learn structs and classes. Don't wait to be taught or you'll find yourself falling behind. – user4581301 Mar 01 '18 at 07:37
  • Handy reading: https://stackoverflow.com/questions/1461432/what-is-array-decaying – user4581301 Mar 01 '18 at 07:39
  • No, passing a pointer copies the pointer, just like any other argument. And if you pass an actual array (which decays to a pointer to its first element) then you *can't* modify that, as arrays have a fixed location that can not be changed. – Some programmer dude Mar 01 '18 at 07:41
  • @user4581301 I would love to learn them ahead of the curve, but this project is due friday in addition to have to implement file i/o in the next project (also due friday). Also, I am a CS minor and so I spend a lot of time away from a computer for my major and dont get as much time to practice as I would like or need. – Harrison Weiss Mar 01 '18 at 07:42
  • If you pass a pointer, that pointer is a reference to what it point at. But what of the pointer itself? The pointer is passed by value, and is copied. if you change where the copy of a pointer points, the original is unchanged. As for not enough time, that's precisely why you cant afford to waste it. There are a few things that are so much better than the alternative that they pay for themselves in the long run. Datastructures and debuggers probably top the list when programming. – user4581301 Mar 01 '18 at 07:48
  • @user4581301 Okay, I understand now that I am only passing the pointers to the arrays by reference so even though I am copying the info into the new arrays with ne sizes, when I try to change the array pointers, I am only passing them by value and so I leak all of that memory once I leave the scope of my function. How can I pass the array pointers by reference? What I've been reading doesn't make much sense to me. I need an array double pointer or something? – Harrison Weiss Mar 01 '18 at 08:10
  • Word: Forget you ever heard about pointers and operator `new[]`. Use `std::vector`. If and when you actually do need a pointer, which might be forever from now, use either `std::make_unique` or `std::make_shared`. – Jive Dadson Mar 01 '18 at 08:51
  • Double pointer (`T ** pointer`) or reference to pointer (`T *& pointer`) both work. Use the reference where possible because it helps protect you from some common mistakes and the compiler can sometimes find optimization opportunities. – user4581301 Mar 01 '18 at 16:18

0 Answers0