0

I want to find the size of any array type. My code is:

#include <iostream>

using namespace std;

template <typename t>
int some_function(t arr []){
    int s;
    s = sizeof(arr)/sizeof(t);
    return s;
}

int main(){
    int arr1 [] = {0,1,2,3,4,5,6,7};
    char arr2 [] = {'a','b','c','d','e'};
    int size;
    size = some_function(arr1);
    cout << "Size of arr1 : "<<size<<endl;
    size = some_function(arr2);
    cout << "Size of arr2 : "<<size<<endl;
    return 0;
}

When I run this code on cpp.sh output is:

Size of arr1 : 2
Size of arr2 : 8

and when I run it on CodeBlocks and Visual Studio output is:

Size of arr1 : 1
Size of arr2 : 4

I want it to print the exact size of an array which is:

Size of arr1 : 8
Size of arr2 : 5

SOLUTION

I found the solution with the help of rsp and Cheersandhth.-Alf. I was passing the array by value which is implicitly converted to pointer. After reading this article and answer provided by rsp I passed the array by reference. So final code is:

#include <iostream>

using namespace std;

template <typename t, int s>
int some_function(t (&arr)[s]){
    return s;
}

int main(){
    int arr1 [] = {0,1,2,3,4,5,6,7};
    char arr2 [] = {'a','b','c','d','e'};
    int size;
    size = some_function(arr1);
    cout << "Size of arr1 : "<<size<<endl;
    size = some_function(arr2);
    cout << "Size of arr2 : "<<size<<endl;
    return 0;
}

Thank you everyone for help...

MAY
  • 667
  • 1
  • 6
  • 21
  • 1
    Why not use `std::array` or `std::vector`? – Ed Heal Nov 04 '17 at 09:58
  • 1
    `arr` is a pointer, not an array. – melpomene Nov 04 '17 at 10:00
  • Closed as FAQ. [[[ – Cheers and hth. - Alf Nov 04 '17 at 10:01
  • http://en.cppreference.com/w/cpp/iterator/size – cpplearner Nov 04 '17 at 10:08
  • Hey, who removed my https://stackoverflow.com/questions/43444235/c-does-gcc-provide-extended-macro-function-like-countof-the-size-of-an-arra link? – melpomene Nov 04 '17 at 10:16
  • @melpomene: I did. It appeared to be about compiler-specific functionality, which would be entirely wrong as an answer to this question. It's a FAQ. – Cheers and hth. - Alf Nov 04 '17 at 10:20
  • 1
    @Cheersandhth.-Alf Well, the [answer](https://stackoverflow.com/a/43444328/4944425) to that question is not compiler specific. – Bob__ Nov 04 '17 at 10:31
  • @Bob__: Right, I see. It would be nice with some other reference to C++ `std::size`, because I don't think that's in the (old) FAQ. The linked answer suffers from appearing to be about gcc functionality, and does not mention the limitation discussed in the FAQ for a Do It Yourself `constexpr` `size`, inability to find size of array by reference, which unfortunately still applies to `std::size` (it's not been fixed). I think a question purely about `std::size` could be nice along with the FAQ ref. Or better, if Someone™ updated that FAQ. :) – Cheers and hth. - Alf Nov 04 '17 at 10:50
  • 1
    It isn't really to do with *passing an array by value*, because that doesn't exist in C++. What happens is that a function parameter `T t[N]` gets adjusted to `T* t` – juanchopanza Nov 04 '17 at 11:24

2 Answers2

2

It is called arrays decaying into pointers. When you pass an array by value they decay into a pointer. So, the size of the array is nothing but the size of a pointer which is depends on the system.

msc
  • 33,420
  • 29
  • 119
  • 214
  • So how can I find the size of an array, without passing the size explicitly – MAY Nov 04 '17 at 10:02
  • @MAY - Please read my comment above – Ed Heal Nov 04 '17 at 10:03
  • @MAY One way is to use a sentinel value like the `'\0'` character used to terminate c-style strings. – user0042 Nov 04 '17 at 10:03
  • @EdHeal Thanks for suggesting that, but what if I don't want to use vector or array container – MAY Nov 04 '17 at 10:12
  • @may: You can just read the FAQ linked as a duplicate. – Cheers and hth. - Alf Nov 04 '17 at 10:20
  • @Cheersandhth.-Alf: I am reading that, thanks – MAY Nov 04 '17 at 10:23
  • 1
    Thanks to everyone for help and especially **rsp** and @Cheersandhth.-Alf. I am passing the array by value which is converted into pointer implicitly so I passed it by reference. `template ` `int some_function(t (&arr)[N]){` `return N;` `}` – MAY Nov 04 '17 at 10:50
  • 1
    What really happens is that `t arr []` is *type adjusted* to `t* arr`. Where array decaying comes in is that it allows passing an array to the function to compile. But the function parameter is always a pointer. – juanchopanza Nov 04 '17 at 11:23
-4

While calling your function try adding datatype of your array

size = some_function<int>(arr1);

size = some_function<char>(arr2);
msc
  • 33,420
  • 29
  • 119
  • 214
Utpal Dutt
  • 383
  • 3
  • 18