1
void f(int* b[])
{
    cout << sizeof(*b) << endl;
}

int main()
{
    int x[4] = {1, 2, 3, 4};
    int* a[] = {&x[0], &x[1], &x[2], &x[3]};

    cout << sizeof(a) << endl;

    f(a);
}

The program is output firstly 32 (in main) and 8 (in function)

How do i handle this situation?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
tango-2
  • 94
  • 1
  • 10
  • You must pass the size of the array into your function as well. – ChrisMM Dec 12 '19 at 11:50
  • 2
    Does this answer your question? [When passing an array to a function in C++, why won't sizeof() work the same as in the main function?](https://stackoverflow.com/questions/36525798/when-passing-an-array-to-a-function-in-c-why-wont-sizeof-work-the-same-as) – ChrisMM Dec 12 '19 at 11:51
  • The size of a pointer is the size of the pointer itself, not what it might point to (since the compiler doesn't really know what it might point to). – Some programmer dude Dec 12 '19 at 11:51
  • 1
    Also, if you wanted to really compare apples to apples, in `f` you should have done `sizeof(b)` and not `sizeof(b[0])` (which is what `sizeof(*b)` really is). Not that it would change anything, since `b` is also a pointer. – Some programmer dude Dec 12 '19 at 11:53
  • The parameter type `int* b[]` is equivalent to `int** b`, while the type of `a` is not `int*[]`, but `int*[4]` – molbdnilo Dec 12 '19 at 11:54
  • You pass a pointer to an array, and the size of pointer on your platform is 8 bytes (apparently 64 bit platform). If you pass 'int b[const]', it would decay into a pointer as well, and its size would be 8 bytes as well. – Uri Raz Dec 12 '19 at 12:45

3 Answers3

2

If I have understood correctly you are trying to get the size of the passed array in the function.

Do it the following way

template <size_t N>
void f( int* ( &b )[N] )
{
    cout << sizeof( b ) << endl;
}

In C you should declare the function with one more parameter like

void f(int* b[], size_t n )
{
    printf( "%zu\n", n * sizeof( *b ) );
}

and call it like

f(a, sizeof( a ) / sizeof( *a ) );

Such an approach you can use also in C++.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Better put the size arguments before the array pointer in C, as that allows you to use them to declare pointers to arrays: `smoothImage(int width, int height, Pixel image[height][width])` Or equivalently, declaring the pointer the way it's understood by the compiler: `smoothImage(int width, int height, Pixel (*image)[width])` – cmaster - reinstate monica Dec 12 '19 at 12:08
  • @cmasterThere is no variable length array. So your suggestion does not male sense in this situation. – Vlad from Moscow Dec 12 '19 at 12:11
  • There is. In C. Since C99. Not in C++, that language is vastly inferior to C99 in this regard. – cmaster - reinstate monica Dec 12 '19 at 12:50
1

You handle the situation by passing the number of elements in the array using a size_t type.

Once the array is passed to a function, it decays to a pointer to the first element. All size information is lost. sizeof is a curious operator insofar that pointer decay doesn't apply to its argument, which accounts for the difference in output.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
0

Array decays to a pointer. That is the reason of the problem. Here's how you can solve it. Pass the array by reference:

template <typename T>
void f(T &b) {
  std::cout << sizeof(b) << std::endl;
}
Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93