3

I am trying to traverse a double array of an unknown size.

Why does the following not work?

for (unsigned int = 1; i < sizeof(anArray)/sizeof(double); i++) {
    ...
}

Everything compiles fine (g++ -Wall -Werror -std=c++11 app.cpp -o app), but the program simply does not even enter the loop.

Full function:

struct stock_data {
   int sell_index;
   int buy_index;
   double profit;
};

stock_data max_profit(double price_array[]) {
   int sell_index = -1, buy_index = -1, 
      min = 0;

   double profit = 0.0;

   for(int i = 1; i < size; i++) {

      if(price_array[i] - price_array[min] > profit) {
         sell_index = min;
         buy_index = i;
         profit = price_array[i] - price_array[min];
      }

      if(price_array[i] < price_array[min]) {
        min = i;
      }
   }  

   return {sell_index, buy_index, profit};
}

int main() {
   double yesterday[] = {0.1, 0.2, 0.3, 0.4};
   stock_data data = max_profit(yesterday);
   cout << data.profit << endl;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Mike B
  • 125
  • 1
  • 1
  • 10
  • 1
    `sizeof(anArray)` is telling you the size of a pointer, which when divided by the size of a double, is 0 or 1. – Paul Draper Feb 27 '15 at 05:16
  • 3
    My crystal ball says you passed `anArray` into this function and it decayed to a pointer. Wish I could see enough code to be sure. – Retired Ninja Feb 27 '15 at 05:16
  • where this array is declared? if it's in some other block it may not work. – user1627167 Feb 27 '15 at 05:16
  • Not enough code to determine anything. That code would work if you declared anArray as double anArray[] and anArray hasn't decayed to just a pointer. – thang Feb 27 '15 at 05:17
  • 1
    Talk is cheap. Show us the code. – Anmol Singh Jaggi Feb 27 '15 at 05:19
  • it must of decayed to a pointer. – Mike B Feb 27 '15 at 05:19
  • You mean "must have", "must of" is just nonsense. In your loop, are you sure you want to initialize `i` (which is missing) to `1`, and not `0`? As written, even if you'd managed to get the number of elements correct, you'd skip the first element in the array (assuming you index using `i` and not `i-1` or something similar). – Praetorian Feb 27 '15 at 05:23
  • `unsigned int = 1;` should give a compilation error – M.M Feb 27 '15 at 05:27

4 Answers4

9

In C++ operator sizeof is a compile-time operator. That is its value is calculated by the compiler not at run-time.

So if as you say the array has unknown size then the compiler can not calculate the size of the memory occupied by the array.

So it is evident that anArray is pointer to first element of the array that you might pass to the function.

Thus this expression

sizeof(anArray)/sizeof(double)

is equivalent to expression

sizeof( double * )/sizeof( double ).

You have to pass also the size of the array explicitly to the function.

Take into account that an array passed by value to a function is converted to pointer to its first element.

Or the other approach is to use conteiner std::vector<double>instead of the arrays.

You could declare your function like

stock_data max_profit(double price_array[], size_t size );

and call it like

 stock_data data = max_profit( yesterday, 
                               sizeof( yesterday ) / sizeof( *yesterday ) );
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
5

sizeof(anArray) returns the size of the type anArray, probably *double. You have to pass the array size as an additional parameter or use std::vector and it's size method.

Have a look at the C main function main(argc, argv) where argv are the parameters of the program and argc is their count. Or take strings, their end is determined by a special character \0 that determines its end. But that's error prone.

In C/C++ there is no other way with arrays.

usr1234567
  • 21,601
  • 16
  • 108
  • 128
2

If you really want to avoid passing the size, you can add a NaN at the end of the array and check for it in the loop.

#include<cmath>
...
for(int i = 0; !isnan(price_array[i]; i++){
    ...
}

To add the NaN when you're declaring the array:

double price_array{0.0d, 5.9d, nan()};

Of course, this assumes you won't have any other NaNs in your array as part of the actual data.

Tyler
  • 1,818
  • 2
  • 13
  • 22
1

Your function max_profit does not know the size of the array price_array. If you want the function to accept arrays of varying size, you have two options:

  • Pass the size value alongside the array (C style)
  • Use std::vector object which holds the size inside (C++ style)
CygnusX1
  • 20,968
  • 5
  • 65
  • 109