1

I want to calculate the 'sizeof' array:

char* arr[] = { "abc", "def" };

When I call sizeof manually, immediately after the initialization of the array, it works fine. However if I pass the array to some function, It doesn't give the same result.

int test(char* b[]) {
    return (int)sizeof(b);
}

int _tmain(int argc, _TCHAR* argv[])
{
    char* arr[] = { "abc", "def" };
    int p = test(arr); // gives 4
    int k = sizeof(arr); // gives 8
    ...
}

So what's the problem? Sorry for the newbie question, but I really miss it.

grjj3
  • 383
  • 2
  • 5
  • 15
  • possible duplicate of [Sizeof array passed as parameter](http://stackoverflow.com/questions/1328223/sizeof-array-passed-as-parameter) – Bo Persson Apr 08 '12 at 18:53

5 Answers5

3

When you use sizeof on arr in _tmain, you're taking the size of the array itself, which is 2 * sizeof(char*) = 2 * 4 = 8 on your architecture, because your char*s occupy 32 bits = 4 bytes.

When you use sizeof on b in test, you're taking the size of a pointer to the first element of the array, which is sizeof(char**) = 4 on your architecture. The reason you're taking the size of the pointer and not the array is that you can't pass arrays as such to functions - instead, you pass them via a pointer to their first element. The information about the actual size of the array is thus lost in the call. Put another way, the signature of your function test is equivalent to int test(char **b).

Stuart Golodetz
  • 20,238
  • 4
  • 51
  • 80
  • 3
    Thank you very much sir for a brief explanation. I appreciate all the other answers as well. – grjj3 Apr 08 '12 at 15:35
2

This is because when you call a function in C or C++, arrays decay to pointers.

In the first case, the compiler sees arr as an array; in the second case, all it sees is a pointer to a pointer.

Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

At point where test is defined, compiler cannot see what b points to. So, sizeof gives the size of a pointer (actually, size of a pointer to pointers).

Inside main, it can see perfectly well that real size is two pointers.

There really is no such thing as passing array into function - only pointer to array is passed. The [] form is just syntactic sugar.

Johan Kotlinski
  • 25,185
  • 9
  • 78
  • 101
0

You lose the size information when calling test because you array decays to a pointer.

You have to pass the size of the array as additional parameter, or fix it in the parameter type.

bitmask
  • 32,434
  • 14
  • 99
  • 159
0

Welcome to C++.

You cannot pass an array to a function at all. In C++, arrays decay to pointers most of the time. You are trying to pass an array but in fact passing a pointer, and your second sizeof evaluates to the size of that pointer.

You should not care about any of this. Arrays are specialized low-level data structures that should be only used to implement higher-level abstractions. Use std::vector instead.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243