7

Possible Duplicate:
Sizeof array passed as parameter

I am being stupid with this sizeof operator in c++, do you have any idea why it is 4 and 12 ?

 void function (int arg[]) {
        cout<<sizeof(arg)<<endl; // 4
    }

    int main ()
    {
        int array[] = {1, 2, 3};
        cout<<sizeof array<<endl; // 12
        function (array);
       return 0;
    }
Community
  • 1
  • 1
Shamim Ahmed
  • 931
  • 2
  • 16
  • 30
  • 2
    [Sizeof array passed as parameter](http://stackoverflow.com/questions/1328223/sizeof-array-passed-as-parameter) – Blastfurnace Mar 01 '12 at 02:37
  • See also http://stackoverflow.com/questions/1641957/is-array-name-a-pointer-in-c and http://stackoverflow.com/questions/720077/calculating-size-of-an-array - this is a FAQ. – Michael Urman Mar 01 '12 at 02:43

4 Answers4

24

In main, the name array is an array so you get the size in bytes of the array with sizeof. However, an array decays to a pointer when passed to a function, so you get sizeof(int*) inside the function.

Be aware that taking an argument in the form of T arg[] is exactly the same as taking the argument as T* arg. So your function is the exact equivalent of

void function(int* arg) {
    cout << sizeof(arg) << endl;
}
Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249
4
 void function (int arg[]) // or void function (int arg[N])

is equivalent to

 void function (int *arg)

thus,

sizeof(arg) == sizeof(int*)

If you intend to pass array itself, then C++ offers you to pass it by reference:

void function (int (&arg)[3])
              //   ^^^ pass by reference

Now,

sizeof(arg) == sizeof(int[3])
iammilind
  • 68,093
  • 33
  • 169
  • 336
0

Your program below is similar to the next one.

void function (int arg[]) {
    cout<<sizeof(arg)<<endl; // 4
}

Program below prints the size of pointer.

void function (int *arg) {
    cout<<sizeof(arg)<<endl; // 4
}
kamae
  • 1,825
  • 1
  • 16
  • 21
-1

Arrays are simply pointers to an arbitrary amount of memory. If you do sizeof(array) it will return the size of a pointer - 4 bytes on 32 bit systems, and 8 bytes on 64 bit systems (if the program is compiled as 64 bit).

This is the same reason that you have to null-terminate your strings in c/c++ - to denote the end of the array.

Simply put, you have the keep track of the size of your arrays yourself. If you allocate an array of 40 bytes, you have to make sure you never access the array above the 40th index (ie. array[39]).

Hope this helps.

Gogeta70
  • 881
  • 1
  • 9
  • 23
  • If arrays are simply pointers, how come inside `main` it prints 12? – fredoverflow Mar 01 '12 at 07:33
  • I can't be 100% sure, but my guess is because your array is static. It's size is determined at compile-time, unlike dynamic memory. Sizeof() may work on static arrays, but i can assure you it won't work on arrays that are allocated dynamically during runtime. – Gogeta70 Mar 01 '12 at 14:08
  • 1
    arrays are not simply pointers. – xaxxon Sep 09 '17 at 01:26
  • @xaxxon Yes, they are when it comes right down to it. An array is a pointer to an area of memory containing a sequence of a specified datatype. – Gogeta70 Sep 12 '17 at 16:27
  • 1
    @Gogeta70 An array is a language construct which is distinct from and behaves differently than a pointer. A pointer is also a language construct. Both may contain a memory address, but neither is "simply" the other and both have unique behaviors within the language. – xaxxon Sep 12 '17 at 17:36
  • @xaxxon I'm afraid you're mistaken. Write a simple C/C++ program that uses an array and then open the resulting executable in a disassembler. You'll see the code for your array is treated the same as a pointer at the assembly level. – Gogeta70 Sep 12 '17 at 17:41
  • @Gogeta70 I was simply trying to point out the difference between a language construct and the resulting generated code. From a language perspective "arrays are simply pointers" is clearly false. That was all I was saying. What the linker generates is orthogonal to how the language handles them. Perhaps the context of the question makes your answer sufficiently correct, but many people are confused when they hear "arrays are just pointers" too often and then expect the langauge to treat them the same as well. – xaxxon Sep 12 '17 at 22:23
  • This question is tagged [tag:c++]. In C++, arrays are decidedly different entities from pointers. Arrays can *decay* into pointers, when passed to functions. But you can also pass arrays by reference. Change the code to `template void function(int (&arg)[N]) { ... }` and [observe the result](http://coliru.stacked-crooked.com/a/e7288803105da5a8). With that it should be easy to understand, why people think this answer is wrong. – IInspectable Sep 14 '17 at 19:12