-4

The following

#include <iostream>

void printArraySize ( int * arr ) 
{
    std::cout << sizeof(arr)/sizeof(int);

}

int main () 
{
    int arr [] = {1, 2, 3, 69203};
    printArraySize(arr);  
    return 0;
}

outputs 1 (Proof: http://codepad.org/yKG3mZIz). Explain this nonsense. Does an array forget its size once it enters a function? From what I understand, passing in arr, a memory address, a number, just means making a copy of that number. So sizeof(arr)/sizeof(int) should mean the same thing inside and outside of the function.

shinzou
  • 5,850
  • 10
  • 60
  • 124
Peter Thiel
  • 51
  • 1
  • 4
  • 2
    This is a very old and common question... Your function doesn't receive an array, but a pointer to `int`. In `main` you transmit `arr` which is declared as an array but converted to a pointer to the first element of it because you cannot have array passed to functions in C, only pointers. – Jean-Baptiste Yunès Feb 21 '15 at 20:49
  • Reopened and re-closed as duplicate, but now of the `sizeof` discussion in the SO C++ array FAQ (because the existing alleged duplicate was for the C langauge, and for this question the difference between C and C++ is very significant). – Cheers and hth. - Alf Feb 21 '15 at 22:34
  • 1
    Your assertions in the question make no sense. Don't accuse the language of "nonsense" when you're making it yourself :P – Lightness Races in Orbit Feb 22 '15 at 00:08

2 Answers2

2

In main, arr is an array and sizeof yields the size of the array.

In printArraySize, arr is a pointer to int and sizeof yields the size of the pointer to int object.

In C you cannot pass (directly) arrays to functions, you can only pass a pointer to the first element of the array. To get the size of the array in printArraySize you need to pass it explicitly as an argument to the function.

In C++ you can pass C arrays by reference but you shouldn't use C arrays in the first place but use std::array or std::vector instead.

ouah
  • 142,963
  • 15
  • 272
  • 331
1

It is because you check sizeof ( int*) which is sizeof pointer to int. This is because arr passed to function in

int arr [] = {1, 2, 3, 69203};
printArraySize(arr);

decays to a pointer to int. It is just a pointer to integer, first element of array.

C

For doing what you would like to achieve there is a macro often used (for static arrays):

#define ARRAY_SIZE(x) (sizeof((x)) / sizeof((x)[0]))

example:

int arr [] = {1, 2, 3, 69203};
printf( "%d", ARRAY_SIZE(arr));

C++

template<class T, size_t n>
size_t array_size( T(&)[n]) {
    return n;
}
4pie0
  • 29,204
  • 9
  • 82
  • 118
  • 2
    upvoted to counter anonymous downvote: I don't see anything very wrong or misleading with this answer. The explanation could have been more clear, but that's just not voting up – Cheers and hth. - Alf Feb 21 '15 at 21:16
  • 1
    there is justice in this world – 4pie0 Feb 21 '15 at 21:36
  • 1
    Not really. It's not Alf's job to "undo" someone else's right to vote. Sigh. "Justice" is about objective values but voting is a subjective act. – Lightness Races in Orbit Feb 22 '15 at 00:09
  • well, it is not coincidence that we think about democracy as most right and fair system, this works so good because we trust the correct choice can be made when pointed out by majority of votes. In other words the (incorrect) noise will be filtered out – 4pie0 Feb 22 '15 at 00:22