0

In my project I have an array of floats which I want to pass through a function. I calculate the size of the array before passing the array through the function and I get the correct size.

I now pass the array as argument to a function and the size is wrong.

float histogram1[ 27 ];
float histogram2[ 27 ];
........ Do stuff ...
// Histogramm size is correct here the output is 27
cout << " histograms size is  " << sizeof(histogram1)/sizeof(float) << endl;
ut.categoricalhistogramCompare(histogram1, histogram2);

The first line of my function is:

std::vector<float>  Utilities::categoricalhistogramCompare( float histA[], float histB[]){
    const int N = sizeof(histA) / sizeof(float);
    //Size output here is 2
    cout << " size of histograms " << N<< endl;
    ..... Do stuff .....
}

Any clues why this happens? Is it the way I define the array function arguments?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • Well, `sizeof` is not a runtime function. If you use `float[]` for a parameter type, the compiler won't know that it is a 27 element array. – Mr Lister May 14 '15 at 13:48
  • The function will take the array as a pointer and sizeof the pointer is 8 and sizeof float is 4. Hence the function is printing the value as 2. The function won't know the sizeof the array, you have to explicitly pass it. Or may be use vector (or std::array) – Nipun Talukdar May 14 '15 at 13:53
  • Great, thanks for the super-fast answers. I'll pass it explicitly then, even though I don't believe it is a good programming paradigm. – Panagiotis Chatzichristodoulou May 14 '15 at 13:56

0 Answers0