I notice two different behaviors when invoking sizeof on primitive array that is not argument of function, and calling it on one that is argument of function.
Example:
I have array of six ints (in my codebase, it's much more than that). I am trying to write function that places those ints in std::vector<std::pair<std::pair<int, int>, int> >
, which means the function must check that the size of the array it's given is multiple of three, before constructing the vector. I test sizeof a / sizeof a[0]
, right after declaring the int array, and it correctly returns 6. However, when I do:
#include <iostream>
using namespace std;
void doSomething(const int* a)
{
cout << sizeof a / sizeof a[0] << endl;
// do some real work down here
}
int main()
{
int a[] = {40,30,60,38,609,780};
doSomething(a);
}
it does not return 6. Why is this happening and what is the quickest and dirtiest way around this unexpected behavior?